Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
feat: allow ECR registries to use the AWS default credential chain (#4357)
Guillaume Dfc18c60bdd80
diff --git a/backend/internal/registry/helpers.go b/backend/internal/registry/helpers.gonew file mode 100644index 000000000..9fa279e83--- /dev/null+++ b/backend/internal/registry/helpers.go@@ -0,0 +1,24 @@+package registry++import (+ "errors"+ "regexp"+ "strings"++ "github.com/getarcaneapp/arcane/types/v2/base"++ "github.com/getarcaneapp/arcane/backend/v2/internal/common"+)++var ecrRegistryEndpointPattern = regexp.MustCompile(`^(https://)?[0-9]{12}\.(dkr\.ecr(-fips)?\.[a-z0-9-]+\.amazonaws\.com(\.cn)?|dkr-ecr(-fips)?\.[a-z0-9-]+\.on\.aws)(:443)?/?$`)++func validateECRRegistryEndpoint(registryURL string, insecure bool) error {+ // Match the whole address so credentials cannot reach a user-controlled host.+ if !ecrRegistryEndpointPattern.MatchString(strings.TrimSpace(registryURL)) {+ return common.Classify(common.ErrValidation, &base.FieldError{Field: "url", Err: errors.New("ECR registry URL must be an AWS private ECR endpoint using HTTPS")})+ }+ if insecure {+ return common.Classify(common.ErrValidation, &base.FieldError{Field: "insecure", Err: errors.New("ECR registries require TLS verification")})+ }+ return nil+}diff --git a/backend/internal/registry/service.go b/backend/internal/registry/service.goindex 608720e39..e38f6100a 100644--- a/backend/internal/registry/service.go+++ b/backend/internal/registry/service.go@@ -234,21 +234,30 @@ func (s *ContainerRegistryService) CreateRegistry(ctx context.Context, req conta}if registryType == RegistryTypeECR {- if strings.TrimSpace(req.AWSAccessKeyID) == "" {- return nil, common.Classify(common.ErrValidation, &base.FieldError{Field: "awsAccessKeyId", Err: errors.New("AWS Access Key ID is required")})+ if endpointErr := validateECRRegistryEndpoint(registryRecord.URL, registryRecord.Insecure); endpointErr != nil {+ return nil, endpointErr}if strings.TrimSpace(req.AWSRegion) == "" {return nil, common.Classify(common.ErrValidation, &base.FieldError{Field: "awsRegion", Err: errors.New("AWS Region is required")})}- if strings.TrimSpace(req.AWSSecretAccessKey) == "" {+ // Static keys are optional, but must be supplied as a pair. With neither,+ // the default AWS credential chain (e.g. an EC2 instance profile) is used.+ hasKeyID := strings.TrimSpace(req.AWSAccessKeyID) != ""+ hasSecret := strings.TrimSpace(req.AWSSecretAccessKey) != ""+ if hasSecret && !hasKeyID {+ return nil, common.Classify(common.ErrValidation, &base.FieldError{Field: "awsAccessKeyId", Err: errors.New("AWS Access Key ID is required")})+ }+ if hasKeyID && !hasSecret {return nil, common.Classify(common.ErrValidation, &base.FieldError{Field: "awsSecretAccessKey", Err: errors.New("AWS Secret Access Key is required")})}- encryptedSecret, encryptErr := crypto.Encrypt(req.AWSSecretAccessKey)- if encryptErr != nil {- return nil, fmt.Errorf("failed to encrypt AWS secret access key: %w", encryptErr)+ if hasSecret {+ encryptedSecret, encryptErr := crypto.Encrypt(req.AWSSecretAccessKey)+ if encryptErr != nil {+ return nil, fmt.Errorf("failed to encrypt AWS secret access key: %w", encryptErr)+ }+ registryRecord.AWSSecretAccessKey = encryptedSecret}registryRecord.AWSAccessKeyID = req.AWSAccessKeyID- registryRecord.AWSSecretAccessKey = encryptedSecretregistryRecord.AWSRegion = req.AWSRegion} else {if strings.TrimSpace(req.Username) == "" {@@ -363,10 +372,22 @@ func (s *ContainerRegistryService) applyRegistryTypeUpdateInternal(localRegistry}func (s *ContainerRegistryService) updateECRRegistryFieldsInternal(localRegistry *ContainerRegistry, req containerregistry.UpdateContainerRegistryRequest) error {+ if err := validateECRRegistryEndpoint(localRegistry.URL, localRegistry.Insecure); err != nil {+ return err+ }utils.ApplyChanged(&localRegistry.AWSAccessKeyID, mo.PointerToOption(req.AWSAccessKeyID))utils.ApplyChanged(&localRegistry.AWSRegion, mo.PointerToOption(req.AWSRegion))- if req.AWSSecretAccessKey != nil && *req.AWSSecretAccessKey != "" {+ hasKeyID := strings.TrimSpace(localRegistry.AWSAccessKeyID) != ""+ providedSecret := req.AWSSecretAccessKey != nil && *req.AWSSecretAccessKey != ""++ switch {+ case !hasKeyID && providedSecret:+ return common.Classify(common.ErrValidation, &base.FieldError{Field: "awsAccessKeyId", Err: errors.New("AWS Access Key ID is required")})+ case !hasKeyID:+ // No static key: the default AWS credential chain applies, so drop any stored secret.+ localRegistry.AWSSecretAccessKey = ""+ case providedSecret:encryptedSecret, err := crypto.Encrypt(*req.AWSSecretAccessKey)if err != nil {return fmt.Errorf("failed to encrypt AWS secret access key: %w", err)@@ -374,13 +395,10 @@ func (s *ContainerRegistryService) updateECRRegistryFieldsInternal(localRegistryutils.ApplyChanged(&localRegistry.AWSSecretAccessKey, mo.Some(encryptedSecret))}- if strings.TrimSpace(localRegistry.AWSAccessKeyID) == "" {- return common.Classify(common.ErrValidation, &base.FieldError{Field: "awsAccessKeyId", Err: errors.New("AWS Access Key ID is required")})- }if strings.TrimSpace(localRegistry.AWSRegion) == "" {return common.Classify(common.ErrValidation, &base.FieldError{Field: "awsRegion", Err: errors.New("AWS Region is required")})}- if strings.TrimSpace(localRegistry.AWSSecretAccessKey) == "" {+ if hasKeyID && strings.TrimSpace(localRegistry.AWSSecretAccessKey) == "" {return common.Classify(common.ErrValidation, &base.FieldError{Field: "awsSecretAccessKey", Err: errors.New("AWS Secret Access Key is required")})}@@ -1339,14 +1357,12 @@ func (s *ContainerRegistryService) checkRegistryNeedsUpdateInternal(item containcredChanged := utils.ApplyChanged(&existing.AWSAccessKeyID, mo.Some(item.AWSAccessKeyID))credChanged = utils.ApplyChanged(&existing.AWSRegion, mo.Some(item.AWSRegion)) || credChanged- // Update the AWS secret only when the manager sent one that differs.- if item.AWSSecretAccessKey != "" {- secretChanged, encryptAWSSecretErr := utils.ApplyEncrypted(&existing.AWSSecretAccessKey, item.AWSSecretAccessKey)- if encryptAWSSecretErr != nil {- return false, fmt.Errorf("failed to apply AWS secret for registry %s: %w", existing.ID, encryptAWSSecretErr)- }- credChanged = secretChanged || credChanged+ // An empty secret means the manager has no static keys, so the agent's stored one is cleared.+ secretChanged, encryptAWSSecretErr := utils.ApplyEncrypted(&existing.AWSSecretAccessKey, item.AWSSecretAccessKey)+ if encryptAWSSecretErr != nil {+ return false, fmt.Errorf("failed to apply AWS secret for registry %s: %w", existing.ID, encryptAWSSecretErr)}+ credChanged = secretChanged || credChanged// Invalidate cached ECR token when credentials changeif credChanged {@@ -1625,6 +1641,9 @@ type ecrTokenResult struct {// ECR API, persisted back to the DB, and returned.// Concurrent refreshes for the same registry are deduplicated via singleflight.func (s *ContainerRegistryService) GetOrRefreshECRToken(ctx context.Context, reg *ContainerRegistry) (username, password string, err error) {+ if endpointErr := validateECRRegistryEndpoint(reg.URL, reg.Insecure); endpointErr != nil {+ return "", "", endpointErr+ }// Fast path: return cached token if still valid.if reg.ECRTokenGeneratedAt != nil && time.Since(reg.ECRTokenGeneratedAt.UTC()) < ecrTokenTTL {if reg.ECRToken != "" {@@ -1653,25 +1672,28 @@ func (s *ContainerRegistryService) GetOrRefreshECRToken(ctx context.Context, reg}func (s *ContainerRegistryService) refreshECRTokenInternal(ctx context.Context, reg *ContainerRegistry) (*ecrTokenResult, error) {- // Decrypt the stored AWS secret access key.- secretKey, decErr := crypto.Decrypt(reg.AWSSecretAccessKey)- if decErr != nil {- return nil, fmt.Errorf("failed to decrypt AWS secret key for registry %s: %w", reg.URL, decErr)- }- secretKey = strings.TrimSpace(secretKey)- if secretKey == "" {- return nil, fmt.Errorf("AWS secret access key is empty for registry %s", reg.URL)- }-- // Call AWS ECR GetAuthorizationToken.- cfg, cfgErr := config.LoadDefaultConfig(ctx,- config.WithRegion(reg.AWSRegion),- config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(+ configOptions := []func(*config.LoadOptions) error{config.WithRegion(reg.AWSRegion)}++ // Without a static access key, the default AWS credential chain applies+ // (env, shared config, ECS task role, EC2 instance profile).+ if strings.TrimSpace(reg.AWSAccessKeyID) != "" {+ secretKey, decErr := crypto.Decrypt(reg.AWSSecretAccessKey)+ if decErr != nil {+ return nil, fmt.Errorf("failed to decrypt AWS secret key for registry %s: %w", reg.URL, decErr)+ }+ secretKey = strings.TrimSpace(secretKey)+ if secretKey == "" {+ return nil, fmt.Errorf("AWS secret access key is empty for registry %s", reg.URL)+ }+ configOptions = append(configOptions, config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(reg.AWSAccessKeyID,secretKey,"",- )),- )+ )))+ }++ // Call AWS ECR GetAuthorizationToken.+ cfg, cfgErr := config.LoadDefaultConfig(ctx, configOptions...)if cfgErr != nil {return nil, fmt.Errorf("failed to load AWS config for registry %s: %w", reg.URL, cfgErr)}diff --git a/backend/internal/registry/service_test.go b/backend/internal/registry/service_test.goindex 793e1c9e7..3fb746b99 100644--- a/backend/internal/registry/service_test.go+++ b/backend/internal/registry/service_test.go@@ -639,6 +639,112 @@ func TestContainerRegistryService_UpdateRegistryRejectsECRTargetChangeWithStoredrequire.Equal(t, "new-secret-key", decryptedSecret)}+func TestContainerRegistryService_CreateRegistry_AcceptsECRWithoutKeysForIAMRole(t *testing.T) {+ db := setupContainerRegistryTestDBInternal(t)+ svc := NewContainerRegistryService(db, nil, nil, nil)++ reg, err := svc.CreateRegistry(t.Context(), containerregistry.CreateContainerRegistryRequest{+ URL: "123456789012.dkr.ecr.us-east-1.amazonaws.com",+ RegistryType: RegistryTypeECR,+ AWSRegion: "us-east-1",+ })+ require.NoError(t, err)+ assert.Empty(t, reg.AWSAccessKeyID)+ assert.Empty(t, reg.AWSSecretAccessKey)+ assert.Equal(t, "us-east-1", reg.AWSRegion)+}++func TestContainerRegistryService_CreateRegistry_RejectsIncompleteECRKeyPair(t *testing.T) {+ tests := []struct {+ name string+ keyID string+ key string+ field string+ }{+ {name: "key id without secret", keyID: "access-key", field: "awsSecretAccessKey"},+ {name: "secret without key id", key: "secret-key", field: "awsAccessKeyId"},+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ db := setupContainerRegistryTestDBInternal(t)+ svc := NewContainerRegistryService(db, nil, nil, nil)++ _, err := svc.CreateRegistry(t.Context(), containerregistry.CreateContainerRegistryRequest{+ URL: "123456789012.dkr.ecr.us-east-1.amazonaws.com",+ RegistryType: RegistryTypeECR,+ AWSAccessKeyID: tt.keyID,+ AWSSecretAccessKey: tt.key,+ AWSRegion: "us-east-1",+ })+ require.ErrorIs(t, err, common.ErrValidation)+ fieldErr, ok := errors.AsType[*base.FieldError](err)+ require.True(t, ok)+ assert.Equal(t, tt.field, fieldErr.Field)+ })+ }+}++func TestContainerRegistryService_UpdateRegistry_ClearingECRKeysSwitchesToIAMRole(t *testing.T) {+ db := setupContainerRegistryTestDBInternal(t)+ svc := NewContainerRegistryService(db, nil, nil, nil)++ reg, err := svc.CreateRegistry(t.Context(), containerregistry.CreateContainerRegistryRequest{+ URL: "123456789012.dkr.ecr.us-east-1.amazonaws.com",+ RegistryType: RegistryTypeECR,+ AWSAccessKeyID: "access-key",+ AWSSecretAccessKey: "secret-key",+ AWSRegion: "us-east-1",+ })+ require.NoError(t, err)++ cachedAt := time.Now().UTC()+ reg.ECRToken = "cached-token"+ reg.ECRTokenGeneratedAt = &cachedAt+ require.NoError(t, db.WithContext(t.Context()).Save(reg).Error)++ updated, err := svc.UpdateRegistry(t.Context(), reg.ID, containerregistry.UpdateContainerRegistryRequest{+ AWSAccessKeyID: new(""),+ })+ require.NoError(t, err)+ assert.Empty(t, updated.AWSAccessKeyID)+ assert.Empty(t, updated.AWSSecretAccessKey)+ assert.Empty(t, updated.ECRToken)+ assert.Nil(t, updated.ECRTokenGeneratedAt)+}++func TestContainerRegistryService_SyncRegistries_ClearsECRSecretWhenManagerSendsNoKeys(t *testing.T) {+ db := setupContainerRegistryTestDBInternal(t)+ svc := NewContainerRegistryService(db, nil, nil, nil)++ existing, err := svc.CreateRegistry(t.Context(), containerregistry.CreateContainerRegistryRequest{+ URL: "123456789012.dkr.ecr.us-east-1.amazonaws.com",+ RegistryType: RegistryTypeECR,+ AWSAccessKeyID: "access-key",+ AWSSecretAccessKey: "secret-key",+ AWSRegion: "us-east-1",+ })+ require.NoError(t, err)++ err = svc.SyncRegistries(t.Context(), []containerregistry.Sync{+ {+ ID: existing.ID,+ URL: existing.URL,+ Enabled: true,+ RegistryType: RegistryTypeECR,+ AWSRegion: "us-east-1",+ CreatedAt: existing.CreatedAt,+ UpdatedAt: existing.UpdatedAt,+ },+ })+ require.NoError(t, err)++ var updated ContainerRegistry+ require.NoError(t, db.WithContext(t.Context()).First(&updated, "id = ?", existing.ID).Error)+ assert.Empty(t, updated.AWSAccessKeyID)+ assert.Empty(t, updated.AWSSecretAccessKey)+}+func TestContainerRegistryService_UpdateRegistry_RejectsChangingRegistryType(t *testing.T) {db := setupContainerRegistryTestDBInternal(t)svc := NewContainerRegistryService(db, nil, nil, nil)diff --git a/cli/pkg/registries/cmd.go b/cli/pkg/registries/cmd.goindex 4465db448..897a44499 100644--- a/cli/pkg/registries/cmd.go+++ b/cli/pkg/registries/cmd.go@@ -373,8 +373,8 @@ func init() {createCmd.Flags().BoolVar(®istryCreateInsecure, "insecure", false, "Allow insecure (HTTP) connections")createCmd.Flags().BoolVar(®istryCreateDisabled, "disabled", false, "Create registry in disabled state")createCmd.Flags().StringVar(®istryCreateType, "registry-type", "generic", "Registry type: generic or ecr")- createCmd.Flags().StringVar(®istryCreateAWSKeyID, "aws-access-key-id", "", "AWS Access Key ID (ECR only)")- createCmd.Flags().StringVar(®istryCreateAWSSecret, "aws-secret-access-key", "", "AWS Secret Access Key (ECR only)")+ createCmd.Flags().StringVar(®istryCreateAWSKeyID, "aws-access-key-id", "", "AWS Access Key ID (ECR only; omit both keys to use the host's IAM role)")+ createCmd.Flags().StringVar(®istryCreateAWSSecret, "aws-secret-access-key", "", "AWS Secret Access Key (ECR only; omit both keys to use the host's IAM role)")createCmd.Flags().StringVar(®istryCreateAWSRegion, "aws-region", "", "AWS region (ECR only)")createCmd.Flags().BoolVar(&jsonOutput, "json", false, "Output in JSON format")_ = createCmd.MarkFlagRequired("url")diff --git a/frontend/messages/en.json b/frontend/messages/en.jsonindex 1f7463d76..d37dc6beb 100644--- a/frontend/messages/en.json+++ b/frontend/messages/en.json@@ -2036,6 +2036,8 @@"registries_aws_secret_access_key": "AWS Secret Access Key","registries_aws_region": "AWS Region","registries_aws_region_placeholder": "e.g. us-east-1",+ "registries_aws_credentials_description": "Leave both AWS key fields empty to use the IAM role (instance profile) of the host running Arcane.",+ "registries_aws_iam_role": "IAM role","registries_repository_names": "Repository Names","registries_repository_names_placeholder": "team/api\nteam/platform/api","registries_repository_names_description": "One complete repository name per line. Used when pushing images from the build page.",diff --git a/frontend/src/lib/components/sheets/container-registry-sheet.svelte b/frontend/src/lib/components/sheets/container-registry-sheet.svelteindex 73bcda2b2..3ca09cb1d 100644--- a/frontend/src/lib/components/sheets/container-registry-sheet.svelte+++ b/frontend/src/lib/components/sheets/container-registry-sheet.svelte@@ -48,21 +48,25 @@}).superRefine((data, ctx) => {if (data.registryType === 'ecr') {- if (!data.awsAccessKeyId?.trim()) {+ // Both key fields empty means the host's IAM role is used; otherwise they must come as a pair.+ const hasKeyId = !!data.awsAccessKeyId?.trim();+ const hasSecret = !!data.awsSecretAccessKey?.trim();+ const storedSecretKept = isEditMode && !!registryToEdit?.awsAccessKeyId;+ if (!data.awsRegion?.trim()) {ctx.addIssue({code: z.ZodIssueCode.custom,- message: m.registries_aws_access_key_id_required(),- path: ['awsAccessKeyId']+ message: m.registries_aws_region_required(),+ path: ['awsRegion']});}- if (!data.awsRegion?.trim()) {+ if (hasSecret && !hasKeyId) {ctx.addIssue({code: z.ZodIssueCode.custom,- message: m.registries_aws_region_required(),- path: ['awsRegion']+ message: m.registries_aws_access_key_id_required(),+ path: ['awsAccessKeyId']});}- if (!isEditMode && !data.awsSecretAccessKey?.trim()) {+ if (hasKeyId && !hasSecret && !storedSecretKept) {ctx.addIssue({code: z.ZodIssueCode.custom,message: m.registries_aws_secret_access_key_required(),@@ -161,11 +165,16 @@bind:input={inputs.token}/>{:else}- <FormInput label={m.registries_aws_access_key_id()} type="text" bind:input={inputs.awsAccessKeyId} />+ <FormInput+ label={m.registries_aws_access_key_id()}+ type="text"+ description={m.registries_aws_credentials_description()}+ bind:input={inputs.awsAccessKeyId}+ /><FormInputlabel={m.registries_aws_secret_access_key()}type="password"- placeholder={isEditMode ? m.registries_token_keep_placeholder() : ''}+ placeholder={isEditMode && registryToEdit?.awsAccessKeyId ? m.registries_token_keep_placeholder() : ''}bind:input={inputs.awsSecretAccessKey}/><FormInputdiff --git a/frontend/src/routes/(app)/customize/registries/components/registry-table.svelte b/frontend/src/routes/(app)/customize/registries/components/registry-table.svelteindex 4a4cb6425..cfd55d676 100644--- a/frontend/src/routes/(app)/customize/registries/components/registry-table.svelte+++ b/frontend/src/routes/(app)/customize/registries/components/registry-table.svelte@@ -46,7 +46,7 @@const canBrowseRegistry = $derived(hasPermission('registries:read') && hasPermission('registries:browse'));function maskAccessKeyId(keyId: string | undefined): string {- if (!keyId) return m.common_na();+ if (!keyId) return m.registries_aws_iam_role();if (keyId.length <= 4) return keyId;return '*'.repeat(11) + keyId.slice(-4);}@@ -240,9 +240,7 @@item.registryType === 'ecr' ? maskAccessKeyId(item.awsAccessKeyId) : item.username,icon: UserIcon,iconVariant: 'gray' as const,- show:- (mobileFieldVisibility['username'] ?? true) &&- (item.registryType === 'ecr' ? !!item.awsAccessKeyId : item.username !== undefined)+ show: (mobileFieldVisibility['username'] ?? true) && (item.registryType === 'ecr' || item.username !== undefined)},{label: m.common_description(),diff --git a/types/containerregistry/container_registry.go b/types/containerregistry/container_registry.goindex 568969e62..bfeb61a56 100644--- a/types/containerregistry/container_registry.go+++ b/types/containerregistry/container_registry.go@@ -46,6 +46,7 @@ type ContainerRegistry struct {RepositoryNames []string `json:"repositoryNames"`// AWSAccessKeyID is the AWS Access Key ID for ECR registries.+ // Empty means the default AWS credential chain (e.g. an IAM instance profile) is used.//// Required: falseAWSAccessKeyID string `json:"awsAccessKeyId,omitempty"`@@ -198,6 +199,7 @@ type Sync struct {RepositoryNames []string `json:"repositoryNames"`// AWSAccessKeyID is the AWS Access Key ID for ECR registries.+ // Empty means the agent uses the default AWS credential chain (e.g. its own IAM instance profile).//// Required: falseAWSAccessKeyID string `json:"awsAccessKeyId,omitempty"`