Skip to content

Commit e4d47e9

Browse files
author
Alexandre Catalan
committed
fix(registry): address PR #1693 review comments for STACKITCRE-382
- Migrate optional secret flag resolution to flags.ResolveSecretFlag in create and update commands to prevent unintended stdin prompts when omitted - Update command examples in create.go to read secrets from stdin (-) or file (@<file>) instead of deprecated literal strings - Regenerate registry create and update documentation - Expand unit test coverage in delete_test.go, describe_test.go, and update_test.go for missing args, missing flags, and empty/invalid artifactory UUIDs - Add secret resolution test cases using fstest.MapFS and verify rejection of literal secret values - Add missing unit test case for unsetting registry-custom-endpoint in unset_test.go Relates to STACKITCRE-382
1 parent 724e63f commit e4d47e9

9 files changed

Lines changed: 206 additions & 13 deletions

File tree

‎docs/stackit_registry_artifactory_create.md‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ stackit registry artifactory create [flags]
1919
Create a proxy-cache artifactory for Docker Hub
2020
$ stackit registry artifactory create --name docker-proxy --proxy-url https://registry-1.docker.io --proxy-registry-type docker-hub
2121
22-
Create a proxy-cache artifactory with credentials
23-
$ stackit registry artifactory create --name my-proxy --proxy-url https://custom-registry.com --proxy-registry-type harbor --proxy-access-key my-user --proxy-access-secret my-secret
22+
Create a proxy-cache artifactory with credentials, reading secret from stdin
23+
$ stackit registry artifactory create --name my-proxy --proxy-url https://custom-registry.com --proxy-registry-type harbor --proxy-access-key my-user --proxy-access-secret -
24+
25+
Create a proxy-cache artifactory with credentials, providing the path to a file with the secret
26+
$ stackit registry artifactory create --name my-proxy --proxy-url https://custom-registry.com --proxy-registry-type harbor --proxy-access-key my-user --proxy-access-secret @./secret.txt
2427
```
2528

2629
### Options
@@ -29,7 +32,7 @@ stackit registry artifactory create [flags]
2932
-h, --help Help for "stackit registry artifactory create"
3033
-n, --name string Artifactory name
3134
--proxy-access-key string Access key for upstream proxy registry authentication
32-
--proxy-access-secret string Proxy-Access-Secret. Can be a string (deprecated) or a file path, if prefixed with '@' (example: @./secret.txt). Will be read from stdin when empty.
35+
--proxy-access-secret secret Proxy-Access-Secret. Use @<file> to read from a file (example: @./secret.txt), or - to read from stdin (hidden input on a terminal). Literal values are not supported.
3336
--proxy-description string Description of the proxy registry
3437
--proxy-registry-type string Registry type of the upstream proxy (e.g., docker-hub, harbor)
3538
--proxy-url string Upstream registry URL to proxy

‎docs/stackit_registry_artifactory_update.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ stackit registry artifactory update ARTIFACTORY_ID [flags]
2222
```
2323
-h, --help Help for "stackit registry artifactory update"
2424
--proxy-access-key string Access key for upstream proxy registry authentication
25-
--proxy-access-secret string Proxy-Access-Secret. Can be a string (deprecated) or a file path, if prefixed with '@' (example: @./secret.txt). Will be read from stdin when empty.
25+
--proxy-access-secret secret Proxy-Access-Secret. Use @<file> to read from a file (example: @./secret.txt), or - to read from stdin (hidden input on a terminal). Literal values are not supported.
2626
--proxy-description string Description of the proxy registry
2727
--proxy-url string Upstream registry URL to proxy
2828
```

‎internal/cmd/config/unset/unset_test.go‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,16 @@ func TestParseInput(t *testing.T) {
296296
model.KMSCustomEndpoint = false
297297
}),
298298
},
299+
{
300+
description: "registry custom endpoint empty",
301+
flagValues: fixtureFlagValues(func(flagValues map[string]bool) {
302+
flagValues[registryCustomEndpointFlag] = false
303+
}),
304+
isValid: true,
305+
expectedModel: fixtureInputModel(func(model *inputModel) {
306+
model.RegistryCustomEndpoint = false
307+
}),
308+
},
299309
{
300310
description: "service account custom endpoint empty",
301311
flagValues: fixtureFlagValues(func(flagValues map[string]bool) {

‎internal/cmd/registry/artifactory/create/create.go‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,12 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
5656
"$ stackit registry artifactory create --name docker-proxy --proxy-url https://registry-1.docker.io --proxy-registry-type docker-hub",
5757
),
5858
examples.NewExample(
59-
`Create a proxy-cache artifactory with credentials`,
60-
"$ stackit registry artifactory create --name my-proxy --proxy-url https://custom-registry.com --proxy-registry-type harbor --proxy-access-key my-user --proxy-access-secret my-secret",
59+
`Create a proxy-cache artifactory with credentials, reading secret from stdin`,
60+
"$ stackit registry artifactory create --name my-proxy --proxy-url https://custom-registry.com --proxy-registry-type harbor --proxy-access-key my-user --proxy-access-secret -",
61+
),
62+
examples.NewExample(
63+
`Create a proxy-cache artifactory with credentials, providing the path to a file with the secret`,
64+
"$ stackit registry artifactory create --name my-proxy --proxy-url https://custom-registry.com --proxy-registry-type harbor --proxy-access-key my-user --proxy-access-secret @./secret.txt",
6165
),
6266
),
6367
RunE: func(cmd *cobra.Command, args []string) error {
@@ -142,7 +146,10 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel,
142146
proxyRegistryType := flags.FlagToStringPointer(p, cmd, proxyRegistryTypeFlag)
143147
proxyDesc := flags.FlagToStringPointer(p, cmd, proxyDescriptionFlag)
144148
proxyKey := flags.FlagToStringPointer(p, cmd, proxyAccessKeyFlag)
145-
proxySecret := flags.SecretFlagToStringPointer(p, cmd, proxyAccessSecretFlag)
149+
proxySecret, err := flags.ResolveSecretFlag(p, cmd, proxyAccessSecretFlag)
150+
if err != nil {
151+
return nil, err
152+
}
146153

147154
hasProxyDetails := proxyDesc != nil || proxyKey != nil || proxySecret != nil
148155
if hasProxyDetails && (proxyUrl == nil || proxyRegistryType == nil) {

‎internal/cmd/registry/artifactory/create/create_test.go‎

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@ package create
22

33
import (
44
"context"
5+
"strings"
56
"testing"
7+
"testing/fstest"
68

79
"github.com/google/go-cmp/cmp"
810
"github.com/google/go-cmp/cmp/cmpopts"
911
"github.com/google/uuid"
12+
"github.com/spf13/cobra"
1013
registry "github.com/stackitcloud/stackit-sdk-go/services/registry/v1api"
1114

1215
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
1316
"github.com/stackitcloud/stackit-cli/internal/pkg/testparams"
1417
"github.com/stackitcloud/stackit-cli/internal/pkg/testutils"
18+
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
1519
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1620
)
1721

@@ -32,6 +36,7 @@ var (
3236
testProxyDescription = "Docker Hub proxy cache"
3337
testProxyAccessKey = "my-user"
3438
testProxyAccessSecret = "my-secret"
39+
testSecretFile = "secret.txt"
3540
)
3641

3742
func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string {
@@ -74,6 +79,7 @@ func fixtureRequest(mods ...func(request *registry.ApiCreateArtifactoryRequest))
7479

7580
func TestParseInput(t *testing.T) {
7681
tests := []struct {
82+
stdin string
7783
description string
7884
flagValues map[string]string
7985
isValid bool
@@ -104,7 +110,7 @@ func TestParseInput(t *testing.T) {
104110
flagValues[proxyRegistryTypeFlag] = testProxyRegistryType
105111
flagValues[proxyDescriptionFlag] = testProxyDescription
106112
flagValues[proxyAccessKeyFlag] = testProxyAccessKey
107-
flagValues[proxyAccessSecretFlag] = testProxyAccessSecret
113+
flagValues[proxyAccessSecretFlag] = "@" + testSecretFile
108114
}),
109115
isValid: true,
110116
expectedModel: fixtureInputModel(func(model *inputModel) {
@@ -115,6 +121,36 @@ func TestParseInput(t *testing.T) {
115121
model.ProxyAccessSecret = utils.Ptr(testProxyAccessSecret)
116122
}),
117123
},
124+
{
125+
description: "with proxy full - secret from stdin",
126+
stdin: testProxyAccessSecret + "\n",
127+
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
128+
flagValues[proxyUrlFlag] = testProxyUrl
129+
flagValues[proxyRegistryTypeFlag] = testProxyRegistryType
130+
flagValues[proxyDescriptionFlag] = testProxyDescription
131+
flagValues[proxyAccessKeyFlag] = testProxyAccessKey
132+
flagValues[proxyAccessSecretFlag] = "-"
133+
}),
134+
isValid: true,
135+
expectedModel: fixtureInputModel(func(model *inputModel) {
136+
model.ProxyUrl = utils.Ptr(testProxyUrl)
137+
model.ProxyRegistryType = utils.Ptr(testProxyRegistryType)
138+
model.ProxyDescription = utils.Ptr(testProxyDescription)
139+
model.ProxyAccessKey = utils.Ptr(testProxyAccessKey)
140+
model.ProxyAccessSecret = utils.Ptr(testProxyAccessSecret)
141+
}),
142+
},
143+
{
144+
description: "with proxy full - literal secret rejected",
145+
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
146+
flagValues[proxyUrlFlag] = testProxyUrl
147+
flagValues[proxyRegistryTypeFlag] = testProxyRegistryType
148+
flagValues[proxyDescriptionFlag] = testProxyDescription
149+
flagValues[proxyAccessKeyFlag] = testProxyAccessKey
150+
flagValues[proxyAccessSecretFlag] = "literal-secret"
151+
}),
152+
isValid: false,
153+
},
118154
{
119155
description: "proxy url without registry type",
120156
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
@@ -177,7 +213,13 @@ func TestParseInput(t *testing.T) {
177213

178214
for _, tt := range tests {
179215
t.Run(tt.description, func(t *testing.T) {
180-
testutils.TestParseInput(t, NewCmd, parseInput, tt.expectedModel, []string{}, tt.flagValues, tt.isValid)
216+
testutils.TestParseInput(t, func(params *types.CmdParams) *cobra.Command {
217+
params.Fs = fstest.MapFS{
218+
testSecretFile: {Data: []byte(testProxyAccessSecret)},
219+
}
220+
params.Printer.StdIn = strings.NewReader(tt.stdin)
221+
return NewCmd(params)
222+
}, parseInput, tt.expectedModel, []string{}, tt.flagValues, tt.isValid)
181223
})
182224
}
183225
}

‎internal/cmd/registry/artifactory/delete/delete_test.go‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,36 @@ func TestParseInput(t *testing.T) {
100100
}),
101101
isValid: false,
102102
},
103+
{
104+
description: "no values",
105+
argValues: []string{},
106+
flagValues: map[string]string{},
107+
isValid: false,
108+
},
109+
{
110+
description: "no arg values",
111+
argValues: []string{},
112+
flagValues: fixtureFlagValues(),
113+
isValid: false,
114+
},
115+
{
116+
description: "no flag values",
117+
argValues: []string{testArtifactoryId},
118+
flagValues: map[string]string{},
119+
isValid: false,
120+
},
121+
{
122+
description: "artifactory id invalid 1",
123+
argValues: []string{""},
124+
flagValues: fixtureFlagValues(),
125+
isValid: false,
126+
},
127+
{
128+
description: "artifactory id invalid 2",
129+
argValues: []string{"invalid-uuid"},
130+
flagValues: fixtureFlagValues(),
131+
isValid: false,
132+
},
103133
}
104134

105135
for _, tt := range tests {

‎internal/cmd/registry/artifactory/describe/describe_test.go‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,36 @@ func TestParseInput(t *testing.T) {
102102
}),
103103
isValid: false,
104104
},
105+
{
106+
description: "no values",
107+
argValues: []string{},
108+
flagValues: map[string]string{},
109+
isValid: false,
110+
},
111+
{
112+
description: "no arg values",
113+
argValues: []string{},
114+
flagValues: fixtureFlagValues(),
115+
isValid: false,
116+
},
117+
{
118+
description: "no flag values",
119+
argValues: []string{testArtifactoryId},
120+
flagValues: map[string]string{},
121+
isValid: false,
122+
},
123+
{
124+
description: "artifactory id invalid 1",
125+
argValues: []string{""},
126+
flagValues: fixtureFlagValues(),
127+
isValid: false,
128+
},
129+
{
130+
description: "artifactory id invalid 2",
131+
argValues: []string{"invalid-uuid"},
132+
flagValues: fixtureFlagValues(),
133+
isValid: false,
134+
},
105135
}
106136

107137
for _, tt := range tests {

‎internal/cmd/registry/artifactory/update/update.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,10 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
126126
proxyUrl := flags.FlagToStringPointer(p, cmd, proxyUrlFlag)
127127
proxyDesc := flags.FlagToStringPointer(p, cmd, proxyDescriptionFlag)
128128
proxyKey := flags.FlagToStringPointer(p, cmd, proxyAccessKeyFlag)
129-
proxySecret := flags.SecretFlagToStringPointer(p, cmd, proxyAccessSecretFlag)
129+
proxySecret, err := flags.ResolveSecretFlag(p, cmd, proxyAccessSecretFlag)
130+
if err != nil {
131+
return nil, err
132+
}
130133

131134
if proxyUrl == nil && proxyDesc == nil && proxyKey == nil && proxySecret == nil {
132135
return nil, &cliErr.EmptyUpdateError{}

‎internal/cmd/registry/artifactory/update/update_test.go‎

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@ package update
22

33
import (
44
"context"
5+
"strings"
56
"testing"
7+
"testing/fstest"
68

79
"github.com/google/go-cmp/cmp"
810
"github.com/google/go-cmp/cmp/cmpopts"
911
"github.com/google/uuid"
12+
"github.com/spf13/cobra"
1013
registry "github.com/stackitcloud/stackit-sdk-go/services/registry/v1api"
1114

1215
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
1316
"github.com/stackitcloud/stackit-cli/internal/pkg/testparams"
1417
"github.com/stackitcloud/stackit-cli/internal/pkg/testutils"
18+
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
1519
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1620
)
1721

@@ -31,6 +35,7 @@ var (
3135
testProxyDescription = "docker hub proxy"
3236
testProxyAccessKey = "my-key"
3337
testProxyAccessSecret = "my-secret"
38+
testSecretFile = "secret.txt"
3439
)
3540

3641
func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string {
@@ -76,6 +81,7 @@ func fixtureRequest(mods ...func(request *registry.ApiPatchArtifactoryRequest))
7681

7782
func TestParseInput(t *testing.T) {
7883
tests := []struct {
84+
stdin string
7985
description string
8086
argValues []string
8187
flagValues map[string]string
@@ -108,7 +114,7 @@ func TestParseInput(t *testing.T) {
108114
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
109115
delete(flagValues, proxyUrlFlag)
110116
flagValues[proxyAccessKeyFlag] = testProxyAccessKey
111-
flagValues[proxyAccessSecretFlag] = testProxyAccessSecret
117+
flagValues[proxyAccessSecretFlag] = "@" + testSecretFile
112118
}),
113119
isValid: true,
114120
expectedModel: fixtureInputModel(func(model *inputModel) {
@@ -117,6 +123,32 @@ func TestParseInput(t *testing.T) {
117123
model.ProxyAccessSecret = utils.Ptr(testProxyAccessSecret)
118124
}),
119125
},
126+
{
127+
description: "with credentials - secret from stdin",
128+
stdin: testProxyAccessSecret + "\n",
129+
argValues: []string{testArtifactoryId},
130+
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
131+
delete(flagValues, proxyUrlFlag)
132+
flagValues[proxyAccessKeyFlag] = testProxyAccessKey
133+
flagValues[proxyAccessSecretFlag] = "-"
134+
}),
135+
isValid: true,
136+
expectedModel: fixtureInputModel(func(model *inputModel) {
137+
model.ProxyUrl = nil
138+
model.ProxyAccessKey = utils.Ptr(testProxyAccessKey)
139+
model.ProxyAccessSecret = utils.Ptr(testProxyAccessSecret)
140+
}),
141+
},
142+
{
143+
description: "with credentials - literal secret rejected",
144+
argValues: []string{testArtifactoryId},
145+
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
146+
delete(flagValues, proxyUrlFlag)
147+
flagValues[proxyAccessKeyFlag] = testProxyAccessKey
148+
flagValues[proxyAccessSecretFlag] = "literal-secret"
149+
}),
150+
isValid: false,
151+
},
120152
{
121153
description: "proxy access key without secret",
122154
argValues: []string{testArtifactoryId},
@@ -129,7 +161,7 @@ func TestParseInput(t *testing.T) {
129161
description: "proxy access secret without key",
130162
argValues: []string{testArtifactoryId},
131163
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
132-
flagValues[proxyAccessSecretFlag] = testProxyAccessSecret
164+
flagValues[proxyAccessSecretFlag] = "@" + testSecretFile
133165
}),
134166
isValid: false,
135167
},
@@ -165,11 +197,47 @@ func TestParseInput(t *testing.T) {
165197
}),
166198
isValid: false,
167199
},
200+
{
201+
description: "no values",
202+
argValues: []string{},
203+
flagValues: map[string]string{},
204+
isValid: false,
205+
},
206+
{
207+
description: "no arg values",
208+
argValues: []string{},
209+
flagValues: fixtureFlagValues(),
210+
isValid: false,
211+
},
212+
{
213+
description: "no flag values",
214+
argValues: []string{testArtifactoryId},
215+
flagValues: map[string]string{},
216+
isValid: false,
217+
},
218+
{
219+
description: "artifactory id invalid 1",
220+
argValues: []string{""},
221+
flagValues: fixtureFlagValues(),
222+
isValid: false,
223+
},
224+
{
225+
description: "artifactory id invalid 2",
226+
argValues: []string{"invalid-uuid"},
227+
flagValues: fixtureFlagValues(),
228+
isValid: false,
229+
},
168230
}
169231

170232
for _, tt := range tests {
171233
t.Run(tt.description, func(t *testing.T) {
172-
testutils.TestParseInput(t, NewCmd, parseInput, tt.expectedModel, tt.argValues, tt.flagValues, tt.isValid)
234+
testutils.TestParseInput(t, func(params *types.CmdParams) *cobra.Command {
235+
params.Fs = fstest.MapFS{
236+
testSecretFile: {Data: []byte(testProxyAccessSecret)},
237+
}
238+
params.Printer.StdIn = strings.NewReader(tt.stdin)
239+
return NewCmd(params)
240+
}, parseInput, tt.expectedModel, tt.argValues, tt.flagValues, tt.isValid)
173241
})
174242
}
175243
}

0 commit comments

Comments
 (0)