Skip to content

Commit 4df5a62

Browse files
authored
[caching] Add some basic AWS credentials caching (#1955)
1 parent 53c8fff commit 4df5a62

File tree

4 files changed

+27
-34
lines changed

4 files changed

+27
-34
lines changed

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ require (
4040
github.com/wk8/go-ordered-map/v2 v2.1.8
4141
github.com/zealic/go2node v0.1.0
4242
go.jetpack.io/envsec v0.0.16-0.20240329013200-4174c0acdb00
43-
go.jetpack.io/pkg v0.0.0-20240329230128-09e8a66df983
43+
go.jetpack.io/pkg v0.0.0-20240404001923-7b42192bf9a5
44+
go.jetpack.io/typeid v1.0.0
4445
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225
4546
golang.org/x/mod v0.16.0
47+
golang.org/x/oauth2 v0.18.0
4648
golang.org/x/sync v0.6.0
4749
golang.org/x/tools v0.19.0
4850
gopkg.in/natefinch/lumberjack.v2 v2.2.1
@@ -123,10 +125,8 @@ require (
123125
github.com/therootcompany/xz v1.0.1 // indirect
124126
github.com/ulikunitz/xz v0.5.11 // indirect
125127
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c // indirect
126-
go.jetpack.io/typeid v1.0.0 // indirect
127128
go4.org v0.0.0-20230225012048-214862532bf5 // indirect
128129
golang.org/x/crypto v0.21.0 // indirect
129-
golang.org/x/oauth2 v0.18.0 // indirect
130130
golang.org/x/sys v0.18.0 // indirect
131131
golang.org/x/term v0.18.0 // indirect
132132
golang.org/x/text v0.14.0 // indirect

go.sum

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -363,20 +363,10 @@ github.com/yuin/gopher-lua v0.0.0-20190514113301-1cd887cd7036/go.mod h1:gqRgreBU
363363
github.com/zaffka/mongodb-boltdb-mock v0.0.0-20221014194232-b4bb03fbe3a0/go.mod h1:GsDD1qsG+86MeeCG7ndi6Ei3iGthKL3wQ7PTFigDfNY=
364364
github.com/zealic/go2node v0.1.0 h1:ofxpve08cmLJBwFdI0lPCk9jfwGWOSD+s6216x0oAaA=
365365
github.com/zealic/go2node v0.1.0/go.mod h1:GrkFr+HctXwP7vzcU9RsgtAeJjTQ6Ud0IPCQAqpTfBg=
366-
go.jetpack.io/envsec v0.0.16-0.20240327051701-1c56cf3690d7 h1:f+EANkek7CaHK6LS9RIm9rbOt2WrZYyYFTsXGKUdMgc=
367-
go.jetpack.io/envsec v0.0.16-0.20240327051701-1c56cf3690d7/go.mod h1:Z4tKIpAkdW4tpNUlToVLg7LZFl688GxWae02m3w//G0=
368366
go.jetpack.io/envsec v0.0.16-0.20240329013200-4174c0acdb00 h1:Kb+OlWOntAq+1nF+01ntqnQEqSJkFmLLS0RX5sl5zak=
369367
go.jetpack.io/envsec v0.0.16-0.20240329013200-4174c0acdb00/go.mod h1:dVG2n8fBAGpQczW8yk/6wuXb9uEhzaJF7wGXkGLRRCU=
370-
go.jetpack.io/pkg v0.0.0-20240327051701-89e2d24bc65e h1:JuyexYMVBsXwMrnmcCYVEOP9+Mrtvo0mAHSGeWdDUS0=
371-
go.jetpack.io/pkg v0.0.0-20240327051701-89e2d24bc65e/go.mod h1:vpIQT+m8iHO11v6bgMMG6iWfbGE2vxvLr9k7hLb4OeU=
372-
go.jetpack.io/pkg v0.0.0-20240329001056-e451f5c5e234 h1:MHZNJeQQwxqwVJhaCKtHAkCXrv3sWQkJoVf0i8Pf1Ro=
373-
go.jetpack.io/pkg v0.0.0-20240329001056-e451f5c5e234/go.mod h1:vpIQT+m8iHO11v6bgMMG6iWfbGE2vxvLr9k7hLb4OeU=
374-
go.jetpack.io/pkg v0.0.0-20240329204722-f4f14c8a894b h1:yAhOOZjimsf/hxY9d49xmVB+L5H5wkgoKXYqNLx8PFQ=
375-
go.jetpack.io/pkg v0.0.0-20240329204722-f4f14c8a894b/go.mod h1:vpIQT+m8iHO11v6bgMMG6iWfbGE2vxvLr9k7hLb4OeU=
376-
go.jetpack.io/pkg v0.0.0-20240329213144-bd03f1a1e491 h1:rD7aVnnpLHUnWKjiiMzWB1wKoJXQ/bsE/bZiei4KZ5Q=
377-
go.jetpack.io/pkg v0.0.0-20240329213144-bd03f1a1e491/go.mod h1:vpIQT+m8iHO11v6bgMMG6iWfbGE2vxvLr9k7hLb4OeU=
378-
go.jetpack.io/pkg v0.0.0-20240329230128-09e8a66df983 h1:tUWQOC0f12n8phuq7WGGtRVQ68F/DHPv+hWyR3bQUDA=
379-
go.jetpack.io/pkg v0.0.0-20240329230128-09e8a66df983/go.mod h1:gtmpVShXMEcZPBFZHswB3oCPYXobeR41b9CMybAjQYw=
368+
go.jetpack.io/pkg v0.0.0-20240404001923-7b42192bf9a5 h1:uFFlceGNlxqrKA/1umrBvNLTTBNkBU306Uqq1O27agM=
369+
go.jetpack.io/pkg v0.0.0-20240404001923-7b42192bf9a5/go.mod h1:gtmpVShXMEcZPBFZHswB3oCPYXobeR41b9CMybAjQYw=
380370
go.jetpack.io/typeid v1.0.0 h1:8gQ+iYGdyiQ0Pr40ydSB/PzMOIwlXX5DTojp1CBeSPQ=
381371
go.jetpack.io/typeid v1.0.0/go.mod h1:+UPEaECUgFxgAjFPn5Yf9eO/3ft/3xZ98Eahv9JW/GQ=
382372
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=

internal/devbox/providers/nixcache/nixcache.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"os"
7+
"time"
78

89
"github.com/aws/aws-sdk-go-v2/aws"
910
"github.com/aws/aws-sdk-go-v2/service/cognitoidentity/types"
@@ -12,7 +13,9 @@ import (
1213
"go.jetpack.io/devbox/internal/nix"
1314
"go.jetpack.io/devbox/internal/ux"
1415
"go.jetpack.io/pkg/api"
16+
nixv1alpha1 "go.jetpack.io/pkg/api/gen/priv/nix/v1alpha1"
1517
"go.jetpack.io/pkg/auth"
18+
"go.jetpack.io/pkg/filecache"
1619
)
1720

1821
type Provider struct{}
@@ -59,7 +62,17 @@ func (p *Provider) Config(ctx context.Context) (NixCacheConfig, error) {
5962
}
6063

6164
apiClient := api.NewClient(ctx, build.JetpackAPIHost(), token)
62-
binCacheResponse, err := apiClient.GetBinCache(ctx)
65+
cache := filecache.New[*nixv1alpha1.GetBinCacheResponse]("devbox/credentials")
66+
binCacheResponse, err := cache.GetOrSetWithTime(
67+
"aws-nix-bin-cache",
68+
func() (*nixv1alpha1.GetBinCacheResponse, time.Time, error) {
69+
r, err := apiClient.GetBinCache(ctx)
70+
if err != nil || r.GetNixBinCacheUri() == "" {
71+
return nil, time.Time{}, err
72+
}
73+
return r, r.GetNixBinCacheCredentials().Expiration.AsTime(), nil
74+
},
75+
)
6376
if err != nil {
6477
return NixCacheConfig{}, err
6578
}

internal/nix/search.go

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,14 @@ func SearchNixpkgsAttribute(query string) (map[string]*Info, error) {
136136
key := cacheKey(query)
137137

138138
// Check if the query was already cached, and return the result if so
139-
cache := filecache.New("devbox/nix", filecache.WithCacheDir(xdg.CacheSubpath("")))
140-
if cachedResults, err := cache.Get(key); err == nil {
141-
var results map[string]*Info
142-
if err := json.Unmarshal(cachedResults, &results); err != nil {
143-
return nil, err
144-
}
139+
cache := filecache.New(
140+
"devbox/nix",
141+
filecache.WithCacheDir[map[string]*Info](xdg.CacheSubpath("")),
142+
)
143+
144+
if results, err := cache.Get(key); err == nil {
145145
return results, nil
146-
} else if !filecacheIsCacheMiss(err) {
146+
} else if !filecache.IsCacheMiss(err) {
147147
return nil, err // genuine error
148148
}
149149

@@ -154,27 +154,17 @@ func SearchNixpkgsAttribute(query string) (map[string]*Info, error) {
154154
}
155155

156156
// Save the results to the cache
157-
marshalled, err := json.Marshal(infos)
158-
if err != nil {
159-
return nil, err
160-
}
161157
// TODO savil: add a SetForever API that does not expire. Time based expiration is not needed here
162158
// because we're caching results that are guaranteed to be stable.
163159
// TODO savil: Make filecache.cache a public struct so it can be passed into other functions
164160
const oneYear = 12 * 30 * 24 * time.Hour
165-
if err := cache.Set(key, marshalled, oneYear); err != nil {
161+
if err := cache.Set(key, infos, oneYear); err != nil {
166162
return nil, err
167163
}
168164

169165
return infos, nil
170166
}
171167

172-
// read as: filecache.IsCacheMiss(err)
173-
// TODO savil: this should be implemented in the filecache package
174-
func filecacheIsCacheMiss(err error) bool {
175-
return errors.Is(err, filecache.NotFound) || errors.Is(err, filecache.Expired)
176-
}
177-
178168
// cacheKey sanitizes the search query to be a valid unix filename.
179169
// This cache key is used as the filename to store the cache value, and having a
180170
// representation of the query is important for debuggability.

0 commit comments

Comments
 (0)