|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path" |
| 8 | + |
| 9 | + "github.com/DevLabFoundry/aws-cli-auth/internal/credentialexchange" |
| 10 | + "github.com/spf13/cobra" |
| 11 | +) |
| 12 | + |
| 13 | +var ( |
| 14 | + Version string = "0.0.1" |
| 15 | + Revision string = "1111aaaa" |
| 16 | +) |
| 17 | + |
| 18 | +type Root struct { |
| 19 | + Cmd *cobra.Command |
| 20 | + // ChannelOut io.Writer |
| 21 | + // ChannelErr io.Writer |
| 22 | + // viperConf *viper.Viper |
| 23 | + rootFlags *rootCmdFlags |
| 24 | + Datadir string |
| 25 | +} |
| 26 | + |
| 27 | +type rootCmdFlags struct { |
| 28 | + cfgSectionName string |
| 29 | + storeInProfile bool |
| 30 | + killHangingProcess bool |
| 31 | + roleChain []string |
| 32 | + verbose bool |
| 33 | + duration int |
| 34 | +} |
| 35 | + |
| 36 | +func New() *Root { |
| 37 | + rf := &rootCmdFlags{} |
| 38 | + r := &Root{ |
| 39 | + rootFlags: rf, |
| 40 | + Cmd: &cobra.Command{ |
| 41 | + Use: "aws-cli-auth", |
| 42 | + Short: "CLI tool for retrieving AWS temporary credentials", |
| 43 | + Long: `CLI tool for retrieving AWS temporary credentials using SAML providers, or specified method of retrieval - i.e. force AWS_WEB_IDENTITY. |
| 44 | +Useful in situations like CI jobs or containers where multiple env vars might be present. |
| 45 | +Stores them under the $HOME/.aws/credentials file under a specified path or returns the crednetial_process payload for use in config`, |
| 46 | + Version: fmt.Sprintf("%s-%s", Version, Revision), |
| 47 | + SilenceUsage: true, |
| 48 | + SilenceErrors: true, |
| 49 | + }, |
| 50 | + } |
| 51 | + |
| 52 | + r.Cmd.PersistentFlags().StringSliceVarP(&rf.roleChain, "role-chain", "", []string{}, "If specified it will assume the roles from the base credentials, in order they are specified in") |
| 53 | + r.Cmd.PersistentFlags().BoolVarP(&rf.storeInProfile, "store-profile", "s", false, `By default the credentials are returned to stdout to be used by the credential_process. |
| 54 | + Set this flag to instead store the credentials under a named profile section. You can then reference that profile name via the CLI or for use in an SDK`) |
| 55 | + r.Cmd.PersistentFlags().StringVarP(&rf.cfgSectionName, "cfg-section", "", "", "Config section name in the default AWS credentials file. To enable priofi") |
| 56 | + // When specifying store in profile the config section name must be provided |
| 57 | + r.Cmd.MarkFlagsRequiredTogether("store-profile", "cfg-section") |
| 58 | + r.Cmd.PersistentFlags().IntVarP(&rf.duration, "max-duration", "d", 900, `Override default max session duration, in seconds, of the role session [900-43200]. |
| 59 | +NB: This cannot be higher than the 3600 as the API does not allow for AssumeRole for sessions longer than an hour`) |
| 60 | + r.Cmd.PersistentFlags().BoolVarP(&rf.verbose, "verbose", "v", false, "Verbose output") |
| 61 | + _ = r.dataDirInit() |
| 62 | + return r |
| 63 | +} |
| 64 | + |
| 65 | +// SubCommands is a standalone Builder helper |
| 66 | +// |
| 67 | +// IF you are making your sub commands public, you can just pass them directly `WithSubCommands` |
| 68 | +func SubCommands() []func(*Root) { |
| 69 | + return []func(*Root){ |
| 70 | + newSamlCmd, |
| 71 | + newClearCmd, |
| 72 | + newSpecificIdentityCmd, |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func (r *Root) WithSubCommands(iocFuncs ...func(rootCmd *Root)) { |
| 77 | + for _, fn := range iocFuncs { |
| 78 | + fn(r) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func (r *Root) Execute(ctx context.Context) error { |
| 83 | + return r.Cmd.ExecuteContext(ctx) |
| 84 | +} |
| 85 | + |
| 86 | +func (r *Root) dataDirInit() error { |
| 87 | + datadir := path.Join(credentialexchange.HomeDir(), fmt.Sprintf(".%s-data", credentialexchange.SELF_NAME)) |
| 88 | + if _, err := os.Stat(datadir); err != nil { |
| 89 | + return os.MkdirAll(datadir, 0755) |
| 90 | + } |
| 91 | + r.Datadir = datadir |
| 92 | + return nil |
| 93 | +} |
0 commit comments