From 2ad9504d158586c18c69adb0d3bfd1e4a7ca3e4d Mon Sep 17 00:00:00 2001 From: aiordache Date: Wed, 29 Apr 2020 16:54:24 +0200 Subject: [PATCH] add secret interface Signed-off-by: aiordache Signed-off-by: Nicolas De Loof --- ecs/cmd/main/main.go | 95 +++++++++++++++++++++++++++++++++++++++ ecs/pkg/amazon/api.go | 1 + ecs/pkg/amazon/sdk.go | 24 ++++++++++ ecs/pkg/amazon/secrets.go | 28 ++++++++++++ ecs/pkg/compose/api.go | 5 +++ 5 files changed, 153 insertions(+) create mode 100644 ecs/pkg/amazon/secrets.go diff --git a/ecs/cmd/main/main.go b/ecs/cmd/main/main.go index 99f556c7c..9326f3eee 100644 --- a/ecs/cmd/main/main.go +++ b/ecs/cmd/main/main.go @@ -2,6 +2,7 @@ package main import ( "context" + "errors" "fmt" "github.com/docker/cli/cli-plugins/manager" @@ -45,6 +46,7 @@ func NewRootCmd(name string, dockerCli command.Cli) *cobra.Command { cmd.AddCommand( VersionCommand(), ComposeCommand(&opts), + SecretCommand(&opts), ) cmd.Flags().StringVarP(&opts.profile, "profile", "p", "default", "AWS Profile") cmd.Flags().StringVarP(&opts.cluster, "cluster", "c", "default", "ECS cluster") @@ -164,3 +166,96 @@ func DownCommand(clusteropts *clusterOptions, projectOpts *compose.ProjectOption cmd.Flags().BoolVar(&opts.DeleteCluster, "delete-cluster", false, "Delete cluster") return cmd } + +func SecretCommand(clusteropts *clusterOptions) *cobra.Command { + cmd := &cobra.Command{ + Use: "secret", + } + opts := &compose.ProjectOptions{} + opts.AddFlags(cmd.Flags()) + + cmd.AddCommand( + CreateSecret(clusteropts), + InspectSecret(clusteropts), + ListSecrets(clusteropts), + DeleteSecret(clusteropts), + ) + return cmd +} + +type createSecretOptions struct { + Label string +} + +func CreateSecret(clusteropts *clusterOptions) *cobra.Command { + //opts := createSecretOptions{} + cmd := &cobra.Command{ + Use: "create [NAME]", + RunE: func(cmd *cobra.Command, args []string) error { + client, err := amazon.NewClient(clusteropts.profile, clusteropts.cluster, clusteropts.region) + if err != nil { + return err + } + if len(args) == 0 { + return errors.New("Missing mandatory parameter: [NAME]") + } + name := args[0] + content := "blabla" + id, err := client.CreateSecret(context.Background(), name, content) + fmt.Println(id) + return err + }, + } + //cmd.Flags().BoolVar(&opts.Label, "label", false, "Secret label") + return cmd +} + +func InspectSecret(clusteropts *clusterOptions) *cobra.Command { + cmd := &cobra.Command{ + Use: "inspect [NAME]", + RunE: func(cmd *cobra.Command, args []string) error { + client, err := amazon.NewClient(clusteropts.profile, clusteropts.cluster, clusteropts.region) + if err != nil { + return err + } + if len(args) == 0 { + return errors.New("Missing mandatory parameter: [NAME]") + } + name := args[0] + return client.InspectSecret(context.Background(), name) + }, + } + return cmd +} + +func ListSecrets(clusteropts *clusterOptions) *cobra.Command { + cmd := &cobra.Command{ + Use: "list", + Aliases: []string{"ls"}, + RunE: func(cmd *cobra.Command, args []string) error { + client, err := amazon.NewClient(clusteropts.profile, clusteropts.cluster, clusteropts.region) + if err != nil { + return err + } + return client.ListSecrets(context.Background()) + }, + } + return cmd +} + +func DeleteSecret(clusteropts *clusterOptions) *cobra.Command { + cmd := &cobra.Command{ + Use: "delete [NAME]", + RunE: func(cmd *cobra.Command, args []string) error { + client, err := amazon.NewClient(clusteropts.profile, clusteropts.cluster, clusteropts.region) + if err != nil { + return err + } + if len(args) == 0 { + return errors.New("Missing mandatory parameter: [NAME]") + } + return client.DeleteSecret(context.Background(), args[0]) + }, + } + return cmd +} diff --git a/ecs/pkg/amazon/api.go b/ecs/pkg/amazon/api.go index ff61174d0..b4914d68a 100644 --- a/ecs/pkg/amazon/api.go +++ b/ecs/pkg/amazon/api.go @@ -6,4 +6,5 @@ type API interface { downAPI upAPI convertAPI + secretsAPI } diff --git a/ecs/pkg/amazon/sdk.go b/ecs/pkg/amazon/sdk.go index b02e0df8c..2aa017e9f 100644 --- a/ecs/pkg/amazon/sdk.go +++ b/ecs/pkg/amazon/sdk.go @@ -18,6 +18,8 @@ import ( "github.com/aws/aws-sdk-go/service/elbv2/elbv2iface" "github.com/aws/aws-sdk-go/service/iam" "github.com/aws/aws-sdk-go/service/iam/iamiface" + "github.com/aws/aws-sdk-go/service/secretsmanager" + "github.com/aws/aws-sdk-go/service/secretsmanager/secretsmanageriface" cf "github.com/awslabs/goformation/v4/cloudformation" "github.com/sirupsen/logrus" ) @@ -30,6 +32,7 @@ type sdk struct { CW cloudwatchlogsiface.CloudWatchLogsAPI IAM iamiface.IAMAPI CF cloudformationiface.CloudFormationAPI + SM secretsmanageriface.SecretsManagerAPI } func NewAPI(sess *session.Session) API { @@ -40,6 +43,7 @@ func NewAPI(sess *session.Session) API { CW: cloudwatchlogs.New(sess), IAM: iam.New(sess), CF: cloudformation.New(sess), + SM: secretsmanager.New(sess), } } @@ -193,3 +197,23 @@ func (s sdk) DeleteStack(ctx context.Context, name string) error { }) return err } + +func (s sdk) CreateSecret(ctx context.Context, name string, content string) (string, error) { + logrus.Debug("Create secret " + name) + return "test", nil +} + +func (s sdk) InspectSecret(ctx context.Context, name string) error { + fmt.Printf("... done. \n") + return nil +} + +func (s sdk) ListSecrets(ctx context.Context) error { + fmt.Printf("... done. \n") + return nil +} + +func (s sdk) DeleteSecret(ctx context.Context, name string) error { + fmt.Printf("... done. \n") + return nil +} diff --git a/ecs/pkg/amazon/secrets.go b/ecs/pkg/amazon/secrets.go new file mode 100644 index 000000000..daabe79bc --- /dev/null +++ b/ecs/pkg/amazon/secrets.go @@ -0,0 +1,28 @@ +package amazon + +import ( + "context" +) + +type secretsAPI interface { + CreateSecret(ctx context.Context, name string, content string) (string, error) + InspectSecret(ctx context.Context, name string) error + ListSecrets(ctx context.Context) error + DeleteSecret(ctx context.Context, name string) error +} + +func (c client) CreateSecret(ctx context.Context, name string, content string) (string, error) { + return c.api.CreateSecret(ctx, name, content) +} + +func (c client) InspectSecret(ctx context.Context, name string) error { + return c.api.InspectSecret(ctx, name) +} + +func (c client) ListSecrets(ctx context.Context) error { + return c.api.ListSecrets(ctx) +} + +func (c client) DeleteSecret(ctx context.Context, name string) error { + return c.api.DeleteSecret(ctx, name) +} diff --git a/ecs/pkg/compose/api.go b/ecs/pkg/compose/api.go index f5e45bc68..32095102c 100644 --- a/ecs/pkg/compose/api.go +++ b/ecs/pkg/compose/api.go @@ -10,4 +10,9 @@ type API interface { Convert(ctx context.Context, project *Project) (*cloudformation.Template, error) ComposeUp(ctx context.Context, project *Project) error ComposeDown(ctx context.Context, projectName string, deleteCluster bool) error + + CreateSecret(ctx context.Context, name string, content string) (string, error) + InspectSecret(ctx context.Context, name string) error + ListSecrets(ctx context.Context) error + DeleteSecret(ctx context.Context, name string) error }