Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support different shells #18

Merged
merged 3 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ $ aws --profile=my-profile sts get-caller-identity
"SessionToken": "..."
}
```
2. Env format suitable for setting environment variables in the shell, via `-env` flag
2. Env format suitable for setting environment variables in the shell, via `-env` flag.

_Note that the command for setting the environment variables is for the default shell of the current user._

```
$ oidc2aws arn:aws:iam::123456789012:role/my-role
Expand All @@ -157,6 +159,21 @@ $ aws --profile=my-profile sts get-caller-identity
AWS_SESSION_TOKEN=...
```

If you are using `fish` shell, you can do this instead:
```
$ oidc2aws -env arn:aws:iam::123456789012:role/my-role | source
```

If you are not using the default shell of current user, you can set the shell
type explicitly by `-shell` flag:

```
$ oidc2aws -env -shell csh arn:aws:iam::123456789012:role/my-role
setenv AWS_ACCESS_KEY_ID ASIA...
setenv AWS_SECRET_ACCESS_KEY ...
setenv AWS_SESSION_TOKEN ...
```

# `-login`: AWS Console Login

You can use `oidc2aws` to automatically log in to the AWS console
Expand Down
36 changes: 33 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ var sourceRole = flag.String("sourcerole", "", "source role to assume before ass

var aliasFlag = flag.String("alias", "", "alias configured in ~/.oidc2aws/oidcconfig")

var shell = flag.String("shell", "", "shell type, possible values: bash, zsh, sh, fish, csh, tcsh")

func arnFilename(arn string) string {
arn = strings.Replace(arn, "/", "-", -1)
arn = strings.Replace(arn, ":", "-", -1)
Expand All @@ -48,9 +50,37 @@ func arnFilename(arn string) string {

func printCredentials(result *result) error {
if *envFormat {
fmt.Printf("export AWS_ACCESS_KEY_ID=%s\n", *result.Credentials.AccessKeyId)
fmt.Printf("export AWS_SECRET_ACCESS_KEY=%s\n", *result.Credentials.SecretAccessKey)
fmt.Printf("export AWS_SESSION_TOKEN=%s\n", *result.Credentials.SessionToken)
// Get the name of current user's default shell
default_shell := os.Getenv("SHELL")

current_shell := path.Base(default_shell)

// If the user has specified a shell, use that instead
if *shell != "" {
current_shell = *shell
}

// Check the shell type and print the appropriate command to export the variable
switch current_shell {
case "fish":
// For fish, use the set command
fmt.Printf("set -x AWS_ACCESS_KEY_ID %s\n", *result.Credentials.AccessKeyId)
fmt.Printf("set -x AWS_SECRET_ACCESS_KEY %s\n", *result.Credentials.SecretAccessKey)
fmt.Printf("set -x AWS_SESSION_TOKEN %s\n", *result.Credentials.SessionToken)
case "csh", "tcsh":
// For csh and tcsh, use the setenv command
fmt.Printf("setenv AWS_ACCESS_KEY_ID %s\n", *result.Credentials.AccessKeyId)
fmt.Printf("setenv AWS_SECRET_ACCESS_KEY %s\n", *result.Credentials.SecretAccessKey)
fmt.Printf("setenv AWS_SESSION_TOKEN %s\n", *result.Credentials.SessionToken)
case "bash", "zsh", "sh":
fallthrough
default:
// For bash, zsh, sh and any other shell, use the export command
fmt.Printf("export AWS_ACCESS_KEY_ID=%s\n", *result.Credentials.AccessKeyId)
fmt.Printf("export AWS_SECRET_ACCESS_KEY=%s\n", *result.Credentials.SecretAccessKey)
fmt.Printf("export AWS_SESSION_TOKEN=%s\n", *result.Credentials.SessionToken)
}

return nil
} else if *loginFormat {
return fetchSigninToken(result)
Expand Down