Skip to content

Commit 9aac11a

Browse files
authored
[pat]Exchange pat for access token (#1947)
## Summary If user specifies a `DEVBOX_ACCESS_TOKEN` we use it to fetch a short lived JWT access token. ## How was it tested? generated a pat and used it `DEVBOX_ACCESS_TOKEN=xxx devbox add hello`
1 parent 4253632 commit 9aac11a

File tree

5 files changed

+50
-3
lines changed

5 files changed

+50
-3
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ 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-20240329001056-e451f5c5e234
43+
go.jetpack.io/pkg v0.0.0-20240329230128-09e8a66df983
4444
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225
4545
golang.org/x/mod v0.16.0
4646
golang.org/x/sync v0.6.0

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,12 @@ go.jetpack.io/pkg v0.0.0-20240327051701-89e2d24bc65e h1:JuyexYMVBsXwMrnmcCYVEOP9
371371
go.jetpack.io/pkg v0.0.0-20240327051701-89e2d24bc65e/go.mod h1:vpIQT+m8iHO11v6bgMMG6iWfbGE2vxvLr9k7hLb4OeU=
372372
go.jetpack.io/pkg v0.0.0-20240329001056-e451f5c5e234 h1:MHZNJeQQwxqwVJhaCKtHAkCXrv3sWQkJoVf0i8Pf1Ro=
373373
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=
374380
go.jetpack.io/typeid v1.0.0 h1:8gQ+iYGdyiQ0Pr40ydSB/PzMOIwlXX5DTojp1CBeSPQ=
375381
go.jetpack.io/typeid v1.0.0/go.mod h1:+UPEaECUgFxgAjFPn5Yf9eO/3ft/3xZ98Eahv9JW/GQ=
376382
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=

internal/devbox/cache.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ func (d *Devbox) UploadProjectToCache(
2525
return err
2626
}
2727

28-
return nix.CopyInstallableToCache(ctx, d.stderr, cacheConfig.URI, profilePath)
28+
return nix.CopyInstallableToCache(
29+
ctx,
30+
d.stderr, cacheConfig.URI, profilePath, cacheConfig.CredentialsEnvVars())
2931
}
3032

3133
func UploadInstallableToCache(
@@ -41,5 +43,7 @@ func UploadInstallableToCache(
4143
return err
4244
}
4345
}
44-
return nix.CopyInstallableToCache(ctx, stderr, cacheConfig.URI, installable)
46+
return nix.CopyInstallableToCache(
47+
ctx,
48+
stderr, cacheConfig.URI, installable, cacheConfig.CredentialsEnvVars())
4549
}

internal/devbox/providers/identity/identity.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@ package identity
22

33
import (
44
"context"
5+
"os"
56

67
"go.jetpack.io/devbox/internal/build"
8+
"go.jetpack.io/pkg/api"
79
"go.jetpack.io/pkg/auth"
810
"go.jetpack.io/pkg/auth/session"
11+
"go.jetpack.io/pkg/ids"
12+
"go.jetpack.io/typeid"
13+
"golang.org/x/oauth2"
914
)
1015

1116
var scopes = []string{"openid", "offline_access", "email", "profile"}
@@ -19,6 +24,10 @@ func Get() *Provider {
1924
}
2025

2126
func (p *Provider) GenSession(ctx context.Context) (*session.Token, error) {
27+
if t, err := p.getTokenFromPAT(ctx); err != nil || t != nil {
28+
return t, err
29+
}
30+
2231
c, err := p.AuthClient()
2332
if err != nil {
2433
return nil, err
@@ -35,3 +44,29 @@ func (p *Provider) AuthClient() (*auth.Client, error) {
3544
build.Audience(),
3645
)
3746
}
47+
48+
func (p *Provider) getTokenFromPAT(ctx context.Context) (*session.Token, error) {
49+
pat := os.Getenv("DEVBOX_ACCESS_TOKEN")
50+
if pat == "" {
51+
return nil, nil
52+
}
53+
54+
patID, err := typeid.Parse[ids.PersonalAccessToken](pat)
55+
if err != nil {
56+
return nil, err
57+
}
58+
59+
apiClient := api.NewClient(ctx, build.JetpackAPIHost(), &session.Token{})
60+
response, err := apiClient.GetAccessToken(ctx, patID)
61+
if err != nil {
62+
return nil, err
63+
}
64+
65+
// This is not the greatest. This token is missing id, refresh, etc.
66+
// It may be better to change api.NewClient() to take a token string instead.
67+
return &session.Token{
68+
Token: oauth2.Token{
69+
AccessToken: response.AccessToken,
70+
},
71+
}, nil
72+
}

internal/nix/cache.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ func CopyInstallableToCache(
1515
// paths into "path" flakes which is not what we want for /nix/store paths.
1616
// TODO: Add support for store paths in flake.Installable
1717
to, installable string,
18+
env []string,
1819
) error {
1920
fmt.Fprintf(out, "Copying %s to %s\n", installable, to)
2021
cmd := commandContext(
@@ -30,6 +31,7 @@ func CopyInstallableToCache(
3031
cmd.Stdin = os.Stdin
3132
cmd.Stdout = out
3233
cmd.Stderr = out
34+
cmd.Env = append(os.Environ(), env...)
3335

3436
return cmd.Run()
3537
}

0 commit comments

Comments
 (0)