Skip to content

Commit cd883bf

Browse files
authored
internal/boxcli: add devbox cache credentials (#1960)
The `devbox cache credentials` subcommand prints out short-lived AWS STS credentials that grant access to the user's Nix cache. The output follows the format described in `aws help config-vars` under `Sourcing Credentials From External Processes`. This allows the AWS CLI/SDKs to obtain credentials from Devbox. For example: [default] credential_process = /usr/local/bin/devbox cache credentials Because Nix uses the AWS SDK's default credential chain, this allows it to automatically authenticate with private Devbox caches. Note: this can be improved by using the newer credentials-only API endpoint, which is faster.
1 parent 8e737d9 commit cd883bf

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

internal/boxcli/cache.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
package boxcli
55

66
import (
7+
"encoding/json"
8+
79
"github.com/MakeNowJust/heredoc/v2"
810
"github.com/pkg/errors"
911
"github.com/spf13/cobra"
1012
"go.jetpack.io/devbox/internal/devbox"
1113
"go.jetpack.io/devbox/internal/devbox/devopt"
14+
"go.jetpack.io/devbox/internal/devbox/providers/nixcache"
1215
)
1316

1417
type cacheFlags struct {
@@ -58,7 +61,41 @@ func cacheCmd() *cobra.Command {
5861
&flags.to, "to", "", "URI of the cache to copy to")
5962

6063
cacheCommand.AddCommand(uploadCommand)
64+
cacheCommand.AddCommand(cacheCredentialsCmd())
6165
cacheCommand.Hidden = true
6266

6367
return cacheCommand
6468
}
69+
70+
func cacheCredentialsCmd() *cobra.Command {
71+
return &cobra.Command{
72+
Use: "credentials",
73+
Short: "Output S3 cache credentials",
74+
Hidden: true,
75+
Args: cobra.ExactArgs(0),
76+
RunE: func(cmd *cobra.Command, args []string) error {
77+
cfg, err := nixcache.Get().Config(cmd.Context())
78+
if err != nil {
79+
return err
80+
}
81+
82+
creds := struct {
83+
Version int `json:"Version"`
84+
AccessKeyID string `json:"AccessKeyId"`
85+
SecretAccessKey string `json:"SecretAccessKey"`
86+
SessionToken string `json:"SessionToken"`
87+
}{
88+
Version: 1,
89+
AccessKeyID: *cfg.Credentials.AccessKeyId,
90+
SecretAccessKey: *cfg.Credentials.SecretKey,
91+
SessionToken: *cfg.Credentials.SessionToken,
92+
}
93+
out, err := json.Marshal(creds)
94+
if err != nil {
95+
return err
96+
}
97+
_, _ = cmd.OutOrStdout().Write(out)
98+
return nil
99+
},
100+
}
101+
}

0 commit comments

Comments
 (0)