-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command to manipulate IAM identity mappings
Adds the following commands to get/create/delete IAM role mappings to Kubernetes username and groups. eksctl get iamidentitymapping [arn] eksctl create iamidentitymapping <arn> [--username=USER] [--group=GROUP0] [--group=GROUP1] eksctl delete iamidentitymapping <arn> Its behavior with regard to duplicate role ARNs is the same as when we mappings are manipulated while adding/removing nodegroups: Deletion removes at most one and Create is accepting duplicates.
- Loading branch information
Showing
9 changed files
with
347 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package create | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/kris-nova/logger" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
|
||
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5" | ||
"github.com/weaveworks/eksctl/pkg/authconfigmap" | ||
"github.com/weaveworks/eksctl/pkg/ctl/cmdutils" | ||
"github.com/weaveworks/eksctl/pkg/eks" | ||
) | ||
|
||
func createIAMIdentityMappingCmd(g *cmdutils.Grouping) *cobra.Command { | ||
p := &api.ProviderConfig{} | ||
cfg := api.NewClusterConfig() | ||
id := &authconfigmap.MapRole{} | ||
cmd := &cobra.Command{ | ||
Use: "iamidentitymapping <rolearn>", | ||
Short: "Create an IAM identity mapping", | ||
Long: `Creates a mapping from IAM role to Kubernetes user and groups. | ||
To create an admin use | ||
--group=system:masters --username=admin | ||
`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if err := doCreateIAMIdentityMapping(p, cfg, id, cmdutils.GetNameArg(args)); err != nil { | ||
logger.Critical("%s\n", err.Error()) | ||
os.Exit(1) | ||
} | ||
}, | ||
} | ||
group := g.New(cmd) | ||
|
||
group.InFlagSet("General", func(fs *pflag.FlagSet) { | ||
fs.StringVar(&id.RoleARN, "role", "", "ARN of the IAM role to create") | ||
fs.StringVar(&cfg.Metadata.Name, "cluster", "", "EKS cluster name") | ||
fs.StringVar(&id.Username, "username", "", "User name within Kubernetes to map to IAM role") | ||
fs.StringArrayVar(&id.Groups, "group", []string{}, "Group within Kubernetes to which IAM role is mapped") | ||
}) | ||
|
||
cmdutils.AddCommonFlagsForAWS(group, p, false) | ||
|
||
group.AddTo(cmd) | ||
|
||
return cmd | ||
} | ||
|
||
func doCreateIAMIdentityMapping(p *api.ProviderConfig, cfg *api.ClusterConfig, id *authconfigmap.MapRole, roleArg string) error { | ||
ctl := eks.New(p, cfg) | ||
|
||
if err := ctl.CheckAuth(); err != nil { | ||
return err | ||
} | ||
if id.RoleARN != "" && roleArg != "" { | ||
return cmdutils.ErrFlagAndArg("--role", id.RoleARN, roleArg) | ||
} | ||
roleFilter := id.RoleARN | ||
if roleArg != "" { | ||
roleFilter = roleArg | ||
} | ||
if roleFilter == "" { | ||
return cmdutils.ErrMustBeSet("--role") | ||
} | ||
if cfg.Metadata.Name == "" { | ||
return cmdutils.ErrMustBeSet("--cluster") | ||
} | ||
if err := id.Valid(); err != nil { | ||
return err | ||
} | ||
|
||
if err := ctl.GetCredentials(cfg); err != nil { | ||
return err | ||
} | ||
clientSet, err := ctl.NewStdClientSet(cfg) | ||
if err != nil { | ||
return err | ||
} | ||
acm, err := authconfigmap.NewFromClientSet(clientSet) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := acm.AddRole(roleFilter, id.Username, id.Groups); err != nil { | ||
return err | ||
} | ||
return acm.Save() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package delete | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/kris-nova/logger" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
|
||
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5" | ||
"github.com/weaveworks/eksctl/pkg/authconfigmap" | ||
"github.com/weaveworks/eksctl/pkg/ctl/cmdutils" | ||
"github.com/weaveworks/eksctl/pkg/eks" | ||
) | ||
|
||
func deleteIAMIdentityMappingCmd(g *cmdutils.Grouping) *cobra.Command { | ||
p := &api.ProviderConfig{} | ||
cfg := api.NewClusterConfig() | ||
var roleFlag string | ||
cmd := &cobra.Command{ | ||
Use: "iamidentitymapping <role>", | ||
Short: "Delete a IAM identity mapping", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if err := doDeleteIAMIdentityMapping(p, cfg, roleFlag, cmdutils.GetNameArg(args)); err != nil { | ||
logger.Critical("%s\n", err.Error()) | ||
os.Exit(1) | ||
} | ||
}, | ||
} | ||
group := g.New(cmd) | ||
|
||
group.InFlagSet("General", func(fs *pflag.FlagSet) { | ||
fs.StringVar(&roleFlag, "role", "", "ARN of the IAM role to delete") | ||
fs.StringVar(&cfg.Metadata.Name, "cluster", "", "EKS cluster name") | ||
}) | ||
|
||
cmdutils.AddCommonFlagsForAWS(group, p, false) | ||
|
||
group.AddTo(cmd) | ||
|
||
return cmd | ||
} | ||
|
||
func doDeleteIAMIdentityMapping(p *api.ProviderConfig, cfg *api.ClusterConfig, roleFlag, roleArg string) error { | ||
ctl := eks.New(p, cfg) | ||
|
||
if err := ctl.CheckAuth(); err != nil { | ||
return err | ||
} | ||
|
||
if roleFlag != "" && roleArg != "" { | ||
return cmdutils.ErrFlagAndArg("--role", roleFlag, roleArg) | ||
} | ||
roleFilter := roleFlag | ||
if roleArg != "" { | ||
roleFilter = roleArg | ||
} | ||
if roleFilter == "" { | ||
return cmdutils.ErrMustBeSet("--role") | ||
} | ||
if cfg.Metadata.Name == "" { | ||
return cmdutils.ErrMustBeSet("--cluster") | ||
} | ||
|
||
if err := ctl.GetCredentials(cfg); err != nil { | ||
return err | ||
} | ||
clientSet, err := ctl.NewStdClientSet(cfg) | ||
if err != nil { | ||
return err | ||
} | ||
acm, err := authconfigmap.NewFromClientSet(clientSet) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := acm.RemoveRole(roleFilter); err != nil { | ||
return err | ||
} | ||
return acm.Save() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.