Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit c6fc0e1

Browse files
authored
Merge pull request #879 from docker/acceskey_file
Flag to import access key & secret from file
2 parents ba0d7c6 + 44c58e7 commit c6fc0e1

File tree

1 file changed

+46
-3
lines changed

1 file changed

+46
-3
lines changed

cli/cmd/context/create_ecs.go

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,18 @@
1717
package context
1818

1919
import (
20+
"bufio"
2021
"context"
2122
"fmt"
22-
23-
"github.com/pkg/errors"
24-
"github.com/spf13/cobra"
23+
"os"
24+
"strings"
2525

2626
"github.com/docker/compose-cli/api/client"
2727
"github.com/docker/compose-cli/context/store"
2828
"github.com/docker/compose-cli/ecs"
2929
"github.com/docker/compose-cli/errdefs"
30+
"github.com/pkg/errors"
31+
"github.com/spf13/cobra"
3032
)
3133

3234
func init() {
@@ -41,15 +43,29 @@ $ docker context create ecs CONTEXT [flags]
4143
func createEcsCommand() *cobra.Command {
4244
var localSimulation bool
4345
var opts ecs.ContextParams
46+
var accessKeysFile string
4447
cmd := &cobra.Command{
4548
Use: "ecs CONTEXT [flags]",
4649
Short: "Create a context for Amazon ECS",
4750
Args: cobra.ExactArgs(1),
4851
RunE: func(cmd *cobra.Command, args []string) error {
4952
opts.Name = args[0]
53+
if accessKeysFile != "" {
54+
err := parseAccessKeysFile(accessKeysFile, &opts)
55+
if err != nil {
56+
return err
57+
}
58+
}
59+
5060
if opts.CredsFromEnv && opts.Profile != "" {
5161
return fmt.Errorf("--profile and --from-env flags cannot be set at the same time")
5262
}
63+
if accessKeysFile != "" && opts.Profile != "" {
64+
return fmt.Errorf("--profile and --access-keys flags cannot be set at the same time")
65+
}
66+
if opts.CredsFromEnv && accessKeysFile != "" {
67+
return fmt.Errorf("--access-keys and --from-env flags cannot be set at the same time")
68+
}
5369
if localSimulation {
5470
return runCreateLocalSimulation(cmd.Context(), args[0], opts)
5571
}
@@ -60,10 +76,37 @@ func createEcsCommand() *cobra.Command {
6076
addDescriptionFlag(cmd, &opts.Description)
6177
cmd.Flags().BoolVar(&localSimulation, "local-simulation", false, "Create context for ECS local simulation endpoints")
6278
cmd.Flags().StringVar(&opts.Profile, "profile", "", "Use an existing AWS profile")
79+
cmd.Flags().StringVar(&accessKeysFile, "access-keys", "", "Use AWS access keys from file")
6380
cmd.Flags().BoolVar(&opts.CredsFromEnv, "from-env", false, "Use AWS environment variables for profile, or credentials and region")
6481
return cmd
6582
}
6683

84+
func parseAccessKeysFile(file string, opts *ecs.ContextParams) error {
85+
f, err := os.Open(file)
86+
if err != nil {
87+
return err
88+
}
89+
defer f.Close() // nolint:errcheck
90+
scanner := bufio.NewScanner(f)
91+
scanner.Split(bufio.ScanLines)
92+
values := map[string]string{}
93+
for scanner.Scan() {
94+
line := scanner.Text()
95+
parts := strings.SplitN(line, "=", 2)
96+
values[parts[0]] = parts[1]
97+
}
98+
var ok bool
99+
opts.AccessKey, ok = values["AWSAccessKeyId"]
100+
if !ok {
101+
return fmt.Errorf("%s is missing AWSAccessKeyId", file)
102+
}
103+
opts.SecretKey, ok = values["AWSSecretKey"]
104+
if !ok {
105+
return fmt.Errorf("%s is missing AWSSecretKey", file)
106+
}
107+
return nil
108+
}
109+
67110
func runCreateLocalSimulation(ctx context.Context, contextName string, opts ecs.ContextParams) error {
68111
if contextExists(ctx, contextName) {
69112
return errors.Wrapf(errdefs.ErrAlreadyExists, "context %q", contextName)

0 commit comments

Comments
 (0)