Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get the existing secret in the redis, if not exist, set the new secret #425

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions server/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/memstore"
"github.com/gin-contrib/sessions/redis"
"github.com/no-src/log"
"github.com/no-src/nscache"
)

var (
Expand Down Expand Up @@ -48,12 +50,33 @@
}
if len(redisSecret) > 0 {
secret = redisSecret
} else {
// get the existing secret in the redis, if not exist, set the new secret
secret = getOrSetStoreSecret(address, password, db, secret)

Check warning on line 55 in server/session.go

View check run for this annotation

Codecov / codecov/patch

server/session.go#L53-L55

Added lines #L53 - L55 were not covered by tests
}
// get the existing secret in the redis, if not exist, set the new secret
// TODO
return redis.NewStoreWithDB(maxIdle, network, address, password, strconv.Itoa(db), secret)
}

func getOrSetStoreSecret(address, password string, db int, newSecret []byte) (secret []byte) {
conn := fmt.Sprintf("redis://:%s@%s/%d", password, address, db)
key := "nosrc-gofs-session-secret"
c, err := nscache.NewCache(conn)
if err != nil {
log.Error(err, "init nscache error conn=%s", conn)
return newSecret
}
defer c.Close()
if v, ok := c.GetBytes(key); ok {
secret = v
} else {
log.ErrorIf(c.Set(key, newSecret, 0), "set redis session secret error")
}
if len(secret) == 0 {
secret = newSecret
}
return secret

Check warning on line 77 in server/session.go

View check run for this annotation

Codecov / codecov/patch

server/session.go#L60-L77

Added lines #L60 - L77 were not covered by tests
}

// parseRedisConnection parse the redis connection string
// for example => redis://127.0.0.1:6379?password=redis_password&db=10&max_idle=10&secret=redis_secret
func parseRedisConnection(u *url.URL) (maxIdle int, network, address, password string, db int, secret []byte, err error) {
Expand Down