Skip to content

Commit

Permalink
feat(initrd): Propagate KraftKit's auths to BuildKit
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Jung <[email protected]>
  • Loading branch information
nderjung committed Aug 5, 2024
1 parent 76343ff commit e4b302a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ require (
golang.org/x/sync v0.7.0
golang.org/x/sys v0.22.0
golang.org/x/term v0.22.0
google.golang.org/grpc v1.62.2
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
k8s.io/api v0.30.3
Expand Down Expand Up @@ -273,7 +274,6 @@ require (
google.golang.org/genproto v0.0.0-20240311173647-c811ad7063a7 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240318140521-94a12d6c2237 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 // indirect
google.golang.org/grpc v1.62.2 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/go-jose/go-jose.v2 v2.6.3 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
Expand Down
41 changes: 41 additions & 0 deletions initrd/dockerfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import (
"strings"

"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"kraftkit.sh/config"
"kraftkit.sh/cpio"
"kraftkit.sh/log"
Expand All @@ -23,6 +26,8 @@ import (
soci "github.com/anchore/stereoscope/pkg/image/oci"
"github.com/moby/buildkit/client"
"github.com/moby/buildkit/identity"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/session/auth"
"github.com/moby/buildkit/session/filesync"
"github.com/moby/buildkit/util/progress/progressui"
"github.com/testcontainers/testcontainers-go"
Expand Down Expand Up @@ -279,6 +284,11 @@ func (initrd *dockerfile) Build(ctx context.Context) (string, error) {

solveOpt := &client.SolveOpt{
Ref: identity.NewID(),
Session: []session.Attachable{
&buildkitAuthProvider{
config.G[config.KraftKit](ctx).Auth,
},
},
Exports: []client.ExportEntry{
{
Type: client.ExporterTar,
Expand Down Expand Up @@ -522,3 +532,34 @@ func (initrd *dockerfile) Env() []string {
func (initrd *dockerfile) Args() []string {
return initrd.args
}

type buildkitAuthProvider struct {
auths map[string]config.AuthConfig
}

func (ap *buildkitAuthProvider) Register(server *grpc.Server) {
auth.RegisterAuthServer(server, ap)
}

func (ap *buildkitAuthProvider) Credentials(ctx context.Context, req *auth.CredentialsRequest) (*auth.CredentialsResponse, error) {
res := &auth.CredentialsResponse{}

if a, ok := ap.auths[req.Host]; ok {
res.Username = a.User
res.Secret = a.Token
}

return res, nil
}

func (ap *buildkitAuthProvider) FetchToken(ctx context.Context, req *auth.FetchTokenRequest) (*auth.FetchTokenResponse, error) {
return nil, status.Errorf(codes.Unavailable, "client side tokens disabled")
}

func (ap *buildkitAuthProvider) GetTokenAuthority(ctx context.Context, req *auth.GetTokenAuthorityRequest) (*auth.GetTokenAuthorityResponse, error) {
return nil, status.Errorf(codes.Unavailable, "client side tokens disabled")
}

func (ap *buildkitAuthProvider) VerifyTokenAuthority(ctx context.Context, req *auth.VerifyTokenAuthorityRequest) (*auth.VerifyTokenAuthorityResponse, error) {
return nil, status.Errorf(codes.Unavailable, "client side tokens disabled")
}

0 comments on commit e4b302a

Please sign in to comment.