Skip to content

Commit 6d08895

Browse files
committed
fix: add SBOM and S3 caching secrets
Upstream has moved various type to string conversions to a variety of different places in the code. Signed-off-by: Chris Goller <[email protected]>
1 parent b6118b1 commit 6d08895

File tree

4 files changed

+78
-5
lines changed

4 files changed

+78
-5
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
buf.build/gen/go/depot/api/protocolbuffers/go v1.32.0-20240221184445-e8316610338f.1
88
connectrpc.com/connect v1.15.0
99
github.com/adrg/xdg v0.4.0
10+
github.com/aws/aws-sdk-go-v2/config v1.15.5
1011
github.com/briandowns/spinner v1.18.1
1112
github.com/charmbracelet/bubbles v0.16.1
1213
github.com/charmbracelet/bubbletea v0.24.2
@@ -64,7 +65,6 @@ require (
6465
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
6566
github.com/atotto/clipboard v0.1.4 // indirect
6667
github.com/aws/aws-sdk-go-v2 v1.16.3 // indirect
67-
github.com/aws/aws-sdk-go-v2/config v1.15.5 // indirect
6868
github.com/aws/aws-sdk-go-v2/credentials v1.12.0 // indirect
6969
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.12.4 // indirect
7070
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.10 // indirect

pkg/buildx/bake/buildflags/cache.go

+71
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package buildflags
22

33
import (
4+
"context"
45
"encoding/csv"
56
"encoding/json"
67
"maps"
8+
"os"
9+
"strconv"
710
"strings"
811

12+
awsconfig "github.com/aws/aws-sdk-go-v2/config"
913
"github.com/moby/buildkit/client"
1014
"github.com/pkg/errors"
1115
"github.com/zclconf/go-cty/cty"
@@ -183,6 +187,9 @@ func CreateCaches(entries []*CacheOptionsEntry) []client.CacheOptionsEntry {
183187
return nil
184188
}
185189
for _, entry := range entries {
190+
addGithubToken(entry)
191+
addAwsCredentials(entry)
192+
186193
out := client.CacheOptionsEntry{
187194
Type: entry.Type,
188195
Attrs: map[string]string{},
@@ -194,3 +201,67 @@ func CreateCaches(entries []*CacheOptionsEntry) []client.CacheOptionsEntry {
194201
}
195202
return outs
196203
}
204+
205+
func addGithubToken(ci *CacheOptionsEntry) {
206+
if ci.Type != "gha" {
207+
return
208+
}
209+
version, ok := ci.Attrs["version"]
210+
if !ok {
211+
// https://github.com/actions/toolkit/blob/2b08dc18f261b9fdd978b70279b85cbef81af8bc/packages/cache/src/internal/config.ts#L19
212+
if v, ok := os.LookupEnv("ACTIONS_CACHE_SERVICE_V2"); ok {
213+
if b, err := strconv.ParseBool(v); err == nil && b {
214+
version = "2"
215+
}
216+
}
217+
}
218+
if _, ok := ci.Attrs["token"]; !ok {
219+
if v, ok := os.LookupEnv("ACTIONS_RUNTIME_TOKEN"); ok {
220+
ci.Attrs["token"] = v
221+
}
222+
}
223+
if _, ok := ci.Attrs["url_v2"]; !ok && version == "2" {
224+
// https://github.com/actions/toolkit/blob/2b08dc18f261b9fdd978b70279b85cbef81af8bc/packages/cache/src/internal/config.ts#L34-L35
225+
if v, ok := os.LookupEnv("ACTIONS_RESULTS_URL"); ok {
226+
ci.Attrs["url_v2"] = v
227+
}
228+
}
229+
if _, ok := ci.Attrs["url"]; !ok {
230+
// https://github.com/actions/toolkit/blob/2b08dc18f261b9fdd978b70279b85cbef81af8bc/packages/cache/src/internal/config.ts#L28-L33
231+
if v, ok := os.LookupEnv("ACTIONS_CACHE_URL"); ok {
232+
ci.Attrs["url"] = v
233+
} else if v, ok := os.LookupEnv("ACTIONS_RESULTS_URL"); ok {
234+
ci.Attrs["url"] = v
235+
}
236+
}
237+
}
238+
239+
func addAwsCredentials(ci *CacheOptionsEntry) {
240+
if ci.Type != "s3" {
241+
return
242+
}
243+
_, okAccessKeyID := ci.Attrs["access_key_id"]
244+
_, okSecretAccessKey := ci.Attrs["secret_access_key"]
245+
// If the user provides access_key_id, secret_access_key, do not override the session token.
246+
if okAccessKeyID && okSecretAccessKey {
247+
return
248+
}
249+
ctx := context.TODO()
250+
awsConfig, err := awsconfig.LoadDefaultConfig(ctx)
251+
if err != nil {
252+
return
253+
}
254+
credentials, err := awsConfig.Credentials.Retrieve(ctx)
255+
if err != nil {
256+
return
257+
}
258+
if !okAccessKeyID && credentials.AccessKeyID != "" {
259+
ci.Attrs["access_key_id"] = credentials.AccessKeyID
260+
}
261+
if !okSecretAccessKey && credentials.SecretAccessKey != "" {
262+
ci.Attrs["secret_access_key"] = credentials.SecretAccessKey
263+
}
264+
if _, ok := ci.Attrs["session_token"]; !ok && credentials.SessionToken != "" {
265+
ci.Attrs["session_token"] = credentials.SessionToken
266+
}
267+
}

pkg/buildx/build/build.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ func toSolveOpt(ctx context.Context, node builder.Node, multiDriver bool, opt Op
401401
supportsAttestations := true
402402
if len(attests) > 0 {
403403
for k, v := range attests {
404-
so.FrontendAttrs[k] = v
404+
so.FrontendAttrs["attest:"+k] = v
405405
}
406406
}
407407
if _, ok := opt.Attests["attest:provenance"]; !ok && supportsAttestations {

pkg/helpers/gha.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import "os"
77
func FixGitHubActionsCacheEnv() {
88
original := os.Getenv("UPSTREAM_ACTIONS_CACHE_URL")
99

10-
if original == "" {
11-
original = os.Getenv("GACTIONSCACHE_URL")
10+
if original != "" {
11+
os.Setenv("ACTIONS_CACHE_URL", original)
1212
}
1313

14+
original = os.Getenv("UPSTREAM_ACTIONS_RESULTS_URL")
15+
1416
if original != "" {
15-
os.Setenv("ACTIONS_CACHE_URL", original)
17+
os.Setenv("ACTIONS_RESULTS_URL", original)
1618
}
1719
}

0 commit comments

Comments
 (0)