Skip to content

Commit

Permalink
only connect to mytoken server after the mytoken server is extracted …
Browse files Browse the repository at this point in the history
…from a jwt, so that the client does not fail if the default mytoken server is down
  • Loading branch information
zachmann committed Jul 31, 2024
1 parent 763dc92 commit a4221b7
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 22 deletions.
2 changes: 1 addition & 1 deletion internal/commands/at.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ func getAT(context *cli.Context) error {
req := mytokenlib.NewAccessTokenRequest("", "", atc.Scopes.Value(), atc.Audiences.Value(), comment)
return doSSH(ssh, api.SSHRequestAccessToken, req)
}
mytoken := config.Get().Mytoken
mToken := atc.MustGetToken()
mytoken := config.Get().Mytoken()
atRes, err := mytoken.AccessToken.APIGet(
mToken, "", atc.Scopes.Value(), atc.Audiences.Value(), comment,
)
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func updateMytokenServerFromJWT(token string) {
if err != nil {
log.Fatal(err)
}
*(config.Get().Mytoken) = *mytoken
config.Get().SetMytokenServer(mytoken)
}
func (mt MTOptions) GetToken() string {
token := mt._getToken()
Expand Down
6 changes: 3 additions & 3 deletions internal/commands/grants.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func initGrants(parent *cli.Command) {

func listGrants(_ *cli.Context) error {
mytoken := settingsOptions.MustGetToken()
res, err := config.Get().Mytoken.UserSettings.Grants.APIGet(mytoken)
res, err := config.Get().Mytoken().UserSettings.Grants.APIGet(mytoken)
if err != nil {
return err
}
Expand Down Expand Up @@ -91,7 +91,7 @@ func enableGrant(ctx *cli.Context) error {
}
grant := ctx.Args().Get(0)
mytoken := settingsOptions.MustGetToken()
res, err := config.Get().Mytoken.UserSettings.Grants.APIEnableGrant(mytoken, grant)
res, err := config.Get().Mytoken().UserSettings.Grants.APIEnableGrant(mytoken, grant)
if err != nil {
return err
}
Expand All @@ -108,7 +108,7 @@ func disableGrant(ctx *cli.Context) error {
}
grant := ctx.Args().Get(0)
mytoken := settingsOptions.MustGetToken()
res, err := config.Get().Mytoken.UserSettings.Grants.APIDisableGrant(mytoken, grant)
res, err := config.Get().Mytoken().UserSettings.Grants.APIDisableGrant(mytoken, grant)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions internal/commands/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ func introspect(_ *cli.Context) error {
}
return prettyPrintJSONString(res)
}
mytoken := config.Get().Mytoken
mToken := infoOptions.MustGetToken()
mytoken := config.Get().Mytoken()
res, err := mytoken.Tokeninfo.Introspect(mToken)
if err != nil {
return err
Expand All @@ -133,8 +133,8 @@ func history(_ *cli.Context) (err error) {
return
}
} else { // no ssh
mytoken := config.Get().Mytoken
mToken := infoOptions.MustGetToken()
mytoken := config.Get().Mytoken()
res, err = mytoken.Tokeninfo.APIHistory(mToken)
if err != nil {
return
Expand Down Expand Up @@ -186,8 +186,8 @@ func subTree(_ *cli.Context) (err error) {
return
}
} else {
mytoken := config.Get().Mytoken
mToken := infoOptions.MustGetToken()
mytoken := config.Get().Mytoken()
res, err = mytoken.Tokeninfo.APISubtokens(mToken)
if err != nil {
return err
Expand All @@ -212,8 +212,8 @@ func listMytokens(_ *cli.Context) (err error) {
return
}
} else {
mytoken := config.Get().Mytoken
mToken := infoOptions.MustGetToken()
mytoken := config.Get().Mytoken()
res, err = mytoken.Tokeninfo.APIListMytokens(mToken)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func init() {

func listProviders(_ *cli.Context) error {
defaultProvider := config.Get().DefaultProvider
instanceProviders := config.Get().Mytoken.ServerMetadata.ProvidersSupported
instanceProviders := config.Get().Mytoken().ServerMetadata.ProvidersSupported
urlMaxLen := 0
for _, ip := range instanceProviders {
length := len(ip.Issuer)
Expand Down
3 changes: 1 addition & 2 deletions internal/commands/mt.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,6 @@ func init() {
}

func obtainMTCmd(context *cli.Context) error {

mt, err := obtainMT(context)
if err != nil {
return err
Expand All @@ -479,7 +478,7 @@ func obtainMTCmd(context *cli.Context) error {
}

func obtainMT(context *cli.Context) (string, error) {
mytoken := config.Get().Mytoken
mytoken := config.Get().Mytoken()
if mtCommand.TransferCode != "" {
return mytoken.Mytoken.FromTransferCode(mtCommand.TransferCode)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/revoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ func init() {
}

func revoke(_ *cli.Context) error {
mytoken := config.Get().Mytoken
mToken := revokeCommand.MustGetToken()
mytoken := config.Get().Mytoken()
var err error
if revokeCommand.MOMID != "" {
err = mytoken.Revocation.RevokeID(revokeCommand.MOMID, mToken, "", revokeCommand.Recursive)
Expand Down
6 changes: 3 additions & 3 deletions internal/commands/sshGrant.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func initSSHGrant(parent *cli.Command) {

func listSSH(_ *cli.Context) error {
mytoken := settingsOptions.MustGetToken()
res, err := config.Get().Mytoken.UserSettings.Grants.SSH.APIGet(mytoken)
res, err := config.Get().Mytoken().UserSettings.Grants.SSH.APIGet(mytoken)
if err != nil {
return err
}
Expand Down Expand Up @@ -170,7 +170,7 @@ func addSSHKey(ctx *cli.Context) error {
if err != nil {
return err
}
res, tokenUpdate, err := config.Get().Mytoken.UserSettings.Grants.SSH.APIAdd(
res, tokenUpdate, err := config.Get().Mytoken().UserSettings.Grants.SSH.APIAdd(
mytoken, key, optName, restrictions,
caps, callbacks,
)
Expand Down Expand Up @@ -221,7 +221,7 @@ func deleteSSHKey(ctx *cli.Context) error {
return err
}
}
res, err := config.Get().Mytoken.UserSettings.Grants.SSH.APIRemove(mytoken, keyFP, key)
res, err := config.Get().Mytoken().UserSettings.Grants.SSH.APIRemove(mytoken, keyFP, key)
if err != nil {
return err
}
Expand Down
24 changes: 18 additions & 6 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import (
mytokenlib "github.com/oidc-mytoken/lib"
"github.com/oidc-mytoken/utils/httpclient"
"github.com/oidc-mytoken/utils/utils/fileutil"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)

type Config struct {
URL string `yaml:"instance"`
Mytoken *mytokenlib.MytokenServer `yaml:"-"`
mytoken *mytokenlib.MytokenServer `yaml:"-"`

DefaultProvider string `yaml:"default_provider"`
DefaultTokenCapabilities []string `yaml:"default_token_capabilities"`
Expand Down Expand Up @@ -44,6 +45,22 @@ func Get() *Config {
return conf
}

func (c *Config) Mytoken() *mytokenlib.MytokenServer {
if c.mytoken != nil {
return c.mytoken
}
mytoken, err := mytokenlib.NewMytokenServer(conf.URL)
if err != nil {
log.Fatal(errors.Wrap(err, "could not initialize mytoken server"))
}
conf.mytoken = mytoken
return mytoken
}

func (c *Config) SetMytokenServer(mytoken *mytokenlib.MytokenServer) {
conf.mytoken = mytoken
}

func load(name string, locations []string) {
data, usedLocation, err := fileutil.ReadConfigFile(name, locations)
if err != nil {
Expand All @@ -55,11 +72,6 @@ func load(name string, locations []string) {
}
conf.usedConfigDir = usedLocation
mytokenlib.SetClient(httpclient.Do().GetClient())
mytoken, err := mytokenlib.NewMytokenServer(conf.URL)
if err != nil {
log.Fatal(err)
}
conf.Mytoken = mytoken

hostname, _ := os.Hostname()
conf.Hostname = hostname
Expand Down

0 comments on commit a4221b7

Please sign in to comment.