Skip to content

Commit 9648c63

Browse files
committed
Use utils.ECSCLIResourcePrefix, fix secret string generation
1 parent bede251 commit 9648c63

File tree

3 files changed

+36
-13
lines changed

3 files changed

+36
-13
lines changed

ecs-cli/Gopkg.lock

Lines changed: 1 addition & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ecs-cli/modules/cli/regcreds/regcreds_app.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
secretsClient "github.com/aws/amazon-ecs-cli/ecs-cli/modules/clients/aws/secretsmanager"
2020
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/commands/flags"
2121
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/config"
22+
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/utils"
2223
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/utils/regcreds"
2324
"github.com/aws/aws-sdk-go/aws"
2425
"github.com/aws/aws-sdk-go/service/secretsmanager"
@@ -27,12 +28,6 @@ import (
2728
"github.com/urfave/cli"
2829
)
2930

30-
const (
31-
// EcsCliResourcePrefix is prepended to the names of resources created through the ecs-cli
32-
// TODO: move to more generic package for use in other parts of the ecs-cli
33-
EcsCliResourcePrefix = "amazon-ecs-cli-setup-"
34-
)
35-
3631
// CredsOutputEntry contains the credential ARN and associated container names
3732
// TODO: use & move to output_reader once implemented?
3833
type CredsOutputEntry struct {
@@ -192,11 +187,11 @@ func getNewCommandConfig(c *cli.Context) *config.CommandConfig {
192187
}
193188

194189
func generateSecretName(regName string) *string {
195-
return aws.String(EcsCliResourcePrefix + regName)
190+
return aws.String(utils.ECSCLIResourcePrefix + regName)
196191
}
197192

198193
func generateSecretString(username, password string) *string {
199-
return aws.String(`{"username":"` + username + `"},{"password":"` + password + `"}`)
194+
return aws.String(`{"username":"` + username + `","password":"` + password + `"}`)
200195
}
201196

202197
func generateSecretDescription(regName string) *string {

ecs-cli/modules/cli/regcreds/regcreds_app_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
package regcreds
1515

1616
import (
17+
"encoding/json"
18+
"fmt"
1719
"testing"
1820

1921
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/clients/aws/secretsmanager/mock"
@@ -245,6 +247,36 @@ func TestValidateCredsInput_ErrorOnDuplicateContainers(t *testing.T) {
245247
assert.Error(t, err, "Expected creds with duplicate containers to return error")
246248
}
247249

250+
func TestGenerateSecretString(t *testing.T) {
251+
type ECSRegistrySecret struct {
252+
Username string `json:"username"`
253+
Password string `json:"password"`
254+
}
255+
256+
testCases := []struct {
257+
inputUsername string
258+
inputPassword string
259+
expectedSecret ECSRegistrySecret
260+
}{
261+
{"user1", "l33tp4$$w0rd", ECSRegistrySecret{"user1", "l33tp4$$w0rd"}},
262+
{"someUserNameThatIsVeryLong0987654321", "*3G7nMl6W*Pi#*erjm", ECSRegistrySecret{"someUserNameThatIsVeryLong0987654321", "*3G7nMl6W*Pi#*erjm"}},
263+
{"myemail@example.com", "some-dashed-psswrd-64", ECSRegistrySecret{"myemail@example.com", "some-dashed-psswrd-64"}},
264+
}
265+
for _, test := range testCases {
266+
t.Run(fmt.Sprintf("Parse registry secret %s", test.inputUsername), func(t *testing.T) {
267+
268+
actualSecretString := generateSecretString(test.inputUsername, test.inputPassword)
269+
assert.NotEmpty(t, *actualSecretString)
270+
271+
regSecret := &ECSRegistrySecret{}
272+
err := json.Unmarshal([]byte(*actualSecretString), regSecret)
273+
assert.NoError(t, err, "Unexpected error when unmarshalling registry secret")
274+
assert.Equal(t, test.expectedSecret.Username, regSecret.Username, "Expected username to match")
275+
assert.Equal(t, test.expectedSecret.Password, regSecret.Password, "Expected password to match")
276+
})
277+
}
278+
}
279+
248280
func getTestCredsEntry(secretARN, username, password, kmsKey string, containers []string) readers.RegistryCredEntry {
249281
return readers.RegistryCredEntry{
250282
SecretManagerARN: secretARN,

0 commit comments

Comments
 (0)