Skip to content

Commit 95b3e3e

Browse files
committed
wip
1 parent bcfd7d4 commit 95b3e3e

8 files changed

Lines changed: 101 additions & 82 deletions

File tree

boot.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func main() {
4747
Name: "server",
4848
Aliases: []string{"srv"},
4949
Usage: "Run the git server",
50-
Action: func(ctx context.Context, cmd *cli.Command) error {
50+
Action: func(_ context.Context, _ *cli.Command) error {
5151
cnf := new(sshd.Config)
5252
if err := envconfig.Process(serverConfAppName, cnf); err != nil {
5353
return fmt.Errorf("getting config for %s [%s]", serverConfAppName, err)
@@ -106,7 +106,7 @@ func main() {
106106
Name: "git-receive",
107107
Aliases: []string{"gr"},
108108
Usage: "Run the git-receive hook",
109-
Action: func(ctx context.Context, cmd *cli.Command) error {
109+
Action: func(_ context.Context, _ *cli.Command) error {
110110
cnf := new(gitreceive.Config)
111111
if err := envconfig.Process(gitReceiveConfAppName, cnf); err != nil {
112112
return fmt.Errorf("error getting config for %s [%s]", gitReceiveConfAppName, err)

charts/builder/templates/_helpers.tpl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,32 @@ env:
2222
fieldPath: metadata.namespace
2323
- name: "DRYCC_CONTROLLER_URL"
2424
value: http://drycc-controller-api
25+
{{- if .Values.passport.enabled }}
26+
- name: "DRYCC_PASSPORT_URL"
27+
{{- if .Values.global.certManagerEnabled }}
28+
value: https://drycc-passport.{{ .Values.global.platformDomain }}
29+
{{- else }}
30+
value: http://drycc-passport.{{ .Values.global.platformDomain }}
31+
{{- end }}
32+
- name: DRYCC_PASSPORT_KEY
33+
valueFrom:
34+
secretKeyRef:
35+
name: passport-creds
36+
key: drycc-passport-builder-key
37+
- name: DRYCC_PASSPORT_SECRET
38+
valueFrom:
39+
secretKeyRef:
40+
name: passport-creds
41+
key: drycc-passport-builder-secret
42+
{{- else }}
43+
- name: DRYCC_PASSPORT_URL
44+
valueFrom:
45+
secretKeyRef:
46+
name: builder-secret
47+
key: passport-url
48+
- name: DRYCC_PASSPORT_KEY
49+
valueFrom:
50+
secretKeyRef:
2551
{{- if (.Values.storageEndpoint) }}
2652
- name: "DRYCC_STORAGE_BUCKET"
2753
valueFrom:

charts/builder/templates/builder-secret.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ metadata:
77
type: Opaque
88
data:
99
{{- if (.Values.registryHost) }}
10+
{{- if (.Values.passportUrl) }}
11+
passport-url: {{ .Values.passportUrl | b64enc }}
12+
{{- end }}
13+
{{- if (.Values.passportKey) }}
14+
passport-key: {{ .Values.passportKey | b64enc }}
15+
{{- end }}
16+
{{- if (.Values.passportSecret) }}
17+
passport-secret: {{ .Values.passportSecret | b64enc }}
18+
{{- end }}
1019
registry-host: {{ .Values.registryHost | b64enc }}
1120
registry-username: {{ .Values.registryUsername | b64enc }}
1221
registry-password: {{ .Values.registryPassword | b64enc }}

pkg/conf/config.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
package conf
33

44
import (
5-
"fmt"
65
"net"
76
"net/url"
8-
"os"
97
"strings"
108

119
"github.com/drycc/builder/pkg/sys"
@@ -19,22 +17,9 @@ const (
1917
storagePathStyleEnvVar = "DRYCC_STORAGE_PATH_STYLE"
2018
)
2119

22-
// ServiceKeyLocation holds the path of the service key secret.
23-
var ServiceKeyLocation = "/var/run/secrets/drycc/controller/service-key"
24-
2520
// Parameters is map which contains storage params
2621
type Parameters map[string]any
2722

28-
// GetServiceKey returns the key to be used as token to interact with drycc-controller
29-
func GetServiceKey() (string, error) {
30-
serviceKeyBytes, err := os.ReadFile(ServiceKeyLocation)
31-
if err != nil {
32-
return "", fmt.Errorf("couldn't get builder key from %s (%s)", ServiceKeyLocation, err)
33-
}
34-
serviceKey := strings.TrimSuffix(string(serviceKeyBytes), "\n")
35-
return serviceKey, nil
36-
}
37-
3823
// GetStorageParams returns the credentials required for connecting to object storage
3924
func GetStorageParams(env sys.Env) (Parameters, error) {
4025
params := make(map[string]any)

pkg/conf/config_test.go

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package conf
22

33
import (
4-
"os"
5-
"path/filepath"
64
"testing"
75

86
"github.com/drycc/builder/pkg/sys"
@@ -29,31 +27,3 @@ func TestGetStorageParams(t *testing.T) {
2927
assert.Equal(t, params["accesskey"], "admin", "accesskey")
3028
assert.Equal(t, params["secretkey"], "adminpass", "secretkey")
3129
}
32-
33-
func TestGetControllerClient(t *testing.T) {
34-
tmpDir, err := os.MkdirTemp("", "tmpdir")
35-
if err != nil {
36-
t.Fatalf("error creating temp directory (%s)", err)
37-
}
38-
39-
defer func() {
40-
if err := os.RemoveAll(tmpDir); err != nil {
41-
t.Fatalf("failed to remove service-key from %s (%s)", tmpDir, err)
42-
}
43-
}()
44-
45-
ServiceKeyLocation = filepath.Join(tmpDir, "service-key")
46-
data := []byte("testbuilderkey")
47-
if err := os.WriteFile(ServiceKeyLocation, data, 0o644); err != nil {
48-
t.Fatalf("error creating %s (%s)", ServiceKeyLocation, err)
49-
}
50-
51-
key, err := GetServiceKey()
52-
assert.Equal(t, err, nil)
53-
assert.Equal(t, key, string(data), "data")
54-
}
55-
56-
func TestGetServiceKeyError(t *testing.T) {
57-
_, err := GetServiceKey()
58-
assert.True(t, err != nil, "no error received when there should have been")
59-
}

pkg/controller/utils.go

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,64 @@
22
package controller
33

44
import (
5-
"github.com/drycc/builder/pkg/conf"
5+
"encoding/json"
6+
"fmt"
7+
"net/http"
8+
"net/url"
9+
"os"
10+
"strings"
11+
612
drycc "github.com/drycc/controller-sdk-go"
713
"github.com/drycc/pkg/log"
814
)
915

16+
type tokenResponse struct {
17+
AccessToken string `json:"access_token"`
18+
TokenType string `json:"token_type"`
19+
}
20+
1021
// New creates a new SDK client configured as the builder.
11-
func New(url string) (*drycc.Client, error) {
12-
client, err := drycc.New(true, url, "")
22+
func New(controllerURL string) (*drycc.Client, error) {
23+
client, err := drycc.New(true, controllerURL, "")
1324
if err != nil {
1425
return client, err
1526
}
1627
client.UserAgent = "drycc-builder"
1728

18-
serviceKey, err := conf.GetServiceKey()
29+
passportURL := os.Getenv("DRYCC_PASSPORT_URL")
30+
passportKey := os.Getenv("DRYCC_PASSPORT_KEY")
31+
passportSecret := os.Getenv("DRYCC_PASSPORT_SECRET")
32+
if passportURL == "" || passportKey == "" || passportSecret == "" {
33+
return client, fmt.Errorf("passport credentials not configured")
34+
}
35+
36+
data := url.Values{}
37+
data.Set("grant_type", "client_credentials")
38+
data.Set("client_id", passportKey)
39+
data.Set("client_secret", passportSecret)
40+
41+
req, err := http.NewRequest("POST", fmt.Sprintf("%s/oauth/token/", passportURL), strings.NewReader(data.Encode()))
1942
if err != nil {
20-
return client, err
43+
return client, fmt.Errorf("failed to create token request: %v", err)
2144
}
22-
client.ServiceKey = serviceKey
45+
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
46+
47+
resp, err := http.DefaultClient.Do(req)
48+
if err != nil {
49+
return client, fmt.Errorf("failed to request token: %v", err)
50+
}
51+
defer resp.Body.Close()
52+
53+
if resp.StatusCode != http.StatusOK {
54+
return client, fmt.Errorf("failed to get token: HTTP %d", resp.StatusCode)
55+
}
56+
57+
var tr tokenResponse
58+
if err := json.NewDecoder(resp.Body).Decode(&tr); err != nil {
59+
return client, fmt.Errorf("failed to decode token response: %v", err)
60+
}
61+
62+
client.Token = tr.TokenType + " " + tr.AccessToken
2363

2464
return client, nil
2565
}

pkg/controller/utils_test.go

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,34 @@ package controller
22

33
import (
44
"errors"
5+
"net/http"
6+
"net/http/httptest"
57
"os"
6-
"path/filepath"
78
"testing"
89

9-
builderconf "github.com/drycc/builder/pkg/conf"
1010
drycc "github.com/drycc/controller-sdk-go"
1111
"github.com/stretchr/testify/assert"
1212
)
1313

1414
func TestNew(t *testing.T) {
15-
tmpDir, err := os.MkdirTemp("", "tmpdir")
16-
if err != nil {
17-
t.Fatalf("error creating temp directory (%s)", err)
18-
}
19-
20-
defer func() {
21-
if err := os.RemoveAll(tmpDir); err != nil {
22-
t.Fatalf("failed to remove service-key from %s (%s)", tmpDir, err)
23-
}
24-
}()
25-
26-
builderconf.ServiceKeyLocation = filepath.Join(tmpDir, "service-key")
27-
data := []byte("testbuilderkey")
28-
if err := os.WriteFile(builderconf.ServiceKeyLocation, data, 0o644); err != nil {
29-
t.Fatalf("error creating %s (%s)", builderconf.ServiceKeyLocation, err)
30-
}
15+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
16+
w.Header().Set("Content-Type", "application/json")
17+
w.Write([]byte(`{"access_token": "testing_token", "token_type": "Bearer"}`))
18+
}))
19+
defer ts.Close()
20+
21+
os.Setenv("DRYCC_PASSPORT_URL", ts.URL)
22+
os.Setenv("DRYCC_PASSPORT_KEY", "testing_key")
23+
os.Setenv("DRYCC_PASSPORT_SECRET", "testing_secret")
24+
defer os.Unsetenv("DRYCC_PASSPORT_URL")
25+
defer os.Unsetenv("DRYCC_PASSPORT_KEY")
26+
defer os.Unsetenv("DRYCC_PASSPORT_SECRET")
3127

3228
url := "http://127.0.0.1:80"
3329
cli, err := New(url)
3430
assert.Equal(t, err, nil)
3531
assert.Equal(t, cli.ControllerURL.String(), url, "data")
36-
assert.Equal(t, cli.ServiceKey, string(data), "data")
32+
assert.Equal(t, cli.Token, "Bearer testing_token", "data")
3733
assert.Equal(t, cli.UserAgent, "drycc-builder", "user-agent")
3834

3935
url = "http://127.0.0.1:invalid-port-number"
@@ -42,7 +38,8 @@ func TestNew(t *testing.T) {
4238
}
4339
}
4440

45-
func TestNewWithInvalidBuilderKeyPath(t *testing.T) {
41+
func TestNewWithInvalidCredentials(t *testing.T) {
42+
os.Unsetenv("DRYCC_PASSPORT_URL")
4643
url := "http://127.0.0.1:80"
4744
_, err := New(url)
4845
assert.True(t, err != nil, "no error received when there should have been")

pkg/gitreceive/build_test.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ import (
55
"context"
66
"os"
77
"os/exec"
8-
"path/filepath"
98
"testing"
109

1110
"github.com/distribution/distribution/v3/registry/storage/driver/factory"
1211
_ "github.com/distribution/distribution/v3/registry/storage/driver/inmemory"
13-
builderconf "github.com/drycc/builder/pkg/conf"
1412
"github.com/drycc/builder/pkg/sys"
1513
"github.com/drycc/controller-sdk-go/api"
1614
"github.com/drycc/pkg/log"
@@ -73,13 +71,7 @@ func TestBuild(t *testing.T) {
7371
config.ControllerURL = "http://localhost:1234"
7472

7573
if err := build(config, storageDriver, nil, env, sha); err == nil {
76-
t.Error("expected running build() without a valid builder key to fail")
77-
}
78-
79-
builderconf.ServiceKeyLocation = filepath.Join(tmpDir, "service-key")
80-
data := []byte("testbuilderkey")
81-
if err := os.WriteFile(builderconf.ServiceKeyLocation, data, 0o644); err != nil {
82-
t.Fatalf("error creating %s (%s)", builderconf.ServiceKeyLocation, err)
74+
t.Error("expected running build() without valid credentials to fail")
8375
}
8476

8577
if err := build(config, storageDriver, nil, env, sha); err == nil {

0 commit comments

Comments
 (0)