Skip to content

Commit 417d4b7

Browse files
authored
[runx+envsec] Use same cache location as auth pkg (#154)
## Summary Use consistent cache locations everywhere. Fix envsec aws credentials cache bug where cache key did not take into account changing organizations. ## How was it tested?
1 parent 4b2f0c8 commit 417d4b7

File tree

10 files changed

+43
-69
lines changed

10 files changed

+43
-69
lines changed

envsec/internal/awsfed/awsfed.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package awsfed
22

33
import (
44
"context"
5+
"crypto/sha256"
56
"encoding/json"
67
"fmt"
78

@@ -12,7 +13,7 @@ import (
1213
"go.jetpack.io/pkg/sandbox/auth/session"
1314
)
1415

15-
const cacheKey = "awsfed"
16+
const cacheKeyPrefix = "awsfed"
1617

1718
type AWSFed struct {
1819
AccountID string
@@ -39,8 +40,8 @@ func (a *AWSFed) AWSCreds(
3940
ctx context.Context,
4041
tok *session.Token,
4142
) (*types.Credentials, error) {
42-
cache := filecache.New("envsec")
43-
if cachedCreds, err := cache.Get(cacheKey); err == nil {
43+
cache := filecache.New("jetpack.io/envsec")
44+
if cachedCreds, err := cache.Get(cacheKey(tok)); err == nil {
4445
var creds types.Credentials
4546
if err := json.Unmarshal(cachedCreds, &creds); err == nil {
4647
return &creds, nil
@@ -86,7 +87,7 @@ func (a *AWSFed) AWSCreds(
8687
if creds, err := json.Marshal(output.Credentials); err != nil {
8788
return nil, err
8889
} else if err := cache.SetT(
89-
cacheKey,
90+
cacheKey(tok),
9091
creds,
9192
*output.Credentials.Expiration,
9293
); err != nil {
@@ -95,3 +96,14 @@ func (a *AWSFed) AWSCreds(
9596

9697
return output.Credentials, nil
9798
}
99+
100+
func cacheKey(t *session.Token) string {
101+
id := ""
102+
if claims := t.IDClaims(); claims != nil && claims.OrgID != "" {
103+
id = claims.OrgID
104+
} else {
105+
id = fmt.Sprintf("%x", sha256.Sum256([]byte(t.IDToken)))
106+
}
107+
108+
return fmt.Sprintf("%s-%s", cacheKeyPrefix, id)
109+
}

envsec/internal/filecache/filecache.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,17 @@ import (
88
"time"
99

1010
"github.com/pkg/errors"
11-
"go.jetpack.io/envsec/internal/xdg"
1211
)
1312

1413
var NotFound = errors.New("not found")
1514
var Expired = errors.New("expired")
1615

17-
const prefix = "filecache-"
18-
1916
type cache struct {
20-
appName string
17+
domain string
2118
}
2219

23-
func New(appName string) *cache {
24-
return &cache{appName: appName}
20+
func New(domain string) *cache {
21+
return &cache{domain: domain}
2522
}
2623

2724
type data struct {
@@ -68,7 +65,11 @@ func (c *cache) Get(key string) ([]byte, error) {
6865
}
6966

7067
func (c *cache) filename(key string) string {
71-
dir := xdg.CacheSubpath(c.appName)
68+
cacheDir, err := os.UserCacheDir()
69+
if err != nil {
70+
cacheDir = "~/.cache"
71+
}
72+
dir := filepath.Join(cacheDir, c.domain)
7273
_ = os.MkdirAll(dir, 0755)
73-
return xdg.CacheSubpath(filepath.Join(c.appName, prefix+key))
74+
return filepath.Join(dir, key)
7475
}

envsec/internal/xdg/xdg.go

Lines changed: 0 additions & 44 deletions
This file was deleted.

go.work.sum

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ cloud.google.com/go/compute/metadata v0.2.0/go.mod h1:zFmK7XCadkQkj6TtorcaGlCW1h
33
cloud.google.com/go/compute/metadata v0.2.3/go.mod h1:VAV5nSsACxMJvgaAuX6Pk2AawlZn8kiOGuCv6gTkwuA=
44
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
55
github.com/CloudyKit/fastprinter v0.0.0-20200109182630-33d98a066a53/go.mod h1:+3IMCy2vIlbG1XG/0ggNQv0SvxCAIpPM5b1nCz56Xno=
6+
github.com/adrg/xdg v0.4.0 h1:RzRqFcjH4nE5C6oTAxhBtoE2IRyjBSa62SCbyPidvls=
67
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
78
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4=
89
github.com/cloudflare/ahocorasick v0.0.0-20210425175752-730270c3e184/go.mod h1:tGWUZLZp9ajsxUOnHmFFLnqnlKXsCn6GReG4jAD59H0=
@@ -50,7 +51,6 @@ github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV
5051
github.com/yosssi/ace v0.0.5/go.mod h1:ALfIzm2vT7t5ZE7uoIZqF3TQ7SAOyupFZnkrF5id+K0=
5152
github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE=
5253
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
53-
go.jetpack.io/pkg v0.0.0-20230915205515-567047de7b30/go.mod h1:6RVzBortLFlql8s8oKJTX2+H7DDzp8Lr7wiIOI3FauU=
5454
golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
5555
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
5656
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=

pkg/go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module go.jetpack.io/pkg
33
go 1.20
44

55
require (
6-
github.com/adrg/xdg v0.4.0
76
github.com/cavaliergopher/grab/v3 v3.0.1
87
github.com/codeclysm/extract v2.2.0+incompatible
98
github.com/coreos/go-oidc/v3 v3.6.0

pkg/go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 h1:wPbRQzjjwFc0ih8puEVAOFGELsn1zoIIYdxvML7mDxA=
22
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8/go.mod h1:I0gYDMZ6Z5GRU7l58bNFSkPTFN6Yl12dsUlAZ8xy98g=
3-
github.com/adrg/xdg v0.4.0 h1:RzRqFcjH4nE5C6oTAxhBtoE2IRyjBSa62SCbyPidvls=
4-
github.com/adrg/xdg v0.4.0/go.mod h1:N6ag73EX4wyxeaoeHctc1mas01KZgsj5tYiAIwqJE/E=
53
github.com/bwesterb/go-ristretto v1.2.0/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
64
github.com/cavaliergopher/grab/v3 v3.0.1 h1:4z7TkBfmPjmLAAmkkAZNX/6QJ1nNFdv3SdIHXju0Fr4=
75
github.com/cavaliergopher/grab/v3 v3.0.1/go.mod h1:1U/KNnD+Ft6JJiYoYBAimKH2XrYptb8Kl3DFGmsjpq4=
@@ -85,7 +83,6 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
8583
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
8684
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
8785
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
88-
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
8986
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
9087
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
9188
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
@@ -106,7 +103,6 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
106103
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
107104
golang.org/x/sys v0.0.0-20210616045830-e2b7044e8c71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
108105
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
109-
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
110106
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
111107
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
112108
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=

pkg/sandbox/auth/auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func storeDir() string {
3636
if err != nil {
3737
cacheDir = "~/.cache"
3838
}
39-
return filepath.Join(cacheDir, "go.jetpack.io", "auth")
39+
return filepath.Join(cacheDir, "jetpack.io", "auth")
4040
}
4141

4242
func (c *Client) LoginFlow() (*session.Token, error) {

pkg/sandbox/runx/impl/httpcacher/defaults.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package httpcacher
22

33
import (
4+
"os"
45
"path/filepath"
5-
6-
"github.com/adrg/xdg"
76
)
87

98
const xdgSubdir = "jetpack.io/http"
@@ -16,4 +15,11 @@ const xdgSubdir = "jetpack.io/http"
1615
// TODO: consider supporting a shared cache. Consider changing the default caching
1716
// directory structure, to separate the private cache from the shared cache (which
1817
// could be copied between machines).
19-
var defaultCacheDir = filepath.Join(xdg.CacheHome, xdgSubdir)
18+
19+
func defaultCacheDir() string {
20+
cacheHome, err := os.UserCacheDir()
21+
if err != nil {
22+
cacheHome = "~/.cache"
23+
}
24+
return filepath.Join(cacheHome, xdgSubdir)
25+
}

pkg/sandbox/runx/impl/httpcacher/httpcacher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import "net/http"
1717
// It could even implement state-while-revalidate type of logic on the client
1818
// side: https://developer.mozilla.org/en-US/docs/Web/API/Request/cache
1919

20-
var DefaultClient = NewClient(defaultCacheDir)
20+
var DefaultClient = NewClient(defaultCacheDir())
2121

2222
func NewClient(cacheDir string) *http.Client {
2323
return newTransport(cacheDir).Client()

pkg/sandbox/runx/impl/install.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package impl
22

33
import (
44
"context"
5+
"os"
56
"path/filepath"
67

7-
"github.com/adrg/xdg"
88
"go.jetpack.io/pkg/sandbox/runx/impl/registry"
99
"go.jetpack.io/pkg/sandbox/runx/impl/types"
1010
)
@@ -38,7 +38,11 @@ func install(pkgs ...types.PkgRef) ([]string, error) {
3838
}
3939

4040
func installOne(ref types.PkgRef) (string, error) {
41-
rootDir := filepath.Join(xdg.CacheHome, xdgInstallationSubdir)
41+
cacheDir, err := os.UserCacheDir()
42+
if err != nil {
43+
cacheDir = "~/.cache"
44+
}
45+
rootDir := filepath.Join(cacheDir, xdgInstallationSubdir)
4246
reg, err := registry.NewLocalRegistry(rootDir)
4347
if err != nil {
4448
return "", err

0 commit comments

Comments
 (0)