Skip to content

Commit

Permalink
Add built-in root certificate fallbacks (#3440)
Browse files Browse the repository at this point in the history
* Add built-in root certificate fallbacks

Blank-importing golang.org/x/crypto/x509roots/fallback bundles a set of
root fallback certificates from Mozilla into the resulting binary. This
allows the program to run in environments where the system root
certificates are not available, for example, inside a minimal container.
These are _fallbacks_, meaning that if the system _does have_ a set of
root certificates, those will be given priority. The binary size will
increase a little (~ 220 kB).

It should be added to main.go instead of somewhere else because the
recommendation is for the package to be imported from binaries, not from
libraries. Calling x509.SetFallbackRoots (what the imported package does
in its init function) more than once will cause the program to panic. In
principle, the Go import system will prevent the package from being
imported twice, so that shouldn't be a problem. That said, xk6 has
opinions, and it doesn't want to have an import in main.go, so it's
added to the only package imported from main, "cmd".

---------

Signed-off-by: Marcelo E. Magallon <[email protected]>
Co-authored-by: Oleg Bespalov <[email protected]>
  • Loading branch information
mem and olegbespalov authored Dec 7, 2023
1 parent f67be4c commit f9ee99d
Show file tree
Hide file tree
Showing 8 changed files with 3,900 additions and 3 deletions.
4 changes: 1 addition & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -trimpath -o /usr/b
# Runtime stage
FROM alpine:3.18 as release

# hadolint ignore=DL3018
RUN apk add --no-cache ca-certificates && \
adduser -D -u 12345 -g 12345 k6
RUN adduser -D -u 12345 -g 12345 k6
COPY --from=builder /usr/bin/k6 /usr/bin/k6

USER k6
Expand Down
8 changes: 8 additions & 0 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ import (
"go.k6.io/k6/cmd/state"
"go.k6.io/k6/errext/exitcodes"
"go.k6.io/k6/lib/types"

// Blank-importing golang.org/x/crypto/x509roots/fallback bundles a set of
// root fallback certificates from Mozilla into the resulting binary. This
// allows the program to run in environments where the system root
// certificates are not available, for example inside a minimal container.
// These are _fallbacks_, meaning that if the system _does have_ a set of
// root certificates, those will be given priority.
_ "golang.org/x/crypto/x509roots/fallback"
)

// Panic if the given error is not nil.
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ require (
go.opentelemetry.io/otel/trace v1.19.0
go.uber.org/goleak v1.2.1
golang.org/x/crypto v0.14.0
golang.org/x/crypto/x509roots/fallback v0.0.0-20231030152948-74c2ba9521f1
golang.org/x/net v0.17.0
golang.org/x/term v0.13.0
golang.org/x/time v0.3.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/crypto/x509roots/fallback v0.0.0-20231030152948-74c2ba9521f1 h1:wQ75dCmVn5ExryuIUzbi2MC1/10fUNIL1FP918r4jx8=
golang.org/x/crypto/x509roots/fallback v0.0.0-20231030152948-74c2ba9521f1/go.mod h1:kNa9WdvYnzFwC79zRpLRMJbdEFlhyM5RPFBBZp/wWH8=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
Expand Down
27 changes: 27 additions & 0 deletions vendor/golang.org/x/crypto/x509roots/fallback/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3,827 changes: 3,827 additions & 0 deletions vendor/golang.org/x/crypto/x509roots/fallback/bundle.go

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions vendor/golang.org/x/crypto/x509roots/fallback/fallback.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,9 @@ go.uber.org/goleak/internal/stack
golang.org/x/crypto/md4
golang.org/x/crypto/ocsp
golang.org/x/crypto/ripemd160
# golang.org/x/crypto/x509roots/fallback v0.0.0-20231030152948-74c2ba9521f1
## explicit; go 1.20
golang.org/x/crypto/x509roots/fallback
# golang.org/x/net v0.17.0
## explicit; go 1.17
golang.org/x/net/html
Expand Down

0 comments on commit f9ee99d

Please sign in to comment.