Skip to content

Commit 6f671a0

Browse files
Merge pull request #38 from depot/tls-auth
Allow using TLS certificates for auth
2 parents 1c23c25 + d531593 commit 6f671a0

File tree

8 files changed

+213
-141
lines changed

8 files changed

+213
-141
lines changed

pkg/api/api.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ type BuilderResponse struct {
4545
BuilderState string `json:"builderState"`
4646
PollSeconds int `json:"pollSeconds"`
4747
Platform string `json:"platform"`
48+
49+
// Version 2 uses mTLS for authentication
50+
Version string `json:"version"`
51+
CACert string `json:"caCert"`
52+
Cert string `json:"cert"`
53+
Key string `json:"key"`
4854
}
4955

5056
func (d *Depot) GetBuilder(buildID string, platform string) (*BuilderResponse, error) {
@@ -56,6 +62,19 @@ func (d *Depot) GetBuilder(buildID string, platform string) (*BuilderResponse, e
5662
)
5763
}
5864

65+
type BuilderHealthResponse struct {
66+
OK bool `json:"ok"`
67+
}
68+
69+
func (d *Depot) ReportBuilderHealth(buildID string, platform string, status string) (*BuilderHealthResponse, error) {
70+
return apiRequest[BuilderHealthResponse](
71+
"POST",
72+
fmt.Sprintf("%s/api/internal/cli/builds/%s/platform/%s/health", d.BaseURL, buildID, platform),
73+
d.token,
74+
map[string]string{"status": status},
75+
)
76+
}
77+
5978
type FinishResponse struct {
6079
OK bool `json:"ok"`
6180
}

pkg/api/context.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package api
22

33
import "context"
44

5-
const depotClientKey = "depot.client"
5+
type depotClientKey struct{}
66

77
func WithClient(ctx context.Context, client *Depot) context.Context {
8-
return context.WithValue(ctx, depotClientKey, client)
8+
return context.WithValue(ctx, depotClientKey{}, client)
99
}
1010

1111
func GetContextClient(ctx context.Context) *Depot {
12-
return ctx.Value(depotClientKey).(*Depot)
12+
return ctx.Value(depotClientKey{}).(*Depot)
1313
}

pkg/api/request.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"encoding/json"
66
"fmt"
77
"io"
8-
"io/ioutil"
98
"net/http"
109
"runtime"
1110

@@ -65,7 +64,7 @@ func apiRequest[Response interface{}](method, url, token string, payload interfa
6564
fmt.Println(warnStyle.Render(warnMessage))
6665
}
6766

68-
body, err := ioutil.ReadAll(resp.Body)
67+
body, err := io.ReadAll(resp.Body)
6968
if err != nil {
7069
return nil, err
7170
}

pkg/builder/builder.go

Lines changed: 36 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
package builder
22

33
import (
4-
"context"
54
"fmt"
6-
"net"
75
"time"
86

97
"github.com/depot/cli/pkg/api"
108
"github.com/docker/buildx/util/progress"
11-
"github.com/moby/buildkit/client"
129
"github.com/pkg/errors"
1310
)
1411

@@ -28,11 +25,19 @@ func NewBuilder(depot *api.Depot, buildID, platform string) *Builder {
2825
}
2926
}
3027

31-
func (b *Builder) Acquire(l progress.Logger) (string, error) {
32-
var addr string
28+
type AcquiredBuilder struct {
29+
Version string
30+
Addr string
31+
AccessToken string
32+
CACert string
33+
Cert string
34+
Key string
35+
}
36+
37+
func (b *Builder) Acquire(l progress.Logger) (*AcquiredBuilder, error) {
3338
var resp *api.BuilderResponse
3439
var err error
35-
var accessToken string
40+
var builder AcquiredBuilder
3641

3742
acquireFn := func(sub progress.SubLogger) error {
3843
resp, err = b.depot.GetBuilder(b.BuildID, b.Platform)
@@ -41,7 +46,11 @@ func (b *Builder) Acquire(l progress.Logger) (string, error) {
4146
}
4247

4348
if resp.OK {
44-
accessToken = resp.AccessToken
49+
builder.Version = resp.Version
50+
builder.AccessToken = resp.AccessToken
51+
builder.CACert = resp.CACert
52+
builder.Cert = resp.Cert
53+
builder.Key = resp.Key
4554
}
4655

4756
// Loop if the builder is not ready
@@ -79,64 +88,31 @@ func (b *Builder) Acquire(l progress.Logger) (string, error) {
7988
if err != nil {
8089
err = progress.Wrap("[depot] launching "+b.Platform+" builder", l, acquireFn)
8190
if err != nil {
82-
return "", err
91+
return nil, err
8392
}
8493
}
8594

86-
err = progress.Wrap("[depot] connecting to "+b.Platform+" builder", l, func(sub progress.SubLogger) error {
87-
proxy, err := newProxyServer(resp.Endpoint, accessToken)
88-
if err != nil {
89-
return errors.Wrap(err, "failed to construct proxy server")
90-
}
91-
92-
b.proxy = proxy
93-
proxy.Start()
94-
addr = proxy.Addr().String()
95-
96-
sub.Log(2, []byte("Waiting for builder to report ready...\n"))
97-
98-
count := 0
99-
100-
for {
101-
if count > 30 {
102-
return fmt.Errorf("timed out waiting for builder to be ready")
103-
}
104-
105-
if count > 0 && count%10 == 0 {
106-
sub.Log(2, []byte("Still waiting for builder to report ready...\n"))
107-
}
108-
109-
if count > 0 {
110-
time.Sleep(time.Second)
111-
}
112-
113-
count++
95+
if builder.Version == "2" {
96+
builder.Addr = resp.Endpoint
97+
return &builder, nil
98+
}
11499

115-
conn, err := net.Dial("tcp", proxy.Addr().String())
116-
if err != nil {
117-
continue
118-
}
100+
proxy, err := newProxyServer(resp.Endpoint, builder.AccessToken)
101+
if err != nil {
102+
return nil, errors.Wrap(err, "failed to construct proxy server")
103+
}
119104

120-
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
121-
defer cancel()
122-
testClient, err := client.New(ctx, "", client.WithContextDialer(func(context.Context, string) (net.Conn, error) {
123-
return conn, nil
124-
}))
125-
if err != nil {
126-
continue
127-
}
105+
b.proxy = proxy
106+
proxy.Start()
107+
builder.Addr = fmt.Sprintf("tcp://%s", proxy.Addr().String())
128108

129-
ctx2, cancel2 := context.WithTimeout(context.Background(), 5*time.Second)
130-
defer cancel2()
131-
workers, err := testClient.ListWorkers(ctx2)
132-
if err != nil {
133-
continue
134-
}
109+
return &builder, err
110+
}
135111

136-
if len(workers) > 0 {
137-
return nil
138-
}
139-
}
140-
})
141-
return addr, err
112+
func (b *Builder) ReportHealth(status string) error {
113+
_, err := b.depot.ReportBuilderHealth(b.BuildID, b.Platform, status)
114+
if err != nil {
115+
return err
116+
}
117+
return nil
142118
}

pkg/builder/context.go

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)