-
Notifications
You must be signed in to change notification settings - Fork 100
feat: allow fetching token from shell command #1405
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,18 @@ | ||
| ┌─────────┬──────────────────────┬────────┬────────────────┬──────────────────────┬───────────┐ | ||
| │ OPTION │ DESCRIPTION │ TYPE │ CONFIG KEY │ ENVIRONMENT VARIABLE │ FLAG │ | ||
| ├─────────┼──────────────────────┼────────┼────────────────┼──────────────────────┼───────────┤ | ||
| │ config │ Config file path │ string │ │ HCLOUD_CONFIG │ --config │ | ||
| │ │ (default │ │ │ │ │ | ||
| │ │ "~/.config/hcloud/cl │ │ │ │ │ | ||
| │ │ i.toml") │ │ │ │ │ | ||
| ├─────────┼──────────────────────┼────────┼────────────────┼──────────────────────┼───────────┤ | ||
| │ context │ Currently active │ string │ active_context │ HCLOUD_CONTEXT │ --context │ | ||
| │ │ context │ │ │ │ │ | ||
| ├─────────┼──────────────────────┼────────┼────────────────┼──────────────────────┼───────────┤ | ||
| │ token │ Hetzner Cloud API │ string │ token │ HCLOUD_TOKEN │ │ | ||
| │ │ token │ │ │ │ │ | ||
| └─────────┴──────────────────────┴────────┴────────────────┴──────────────────────┴───────────┘ | ||
| ┌───────────────┬──────────────────────┬────────┬────────────────┬──────────────────────┬───────────┐ | ||
| │ OPTION │ DESCRIPTION │ TYPE │ CONFIG KEY │ ENVIRONMENT VARIABLE │ FLAG │ | ||
| ├───────────────┼──────────────────────┼────────┼────────────────┼──────────────────────┼───────────┤ | ||
| │ config │ Config file path │ string │ │ HCLOUD_CONFIG │ --config │ | ||
| │ │ (default │ │ │ │ │ | ||
| │ │ "~/.config/hcloud/cl │ │ │ │ │ | ||
| │ │ i.toml") │ │ │ │ │ | ||
| ├───────────────┼──────────────────────┼────────┼────────────────┼──────────────────────┼───────────┤ | ||
| │ context │ Currently active │ string │ active_context │ HCLOUD_CONTEXT │ --context │ | ||
| │ │ context │ │ │ │ │ | ||
| ├───────────────┼──────────────────────┼────────┼────────────────┼──────────────────────┼───────────┤ | ||
| │ token │ Hetzner Cloud API │ string │ token │ HCLOUD_TOKEN │ │ | ||
| │ │ token │ │ │ │ │ | ||
| ├───────────────┼──────────────────────┼────────┼────────────────┼──────────────────────┼───────────┤ | ||
| │ token_command │ Command to retrieve │ string │ token_command │ HCLOUD_TOKEN_COMMAND │ │ | ||
| │ │ Hetzner Cloud API │ │ │ │ │ | ||
| │ │ token │ │ │ │ │ | ||
| └───────────────┴──────────────────────┴────────┴────────────────┴──────────────────────┴───────────┘ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
| "runtime" | ||
| "strings" | ||
| ) | ||
|
|
||
| func RetrieveToken(c Config) (string, error) { | ||
| tok, err := OptionToken.Get(c) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| if tok != "" { | ||
| return tok, nil | ||
| } | ||
|
|
||
| cmdStr, err := OptionTokenCommand.Get(c) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| if cmdStr != "" { | ||
| return tokenFromCommand(c, cmdStr) | ||
| } | ||
| return "", nil | ||
| } | ||
|
|
||
| // We might need to run the command to retrieve HCLOUD_TOKEN multiple times, for example when running tests. | ||
| // Since the command is provided by the user and might be expensive, we cache the command result after the first successful run. | ||
| var cmdCache = make(map[string]string) | ||
|
|
||
| func tokenFromCommand(c Config, cmdStr string) (string, error) { | ||
| if tok, ok := cmdCache[cmdStr]; ok { | ||
| return tok, nil | ||
| } | ||
|
|
||
| var cmd *exec.Cmd | ||
| if runtime.GOOS == "windows" { | ||
| cmd = exec.Command("cmd", "/C", cmdStr) | ||
| } else { | ||
| cmd = exec.Command("sh", "-c", cmdStr) | ||
| } | ||
|
|
||
| cmd.Env = append(cmd.Environ(), fmt.Sprintf("HCLOUD_CONTEXT=%s", c.ActiveContext().Name())) | ||
| cmd.Stderr = os.Stderr | ||
|
|
||
| out, err := cmd.Output() | ||
| if err != nil { | ||
| return "", fmt.Errorf("could not retrieve token: %w", err) | ||
| } | ||
|
Comment on lines
+49
to
+52
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we forward the command stderr to the cli stderr?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea |
||
| tok := strings.TrimSpace(string(out)) | ||
| cmdCache[cmdStr] = tok | ||
| return tok, nil | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few questions:
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, env variables would fix this
A shell offers more features like pipes, input/output file redirects etc. Also it allows passing the command as one string instead of a string slice, see below.
List of strings would be fine, except the only problem would be how to configure it using the
hcloud context createcommand. Passing it as a slice would mean passing--token-commandmultiple times which would be very cumbersome or to separate the parts with commas which would mean you couldn't use a comma in your token command. Accepting both a string and a string slice would be fine but I don't know if that would play well with config parsing.