-
Notifications
You must be signed in to change notification settings - Fork 821
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor prompts to mfa-token-providers
Prompts were simple prompts, mfa token providers have the `mfa_serial` that the token is being requested for. It may simply be used to display to the user, or could be passed to the store in order to lookup the correct creds. This enables the interfacing of apps/stores that manage multiple mfa creds/secrets, examples of such stores may be bitwarden, ykman. The --prompt flag becomes --mfa-token-provider and AWS_VAULT_PROMPT is changed to AWS_VAULT_MFA_TOKEN_PROVIDER.
- Loading branch information
Showing
24 changed files
with
296 additions
and
166 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
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,33 @@ | ||
package mfa | ||
|
||
import ( | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
func init() { | ||
TokenProviders["kdialog"] = &KDialog{} | ||
} | ||
|
||
type KDialog struct { | ||
Serial string | ||
} | ||
|
||
func (k *KDialog) GetToken() (string, error) { | ||
cmd := exec.Command("kdialog", "--inputbox", defaultPrompt(k.Serial), "--title", "aws-vault") | ||
|
||
out, err := cmd.Output() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return strings.TrimSpace(string(out)), nil | ||
} | ||
|
||
func (k *KDialog) SetSerial(mfaSerial string) { | ||
k.Serial = mfaSerial | ||
} | ||
|
||
func (k *KDialog) GetSerial() string { | ||
return k.Serial | ||
} |
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,18 @@ | ||
package mfa | ||
|
||
type KnownToken struct { | ||
Token string | ||
Serial string | ||
} | ||
|
||
func (k *KnownToken) GetToken() (string, error) { | ||
return k.Token, nil | ||
} | ||
|
||
func (k *KnownToken) SetSerial(mfaSerial string) { | ||
k.Serial = mfaSerial | ||
} | ||
|
||
func (k *KnownToken) GetSerial() string { | ||
return k.Serial | ||
} |
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,38 @@ | ||
package mfa | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// TokenProvider is an interface to provide an mfa token. It's intended that providers do whatever is necessary to get | ||
// a token, eg prompt the use via the terminal or fetch it from a yubikey. | ||
type TokenProvider interface { | ||
GetToken() (string, error) | ||
GetSerial() string | ||
SetSerial(mfaSerial string) | ||
} | ||
|
||
func defaultPrompt(mfaSerial string) string { | ||
return fmt.Sprintf("Enter token for %s: ", mfaSerial) | ||
} | ||
|
||
var TokenProviders = map[string]TokenProvider{ | ||
"terminal": &Terminal{}, | ||
} | ||
|
||
func TokenProvidersAvailable() []string { | ||
providers := []string{} | ||
for k := range TokenProviders { | ||
providers = append(providers, k) | ||
} | ||
return providers | ||
} | ||
|
||
func GetTokenProvider(s string) TokenProvider { | ||
p, found := TokenProviders[s] | ||
if !found { | ||
panic(fmt.Sprintf("Prompt method %q doesn't exist", s)) | ||
} | ||
|
||
return p | ||
} |
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,21 @@ | ||
package mfa | ||
|
||
import ( | ||
"github.com/99designs/aws-vault/prompt" | ||
) | ||
|
||
type Terminal struct { | ||
Serial string | ||
} | ||
|
||
func (t *Terminal) GetToken() (string, error) { | ||
return prompt.TerminalPrompt(defaultPrompt(t.Serial)) | ||
} | ||
|
||
func (t *Terminal) SetSerial(mfaSerial string) { | ||
t.Serial = mfaSerial | ||
} | ||
|
||
func (t *Terminal) GetSerial() string { | ||
return t.Serial | ||
} |
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,45 @@ | ||
package mfa | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
) | ||
|
||
func init() { | ||
TokenProviders["ykman"] = &YkMan{} | ||
} | ||
|
||
type YkMan struct { | ||
Serial string | ||
} | ||
|
||
func (y *YkMan) GetToken() (otpToken string, err error) { | ||
defer func() { | ||
if err != nil { | ||
fmt.Printf("unable to get otp from ykman: %s\n", err) | ||
|
||
// something went wrong with getting a token from a ykman | ||
// fall back to terminal prompt | ||
tp := TokenProviders["terminal"] | ||
tp.SetSerial(y.Serial) | ||
otpToken, err = tp.GetToken() | ||
|
||
} | ||
}() | ||
|
||
cmd := exec.Command("ykman", "oath", "code", "-s", y.Serial) | ||
var out []byte | ||
out, err = cmd.Output() | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(out[:len(out)-1]), nil | ||
} | ||
|
||
func (y *YkMan) SetSerial(mfaSerial string) { | ||
y.Serial = mfaSerial | ||
} | ||
|
||
func (y *YkMan) GetSerial() string { | ||
return y.Serial | ||
} |
Oops, something went wrong.