-
Notifications
You must be signed in to change notification settings - Fork 1
Add support for SAML #19
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
176f380
febb71e
1f463a6
df47ca7
8030209
373af95
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ import ( | |
| "github.com/corbaltcode/kion/cmd/kion/config" | ||
| "github.com/corbaltcode/kion/cmd/kion/util" | ||
| "github.com/corbaltcode/kion/internal/client" | ||
| "github.com/corbaltcode/kion/internal/saml" | ||
| "github.com/spf13/cobra" | ||
| "github.com/zalando/go-keyring" | ||
| "gopkg.in/yaml.v3" | ||
|
|
@@ -86,9 +87,17 @@ func run() error { | |
| } | ||
| idms := idmss[idmsAnswer.Index] | ||
|
|
||
| if idms.TypeID == client.IDMSTypeSAML { | ||
| return runSAMLSetup(host, idms, userConfigName) | ||
| } | ||
| return runPasswordSetup(host, idms, userConfigName) | ||
| } | ||
|
|
||
| func runPasswordSetup(host string, idms client.IDMS, userConfigName string) error { | ||
| var username string | ||
| var password string | ||
| var kion *client.Client | ||
| var err error | ||
|
|
||
| for { | ||
| err = survey.AskOne( | ||
|
|
@@ -193,29 +202,137 @@ func run() error { | |
| "username": username, | ||
| } | ||
|
|
||
| userConfigDir := filepath.Dir(userConfigName) | ||
| err = os.MkdirAll(userConfigDir, 0700) | ||
| if err := writeConfig(userConfigName, settings); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| keyCfg := config.KeyConfig{ | ||
| Key: appAPIKey.Key, | ||
| Created: appAPIKeyMetadata.Created, | ||
| } | ||
| return keyCfg.Save() | ||
| } | ||
|
|
||
| func runSAMLSetup(host string, idms client.IDMS, userConfigName string) error { | ||
| var metadataSource string | ||
| err := survey.AskOne( | ||
| &survey.Input{Message: "SAML IDP metadata URL or file path:"}, | ||
| &metadataSource, | ||
| survey.WithValidator(survey.Required), | ||
| ) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| f, err := os.OpenFile(userConfigName, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) | ||
|
|
||
| fmt.Println("Fetching and validating SAML metadata...") | ||
| metadata, err := saml.Metadata(metadataSource) | ||
| if err != nil { | ||
| return fmt.Errorf("invalid SAML metadata: %w", err) | ||
| } | ||
| fmt.Println("Metadata looks good.") | ||
|
|
||
| var appAPIKeyAnswer survey.OptionAnswer | ||
| err = survey.AskOne( | ||
| &survey.Select{ | ||
| Message: "Create App API Key?", | ||
| Options: []string{"Yes (recommended)", "No (re-authenticate via browser on each use)"}, | ||
| }, | ||
| &appAPIKeyAnswer, | ||
| ) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer f.Close() | ||
|
|
||
| err = yaml.NewEncoder(f).Encode(settings) | ||
| appAPIKey := &client.AppAPIKey{} | ||
| appAPIKeyMetadata := &client.AppAPIKeyMetadata{} | ||
| var rotateAppAPIKeys bool | ||
| var appAPIKeyDuration time.Duration | ||
|
|
||
| if appAPIKeyAnswer.Index == 0 { | ||
| spIssuer := "https://" + host + "/api/v1/saml/auth" | ||
| fmt.Println("A browser window will open to authenticate and create the App API Key.") | ||
| token, err := saml.Authenticate(host, metadata, spIssuer, false) | ||
| if err != nil { | ||
| return fmt.Errorf("SAML authentication failed: %w", err) | ||
| } | ||
| kion := client.NewWithToken(host, token, time.Now().Add(10*time.Minute)) | ||
|
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. Can we change all occurrences of
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. I am not against this change, but I don't know that it would make a functional difference in the operation. All of the places where we use 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. Somewhere, one of these gets persisted out to The CLI still has a bug somewhere where it doesn't respect the expiration date of credentials in the credentials cache, and part of me once thought it might be due to a TZ issue. |
||
|
|
||
| appAPIKey, err = kion.CreateAppAPIKey(util.AppAPIKeyName) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| appAPIKeyMetadata, err = kion.GetAppAPIKeyMetadata(appAPIKey.ID) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| err = survey.AskOne( | ||
| &survey.Confirm{ | ||
| Message: "Automatically rotate App API Keys?", | ||
| Default: true, | ||
| }, | ||
| &rotateAppAPIKeys, | ||
| ) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| err = survey.AskOne( | ||
| &survey.Input{Message: "Duration of App API Keys:", Default: "168h"}, | ||
| &appAPIKeyDuration, | ||
| survey.WithValidator(survey.Required), | ||
| survey.WithValidator(validateDuration), | ||
| ) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| var sessionDuration time.Duration | ||
| err = survey.AskOne( | ||
| &survey.Input{Message: "Duration of temporary credentials:", Default: "60m"}, | ||
| &sessionDuration, | ||
| survey.WithValidator(survey.Required), | ||
| survey.WithValidator(validateDuration), | ||
| ) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| settings := map[string]interface{}{ | ||
| "host": host, | ||
| "idms": idms.ID, | ||
| "saml-metadata": metadataSource, | ||
| "session-duration": sessionDuration, | ||
| } | ||
| if appAPIKeyAnswer.Index == 0 { | ||
| settings["app-api-key-duration"] = appAPIKeyDuration | ||
| settings["rotate-app-api-keys"] = rotateAppAPIKeys | ||
| } | ||
|
|
||
| if err := writeConfig(userConfigName, settings); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| keyCfg := config.KeyConfig{ | ||
| Key: appAPIKey.Key, | ||
| Created: appAPIKeyMetadata.Created, | ||
| } | ||
| return keyCfg.Save() | ||
| } | ||
|
|
||
| func writeConfig(name string, settings map[string]interface{}) error { | ||
| if err := os.MkdirAll(filepath.Dir(name), 0700); err != nil { | ||
| return err | ||
| } | ||
| f, err := os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer f.Close() | ||
| return yaml.NewEncoder(f).Encode(settings) | ||
| } | ||
|
|
||
| func fileExists(name string) (bool, error) { | ||
| _, err := os.Stat(name) | ||
| if err != nil && !errors.Is(err, fs.ErrNotExist) { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.