Skip to content

Commit

Permalink
refactor: One redis to rule them all
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Sep 25, 2021
1 parent 2e7dc42 commit d5a28a6
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 47 deletions.
27 changes: 0 additions & 27 deletions infra/redis.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion infra/web.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ spec:
KETCHUP_DB_NAME: ketchup
KETCHUP_DB_USER: ketchup
KETCHUP_SCHEDULER_ENABLED: "false"
KETCHUP_REDIS_ADDRESS: ketchup-redis:80
KETCHUP_REDIS_ADDRESS: redis:80
KETCHUP_DOCKER_USERNAME: vibioh
secrets:
KETCHUP_DB_PASS: AgDOK5Ph8WD8SbBi+Mn/ywPirM59KRfhAjkAqaOqDePJJDd3uIQhoJCKN5HCwYsUoux173go8RrPtyhc5CxKm+/sc6JiqeajZVv/mc/EVPa0MyNpCN376jXIvo0P+joasjBGys/rtqEauhdn7hWGr87PnJbL5A2k+XD3kMauzcKJtMXSYkz2QSTx7TW1kW+5zJs/Cgp3FS9qHZ8buvktggnKeffhd0paYJJrLmtyKxKzmkXcyL8Q3i3QnAbXC3u4vSPxP5PMFMEqDZNrmmYiEJVOmNcW+R0jO7u6q3gwWkATL7E0bbtWuanp6/FXgUhlgpmD+i9BSwTEzrglFHEGPESlg+uYDqXDXOKGBzmQPcq7aAU+/jcEyh5ozkF8aVod7fVxFo+g1nGCbwAhXK5E+iT+KZ1Q50mFkhRIOYSQ9aesHONBPpiaLp8y00OJ6oEmbCSmuDNOBTYXo/JIjUnJ5LFIIskcT5MWKPkYV3hlSzshQBR9JPXLqdaiIEiRicjnfU/qWrwel0PlEaU2L1FL1qsikxPkfGu1HHwj0CusGpGduD3DKC1mTEc1U4zAVvmqSMpoSguyfOYf/2C/m+jcd5YTkiS9JpTGUx4OVFvBD6Ug9uT7igLI8nNFZeTHMCnCerG7GES0wapWehYX/wYFaqw9K1ym0Jz2fHByCVNEt8XH0GxFszdTw2iF6Q4AIH4D8lLB9umdTPP91L5hYVSIOpv9qQPUa9pxB5aNwEG9Q+BQt31mM/zNEglv033PqYOAM12KsGqwew9HJBAUFQU=
Expand Down
30 changes: 13 additions & 17 deletions pkg/ketchup/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/ViBiOh/httputils/v4/pkg/logger"
"github.com/ViBiOh/httputils/v4/pkg/uuid"
)

// SecurityQuestion is a question for fighting against bot
Expand Down Expand Up @@ -40,29 +41,20 @@ var (
}
)

func uuid() string {
raw := make([]byte, 16)
if _, err := rand.Read(raw); err != nil {
logger.Fatal(err)
return ""
}

raw[8] = raw[8]&^0xc0 | 0x80
raw[6] = raw[6]&^0xf0 | 0x40

return fmt.Sprintf("%x-%x-%x-%x-%x", raw[0:4], raw[4:6], raw[6:8], raw[8:10], raw[10:])
}

func (a App) generateToken(ctx context.Context) (SecurityPayload, error) {
questionID, err := rand.Int(rand.Reader, big.NewInt(int64(len(colors))))
if err != nil {
return SecurityPayload{}, fmt.Errorf("unable to generate random int: %w", err)
}

token := uuid()
token, err := uuid.New()
if err != nil {
return SecurityPayload{}, fmt.Errorf("unable to generate uuid: %s", err)
}

id := questionID.Int64()

if err := a.redisApp.Store(ctx, token, fmt.Sprintf("%d", id), time.Minute*5); err != nil {
if err := a.redisApp.Store(ctx, tokenKey(token), fmt.Sprintf("%d", id), time.Minute*5); err != nil {
return SecurityPayload{}, fmt.Errorf("unable to store token: %s", err)
}

Expand All @@ -73,7 +65,7 @@ func (a App) generateToken(ctx context.Context) (SecurityPayload, error) {
}

func (a App) validateToken(ctx context.Context, token, answer string) bool {
questionIDString, err := a.redisApp.Load(ctx, token)
questionIDString, err := a.redisApp.Load(ctx, tokenKey(token))
if err != nil {
logger.Warn("unable to retrieve captcha token: %s", err)
return false
Expand All @@ -94,7 +86,11 @@ func (a App) validateToken(ctx context.Context, token, answer string) bool {
}

func (a App) cleanToken(ctx context.Context, token string) {
if err := a.redisApp.Delete(ctx, token); err != nil {
if err := a.redisApp.Delete(ctx, tokenKey(token)); err != nil {
logger.WithField("token", token).Error("unable to delete token: %s", err)
}
}

func tokenKey(token string) string {
return "ketchup:token:" + token
}
2 changes: 1 addition & 1 deletion pkg/provider/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (a app) Start(registerer prometheus.Registerer, done <-chan struct{}) {

cron.New().Now().Each(time.Minute).OnError(func(err error) {
logger.Error("unable to get rate limit metrics: %s", err)
}).Exclusive(a.redisApp, "github_rate_limit_metrics", 45*time.Second).Start(func(ctx context.Context) error {
}).Exclusive(a.redisApp, "ketchup:github_rate_limit_metrics", 45*time.Second).Start(func(ctx context.Context) error {
value, err := a.getRateLimit(ctx)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion pkg/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func New(config Config, notifierApp notifier.App, redisApp redis.App) App {
func (a app) Start(done <-chan struct{}) {
cron.New().At(a.hour).In(a.timezone).Days().OnError(func(err error) {
logger.Error("error while running ketchup notify: %s", err)
}).OnSignal(syscall.SIGUSR1).Exclusive(a.redisApp, "ketchup_notify", 10*time.Minute).Start(func(ctx context.Context) error {
}).OnSignal(syscall.SIGUSR1).Exclusive(a.redisApp, "ketchup:notify", 10*time.Minute).Start(func(ctx context.Context) error {
logger.Info("Starting ketchup notifier")
defer logger.Info("Ending ketchup notifier")
return a.notifierApp.Notify(ctx)
Expand Down

0 comments on commit d5a28a6

Please sign in to comment.