2017-08-03 12:13:53 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-06-09 23:36:01 +00:00
|
|
|
"context"
|
2017-08-03 12:13:53 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2018-05-05 11:56:53 +00:00
|
|
|
"strings"
|
2023-06-09 23:36:01 +00:00
|
|
|
"time"
|
2017-08-03 12:13:53 +00:00
|
|
|
|
2020-01-24 15:15:17 +00:00
|
|
|
"github.com/gofrs/uuid/v3"
|
2023-06-09 23:36:01 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
redis "github.com/redis/go-redis/v9"
|
2017-08-03 12:13:53 +00:00
|
|
|
)
|
|
|
|
|
2023-06-09 23:36:01 +00:00
|
|
|
const redisDefaultPrefix = "io.luzifer.ots"
|
|
|
|
|
2017-08-03 12:13:53 +00:00
|
|
|
type storageRedis struct {
|
2023-06-09 23:36:01 +00:00
|
|
|
conn *redis.Client
|
2017-08-03 12:13:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newStorageRedis() (storage, error) {
|
|
|
|
if os.Getenv("REDIS_URL") == "" {
|
|
|
|
return nil, fmt.Errorf("REDIS_URL environment variable not set")
|
|
|
|
}
|
|
|
|
|
2023-06-09 23:36:01 +00:00
|
|
|
// We replace the old URI format
|
|
|
|
// tcp://auth:password@127.0.0.1:6379/0
|
|
|
|
// with the new one
|
|
|
|
// redis://<user>:<password>@<host>:<port>/<db_number>
|
|
|
|
// in order to maintain backwards compatibility
|
|
|
|
opt, err := redis.ParseURL(strings.Replace(os.Getenv("REDIS_URL"), "tcp://", "redis://", 1))
|
2017-08-03 12:13:53 +00:00
|
|
|
if err != nil {
|
2023-06-09 23:36:01 +00:00
|
|
|
return nil, errors.Wrap(err, "parsing REDIS_URL")
|
2017-08-03 12:13:53 +00:00
|
|
|
}
|
|
|
|
|
2018-05-05 11:56:53 +00:00
|
|
|
s := &storageRedis{
|
2023-06-09 23:36:01 +00:00
|
|
|
conn: redis.NewClient(opt),
|
2018-05-05 11:56:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2023-06-09 23:36:01 +00:00
|
|
|
func (s storageRedis) Create(secret string, expireIn time.Duration) (string, error) {
|
2018-10-06 18:08:21 +00:00
|
|
|
id := uuid.Must(uuid.NewV4()).String()
|
2023-06-09 23:36:01 +00:00
|
|
|
err := s.conn.SetEx(context.Background(), s.redisKey(id), secret, expireIn).Err()
|
2017-08-03 12:13:53 +00:00
|
|
|
|
2023-06-09 23:36:01 +00:00
|
|
|
return id, errors.Wrap(err, "writing redis key")
|
2017-08-03 12:13:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s storageRedis) ReadAndDestroy(id string) (string, error) {
|
2023-06-09 23:36:01 +00:00
|
|
|
secret, err := s.conn.Get(context.Background(), s.redisKey(id)).Result()
|
2017-08-03 12:13:53 +00:00
|
|
|
if err != nil {
|
2023-06-09 23:36:01 +00:00
|
|
|
if errors.Is(err, redis.Nil) {
|
|
|
|
return "", errSecretNotFound
|
|
|
|
}
|
2017-08-03 12:13:53 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-06-09 23:36:01 +00:00
|
|
|
err = s.conn.Del(context.Background(), s.redisKey(id)).Err()
|
|
|
|
return string(secret), errors.Wrap(err, "deleting key")
|
2017-08-03 12:13:53 +00:00
|
|
|
}
|
2018-05-05 11:56:53 +00:00
|
|
|
|
2023-06-09 23:36:01 +00:00
|
|
|
func (s storageRedis) redisKey(id string) string {
|
|
|
|
prefix := redisDefaultPrefix
|
|
|
|
if prfx := os.Getenv("REDIS_KEY"); prfx != "" {
|
|
|
|
prefix = prfx
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join([]string{prefix, id}, ":")
|
2018-05-05 11:56:53 +00:00
|
|
|
}
|