diff --git a/envsec/go.mod b/envsec/go.mod index 35f4d369..3868caed 100644 --- a/envsec/go.mod +++ b/envsec/go.mod @@ -19,7 +19,7 @@ require ( github.com/pkg/errors v0.9.1 github.com/samber/lo v1.38.1 github.com/spf13/cobra v1.7.0 - go.jetpack.io/pkg v0.0.0-20231002215645-9afeb0623fd3 + go.jetpack.io/pkg v0.0.0-20231012130632-77dcd111db2e go.jetpack.io/typeid v0.1.0 golang.org/x/oauth2 v0.12.0 golang.org/x/text v0.13.0 diff --git a/envsec/go.sum b/envsec/go.sum index d343cb1c..9ca42118 100644 --- a/envsec/go.sum +++ b/envsec/go.sum @@ -106,8 +106,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -go.jetpack.io/pkg v0.0.0-20231002215645-9afeb0623fd3 h1:aMydtVCHn7dfotOyV41VAxX5b5OOsCc4TxOXwDt38Yw= -go.jetpack.io/pkg v0.0.0-20231002215645-9afeb0623fd3/go.mod h1:iaf3e/aENp5luwYFlfCxj+GsiwqHagbvRAY3bIdEgGA= +go.jetpack.io/pkg v0.0.0-20231012130632-77dcd111db2e h1:GtqFrE5uUf6rXFk3zaIqIfLfykrqrB8gp6bDmOfH+2s= +go.jetpack.io/pkg v0.0.0-20231012130632-77dcd111db2e/go.mod h1:m49CAcLbpZttEbzoEWC1SKD+/z5mEiFdw0dQxY6AM5Q= go.jetpack.io/typeid v0.1.0 h1:suTmjNR3y2em2gCTG06agFfcACm3+zuxfziMUk5UXnw= go.jetpack.io/typeid v0.1.0/go.mod h1:E11ObFkKlvsSTxwwYIm+zpbsRthjIMDy/JGBogs2vSo= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= diff --git a/envsec/internal/filecache/filecache.go b/envsec/internal/filecache/filecache.go deleted file mode 100644 index ab700549..00000000 --- a/envsec/internal/filecache/filecache.go +++ /dev/null @@ -1,92 +0,0 @@ -// filecache is a simple local file-based cache -package filecache - -import ( - "encoding/json" - "os" - "path/filepath" - "time" - - "github.com/pkg/errors" -) - -var NotFound = errors.New("not found") -var Expired = errors.New("expired") - -type cache struct { - domain string - cacheDir string -} - -type data struct { - Val []byte - Exp time.Time -} - -type Option func(*cache) - -func New(domain string, opts ...Option) *cache { - result := &cache{domain: domain} - - var err error - result.cacheDir, err = os.UserCacheDir() - if err != nil { - result.cacheDir = "~/.cache" - } - - for _, opt := range opts { - opt(result) - } - - return result -} - -func WithCacheDir(dir string) Option { - return func(c *cache) { - c.cacheDir = dir - } -} - -func (c *cache) Set(key string, val []byte, dur time.Duration) error { - d, err := json.Marshal(data{Val: val, Exp: time.Now().Add(dur)}) - if err != nil { - return errors.WithStack(err) - } - - return errors.WithStack(os.WriteFile(c.filename(key), d, 0644)) -} - -func (c *cache) SetT(key string, val []byte, t time.Time) error { - d, err := json.Marshal(data{Val: val, Exp: t}) - if err != nil { - return errors.WithStack(err) - } - - return errors.WithStack(os.WriteFile(c.filename(key), d, 0644)) -} - -func (c *cache) Get(key string) ([]byte, error) { - path := c.filename(key) - if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) { - return nil, NotFound - } - - content, err := os.ReadFile(path) - if err != nil { - return nil, errors.WithStack(err) - } - d := data{} - if err := json.Unmarshal(content, &d); err != nil { - return nil, errors.WithStack(err) - } - if time.Now().After(d.Exp) { - return nil, Expired - } - return d.Val, nil -} - -func (c *cache) filename(key string) string { - dir := filepath.Join(c.cacheDir, c.domain) - _ = os.MkdirAll(dir, 0755) - return filepath.Join(dir, key) -} diff --git a/envsec/pkg/awsfed/awsfed.go b/envsec/pkg/awsfed/awsfed.go index 09169311..3a6cdeca 100644 --- a/envsec/pkg/awsfed/awsfed.go +++ b/envsec/pkg/awsfed/awsfed.go @@ -11,7 +11,7 @@ import ( "github.com/pkg/errors" "go.jetpack.io/envsec" "go.jetpack.io/envsec/internal/envvar" - "go.jetpack.io/envsec/internal/filecache" + "go.jetpack.io/pkg/filecache" "go.jetpack.io/pkg/sandbox/auth/session" ) @@ -76,9 +76,11 @@ func (a *AWSFed) awsCreds( idToken string, ) (*types.Credentials, error) { - svc := cognitoidentity.New(cognitoidentity.Options{ - Region: a.Region, - }) + svc := cognitoidentity.New( + cognitoidentity.Options{ + Region: a.Region, + }, + ) logins := map[string]string{ a.Provider: idToken,