Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions cmd/api/api/build_registry_credentials_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package api

import (
"bytes"
"context"
"encoding/json"
"mime/multipart"
"strings"
"testing"
"time"

"github.com/kernel/hypeman/lib/builds"
"github.com/kernel/hypeman/lib/builds/registryauth"
"github.com/kernel/hypeman/lib/oapi"
"github.com/stretchr/testify/require"
)

type registryCredentialBuildManager struct {
builds.Manager
request builds.CreateBuildRequest
}

func (m *registryCredentialBuildManager) CreateBuild(_ context.Context, req builds.CreateBuildRequest, _ []byte) (*builds.Build, error) {
m.request = req
if _, err := registryauth.Normalize(req.RegistryCredentials, "local.example:4973", time.Now()); err != nil {
return nil, err
}
return &builds.Build{ID: "test-build", Status: builds.StatusQueued}, nil
}

func TestCreateBuildRegistryCredentials(t *testing.T) {
for _, tc := range []struct {
name, data string
valid bool
}{
{"valid", `{"registry.example":{"username":"AWS","password":"synthetic-token"}}`, true},
{"omitted", "", true},
{"malformed", `{"password":"synthetic-token"`, false},
{"bad-host", `{"https://registry.example":{"username":"AWS","password":"synthetic-token"}}`, false},
{"local-registry", `{"local.example:4973":{"username":"AWS","password":"synthetic-token"}}`, false},
{"expired", `{"registry.example":{"username":"AWS","password":"synthetic-token","expires_at":"2000-01-01T00:00:00Z"}}`, false},
{"oversized", strings.Repeat("x", registryauth.MaxBytes+1), false},
} {
t.Run(tc.name, func(t *testing.T) {
manager := &registryCredentialBuildManager{}
svc := &ApiService{BuildManager: manager}
var body bytes.Buffer
writer := multipart.NewWriter(&body)
require.NoError(t, writer.WriteField("source", "source"))
if tc.data != "" {
require.NoError(t, writer.WriteField("registry_credentials", tc.data))
}
require.NoError(t, writer.Close())
response, err := svc.CreateBuild(context.Background(), oapi.CreateBuildRequestObject{Body: multipart.NewReader(&body, writer.Boundary())})
require.NoError(t, err)
if tc.valid {
_, ok := response.(oapi.CreateBuild202JSONResponse)
require.True(t, ok)
if tc.data != "" {
require.Equal(t, "synthetic-token", manager.request.RegistryCredentials["registry.example"].Password)
}
} else {
_, ok := response.(oapi.CreateBuild400JSONResponse)
require.True(t, ok)
}
encoded, err := json.Marshal(response)
require.NoError(t, err)
require.NotContains(t, string(encoded), "synthetic-token")
})
}
}
28 changes: 19 additions & 9 deletions cmd/api/api/builds.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/kernel/hypeman/lib/builders"
"github.com/kernel/hypeman/lib/builds"
"github.com/kernel/hypeman/lib/builds/registryauth"
"github.com/kernel/hypeman/lib/images"
"github.com/kernel/hypeman/lib/logger"
"github.com/kernel/hypeman/lib/oapi"
Expand Down Expand Up @@ -55,6 +56,7 @@ func (s *ApiService) CreateBuild(ctx context.Context, request oapi.CreateBuildRe
var isAdminBuild bool
var secrets []builds.SecretRef
var resourceTags map[string]string
var registryCredentials map[string]registryauth.Credential

for {
part, err := request.Body.NextPart()
Expand All @@ -69,6 +71,11 @@ func (s *ApiService) CreateBuild(ctx context.Context, request oapi.CreateBuildRe
}

switch part.FormName() {
case "registry_credentials":
data, err := io.ReadAll(io.LimitReader(part, registryauth.MaxBytes+1))
if err != nil || len(data) > registryauth.MaxBytes || json.Unmarshal(data, &registryCredentials) != nil {
return oapi.CreateBuild400JSONResponse{Code: "invalid_request", Message: registryauth.ErrInvalid.Error()}, nil
}
case "source":
sourceData, err = io.ReadAll(part)
if err != nil {
Expand Down Expand Up @@ -230,15 +237,16 @@ func (s *ApiService) CreateBuild(ctx context.Context, request oapi.CreateBuildRe

// Build domain request
domainReq := builds.CreateBuildRequest{
BaseImageDigest: baseImageDigest,
BuilderID: builderID,
CacheScope: cacheScope,
Dockerfile: dockerfile,
Secrets: secrets,
IsAdminBuild: isAdminBuild,
GlobalCacheKey: globalCacheKey,
ImageName: imageName,
Tags: resourceTags,
BaseImageDigest: baseImageDigest,
RegistryCredentials: registryCredentials,
BuilderID: builderID,
CacheScope: cacheScope,
Dockerfile: dockerfile,
Secrets: secrets,
IsAdminBuild: isAdminBuild,
GlobalCacheKey: globalCacheKey,
ImageName: imageName,
Tags: resourceTags,
}

// Apply build policy if any field was provided
Expand All @@ -259,6 +267,8 @@ func (s *ApiService) CreateBuild(ctx context.Context, request oapi.CreateBuildRe
build, err := s.BuildManager.CreateBuild(ctx, domainReq, sourceData)
if err != nil {
switch {
case errors.Is(err, registryauth.ErrInvalid):
return oapi.CreateBuild400JSONResponse{Code: "invalid_request", Message: registryauth.ErrInvalid.Error()}, nil
case errors.Is(err, tags.ErrInvalidTags):
return oapi.CreateBuild400JSONResponse{
Code: "invalid_request",
Expand Down
2 changes: 1 addition & 1 deletion integration/builder_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func builderIntegrationDockerContext(t *testing.T, repoRoot string) string {
for _, path := range []string{"go.mod", "go.sum", "lib/guest/guest.pb.go", "lib/guest/guest_grpc.pb.go"} {
copyBuilderIntegrationFile(t, repoRoot, contextDir, path)
}
for _, dir := range []string{"lib/builds/builder_agent", "lib/system/guest_agent"} {
for _, dir := range []string{"lib/builds/builder_agent", "lib/builds/registryauth", "lib/system/guest_agent"} {
err := filepath.WalkDir(filepath.Join(repoRoot, dir), func(path string, entry os.DirEntry, err error) error {
if err != nil {
return err
Expand Down
44 changes: 44 additions & 0 deletions lib/builds/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,50 @@ Builder VMs authenticate to the registry using short-lived JWT tokens:
3. **Token TTL**: Matches build timeout (minimum 30 minutes)
4. **Authentication**: Builder agent sends token via Basic auth (`token:` format)

### Borrowed upstream credentials

`POST /builds` accepts an optional `registry_credentials` multipart string:
a JSON object keyed by exact registry host, with `username`, `password`, and
optional RFC3339 `expires_at`. This supports private `FROM` images without
installing a shared upstream credential on the host.

```json
{"registry.example":{"username":"AWS","password":"<temporary-token>","expires_at":"2030-01-01T12:00:00Z"}}
```

Submit the JSON from a protected file rather than exposing the token in process arguments:

```sh
curl -H "Authorization: Bearer $HYPEMAN_TOKEN" \
-F "source=@source.tar.gz" \
-F "registry_credentials=<$REGISTRY_CREDENTIALS_FILE" \
"$HYPEMAN_URL/builds"
```

- Use pull-only credentials limited to the needed repositories. The registry/IAM
policy grants permissions; this field does not narrow the token itself.
- The API accepts at most 16 registry hosts and 64 KiB of JSON. Schemes, paths,
duplicate normalized hosts, local-output-registry overrides, CR/LF in
credentials, and expired credentials are rejected. Only username/password
authentication is supported for builds; do not pass `registry_token`.
- Hypeman keeps these credentials in memory, not metadata or the config disk.
They are sent only to that build's VM over vsock. The guest adds them beside
local-registry auth in its existing mode-0600 Docker config files, outside
the source context and persistent BuildKit cache, and deletes them afterward.
- Credentialed builds skip host base-image prefetching and the guest's Docker Hub
mirror, preventing private bases from entering the shared host mirror.
- Borrowing is bounded to **30 minutes from submission**, including queue time,
or the earliest caller expiry. Active builds are bounded by the same deadline.
Completion/cancellation/expiry releases host references. This does not revoke
the token at the upstream registry.
- There is no refresh or durable replay. After a restart, credentialed builds fail
and their old builder VM is deleted; resubmit with fresh credentials.
- Rebuild and deploy the generic builder image before enabling callers.
Older builders are rejected rather than accepting a credentialed build silently.
CLI/SDK caller wiring is a separate change; no new CLI flags exist yet.

Omitting the field (or sending `{}`) preserves existing behavior.

## Build Status Flow

```
Expand Down
Loading
Loading