Skip to content

Commit 08cbf46

Browse files
committed
wip
Signed-off-by: Chris Goller <[email protected]>
1 parent 9022292 commit 08cbf46

File tree

2 files changed

+35
-73
lines changed

2 files changed

+35
-73
lines changed

examples/custom-auth/Dockerfile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1-
FROM alpine:latest
2-
RUN echo howdy
1+
FROM golang:1.24
2+
COPY go.mod go.sum ./
3+
RUN go mod download
4+
COPY *.go ./
5+
RUN CGO_ENABLED=0 GOOS=linux go build -o /howdy
6+
RUN /howdy

examples/custom-auth/main.go

Lines changed: 29 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,15 @@ import (
55
"encoding/json"
66
"log"
77
"os"
8-
"path/filepath"
98
"time"
109

1110
"github.com/depot/depot-go/build"
1211
"github.com/depot/depot-go/machine"
1312
cliv1 "github.com/depot/depot-go/proto/depot/cli/v1"
1413
"github.com/moby/buildkit/client"
14+
"github.com/moby/buildkit/client/llb"
1515
"github.com/moby/buildkit/session"
16-
"github.com/moby/buildkit/session/auth"
17-
"google.golang.org/grpc"
18-
"google.golang.org/grpc/codes"
19-
"google.golang.org/grpc/status"
16+
"github.com/moby/buildkit/session/upload/uploadprovider"
2017
)
2118

2219
func main() {
@@ -27,10 +24,17 @@ func main() {
2724
token := os.Getenv("DEPOT_TOKEN")
2825
project := os.Getenv("DEPOT_PROJECT_ID")
2926

30-
// ... and set these variables.
31-
dockerfilePath := "./Dockerfile"
32-
workingDir := "."
33-
imageTag := "AWS_ACCOUNT_ID_HERE.dkr.ecr.us-east-1.amazonaws.com/REPO_HERE:TAG_HERE"
27+
/*
28+
*
29+
* howdy.tar.gz is a compressed tar archive that contains the Dockerfile and
30+
* any other files needed to build the image.
31+
*
32+
*/
33+
r, err := os.Open("howdy.tar.gz")
34+
if err != nil {
35+
log.Printf("unable to open file: %v", err)
36+
return
37+
}
3438

3539
// 1. Register a new build. This returns back an id and a temporary build token.
3640
req := &cliv1.CreateBuildRequest{
@@ -62,28 +66,27 @@ func main() {
6266
return
6367
}
6468

69+
uploader := uploadprovider.New()
70+
// Special buildkit URL for HTTP over gRPC over gRPC.
71+
contextURL := uploader.Add(r)
72+
73+
echo := llb.Scratch().File(llb.Copy(llb.Local("."), "/", "/"))
74+
75+
// TODO: right context?
76+
def, err := echo.Marshal(connectCtx)
77+
if err != nil {
78+
log.Printf("unable to marshal LLB definition: %v", err)
79+
return
80+
}
81+
6582
solverOptions := client.SolveOpt{
6683
Frontend: "dockerfile.v0", // Interpret the build as a Dockerfile.
6784
FrontendAttrs: map[string]string{
68-
"filename": filepath.Base(dockerfilePath),
6985
"platform": "linux/arm64", // Build for arm64 architecture.
70-
},
71-
LocalDirs: map[string]string{
72-
"dockerfile": filepath.Dir(dockerfilePath),
73-
"context": workingDir,
74-
},
75-
Exports: []client.ExportEntry{
76-
{
77-
Type: "image",
78-
Attrs: map[string]string{
79-
"oci-mediatypes": "true",
80-
"push": "true", // Push the image to the registry...
81-
"name": imageTag, // ... with this tag.
82-
},
83-
},
86+
"context": contextURL,
8487
},
8588
Session: []session.Attachable{
86-
&EnvAuth{},
89+
uploader,
8790
},
8891
}
8992

@@ -98,53 +101,8 @@ func main() {
98101
}()
99102

100103
// 4. Build and push the image.
101-
_, buildErr = buildkitClient.Solve(ctx, nil, solverOptions, buildStatusCh)
104+
_, buildErr = buildkitClient.Solve(ctx, def, solverOptions, buildStatusCh)
102105
if buildErr != nil {
103106
return
104107
}
105108
}
106-
107-
// EnvAuth is a custom auth provider that uses environment variables to provide registry credentials.
108-
// Uses REGISTRY_USERNAME and REGISTRY_TOKEN environment variables.
109-
type EnvAuth struct{}
110-
111-
// In BuildKit an Attachable is a client-side gRPC server that the build server can connect to.
112-
// BuildKit tunnels gRPC over gRPC, so the client-side can be dialed by the server-side.
113-
var _ session.Attachable = (*EnvAuth)(nil)
114-
115-
// Register hosts an AuthServer on the client-side for the build server.
116-
func (ap *EnvAuth) Register(server *grpc.Server) {
117-
auth.RegisterAuthServer(server, ap)
118-
}
119-
120-
// AuthServer is not documented in BuildKit, but these are functions called by the build server.
121-
var _ auth.AuthServer = (*EnvAuth)(nil)
122-
123-
// For AWS ECR username is `AWS` and for password run `aws ecr get-login-password --region YOUR_REGION`.
124-
func (ap *EnvAuth) Credentials(ctx context.Context, req *auth.CredentialsRequest) (*auth.CredentialsResponse, error) {
125-
// If base image is at docker return empty creds to use public download.
126-
if req.Host == "registry-1.docker.io" {
127-
return &auth.CredentialsResponse{}, nil
128-
}
129-
130-
username := os.Getenv("REGISTRY_USERNAME")
131-
registryPassword := os.Getenv("REGISTRY_PASSWORD")
132-
133-
return &auth.CredentialsResponse{
134-
Username: username,
135-
Secret: registryPassword,
136-
}, nil
137-
}
138-
139-
// GetTokenAuthority needs to return an Unimplemented or a nil public key in order for the Credentials function to be called.
140-
func (ap *EnvAuth) GetTokenAuthority(ctx context.Context, req *auth.GetTokenAuthorityRequest) (*auth.GetTokenAuthorityResponse, error) {
141-
return nil, status.Errorf(codes.Unimplemented, "method Info not implemented")
142-
}
143-
144-
func (ap *EnvAuth) VerifyTokenAuthority(ctx context.Context, req *auth.VerifyTokenAuthorityRequest) (*auth.VerifyTokenAuthorityResponse, error) {
145-
return nil, status.Errorf(codes.Unimplemented, "method Info not implemented")
146-
}
147-
148-
func (ap *EnvAuth) FetchToken(ctx context.Context, req *auth.FetchTokenRequest) (rr *auth.FetchTokenResponse, err error) {
149-
return nil, status.Errorf(codes.Unimplemented, "method Info not implemented")
150-
}

0 commit comments

Comments
 (0)