// 2019 Rackspace, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"): you may // not use this file except in compliance with the License. You may obtain // a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the // License for the specific language governing permissions and limitations // under the License. // Package tokens is a helper function to gather the tokens from the users // keyring service. package tokens import ( "fmt" "os" "strings" "github.com/jedib0t/go-pretty/v6/table" "github.com/jokelyo/argparse" "github.com/sirupsen/logrus" "github.rackspace.com/DCM/gomigrate/core" "github.rackspace.com/gorax/gokeys" ) var ( argStaging *bool argReset *string resetMap = map[string]gokeys.Service{ "all": gokeys.SvcAll, "identity": gokeys.SvcIdentity, "core": gokeys.SvcCore, "sso": gokeys.SvcSSO, } resetOpts = func() []string { keys := make([]string, len(resetMap)) i := 0 for k := range resetMap { keys[i] = k i++ } return keys }() ) // Plugin function will return a argparse.Command type back to the parent parser // nolint:typecheck func Plugin(p *core.Parser) core.Plugin { cmd := p.NewCommand("tokens", "Return current token values") argStaging = cmd.Flag("", "staging", &argparse.Options{Help: "Return staging tokens"}) argReset = cmd.Selector("", "reset", resetOpts, &argparse.Options{ Help: fmt.Sprintf("Reset keyring creds: [%s]", strings.Join(resetOpts, ","))}) return core.Plugin{CMD: cmd, Func: pluginFunc} } // pluginFunc is the main entry point function and will gather each token type from the keyring service func pluginFunc(cfg gokeys.Settings) { var ( staging = false header = "Keys" ) if *argStaging { logrus.Debug("Using staging tokens.") staging = true resetMap["identity"] = gokeys.SvcIdentityStg resetMap["core"] = gokeys.SvcCoreStg header = "STAGING Keys" } if val, exist := resetMap[*argReset]; exist { if err := cfg.Delete(val); err != nil { logrus.Error(err) } return } // Pull a core token from the users gokeys keychain cKey, err := cfg.Core(staging) if err != nil { logrus.Fatal(err) } // Pull a Identity token from the users gokeys keychain iKey, err := cfg.Identity(staging) if err != nil { logrus.Fatal(err) } t := table.NewWriter() t.SetOutputMirror(os.Stdout) t.AppendHeader(table.Row{cKey.Username, header}) t.AppendRows([]table.Row{ {"SSO", cKey.Password}, {"IDENTITY", iKey.Password}, }) t.SetStyle(table.StyleDefault) t.Render() }