Skip to content

Commit

Permalink
in memory improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jakedoublev committed Sep 12, 2024
1 parent 93469b2 commit 5cc1b5a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 25 deletions.
5 changes: 5 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func NewHandler(c *cli.Cli) handlers.Handler {
tlsNoVerify := c.FlagHelper.GetOptionalBool("tls-no-verify")
withClientCreds := c.FlagHelper.GetOptionalString("with-client-creds")
withClientCredsFile := c.FlagHelper.GetOptionalString("with-client-creds-file")
var inMemoryProfile bool

// if global flags are set then validate and create a temporary profile in memory
var cp *profiles.ProfileStore
Expand Down Expand Up @@ -108,6 +109,7 @@ func NewHandler(c *cli.Cli) handlers.Handler {
cli.ExitWithError("Failed to get client credentials", err)
}

inMemoryProfile = true
profile, err = profiles.New(profiles.WithInMemoryStore())
if err != nil || profile == nil {
cli.ExitWithError("Failed to initialize a temporary profile", err)
Expand Down Expand Up @@ -142,6 +144,9 @@ func NewHandler(c *cli.Cli) handlers.Handler {
if errors.Is(err, auth.ErrPlatformConfigNotFound) {
cli.ExitWithError(fmt.Sprintf("Failed to get platform configuration. Is the platform accepting connections at '%s'?", cp.GetEndpoint()), nil)
}
if inMemoryProfile {
cli.ExitWithError("Failed to authenticate with flag-provided client credentials", err)
}
if errors.Is(err, auth.ErrProfileCredentialsNotFound) {
cli.ExitWithWarning("Profile missing credentials. Please login or add client credentials.")
}
Expand Down
37 changes: 18 additions & 19 deletions pkg/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func GetClientCreds(endpoint string, file string, credsJSON []byte) (ClientCrede
func getPlatformConfiguration(endpoint, publicClientID string, tlsNoVerify bool) (platformConfiguration, error) {
c := platformConfiguration{}

e, err := utils.NormalizeEndpoint(endpoint)
normalized, err := utils.NormalizeEndpoint(endpoint)
if err != nil {
return c, err
}
Expand All @@ -93,42 +93,41 @@ func getPlatformConfiguration(endpoint, publicClientID string, tlsNoVerify bool)
opts = append(opts, sdk.WithInsecureSkipVerifyConn())
}

if e.Scheme == "http" {
if normalized.Scheme == "http" {
opts = append(opts, sdk.WithInsecurePlaintextConn())
}

s, err := sdk.New(e.String(), opts...)
s, err := sdk.New(normalized.String(), opts...)
if err != nil {
return c, err
}

errs := []error{}
c.issuer, err = s.PlatformConfiguration.Issuer()
if err != nil {
errs = append(errs, errors.Join(err, sdk.ErrPlatformIssuerNotFound))
var e error
c.issuer, e = s.PlatformConfiguration.Issuer()
if e != nil {
err = errors.Join(err, sdk.ErrPlatformIssuerNotFound)
}

c.authzEndpoint, err = s.PlatformConfiguration.AuthzEndpoint()
if err != nil {
errs = append(errs, errors.Join(err, sdk.ErrPlatformAuthzEndpointNotFound))
c.authzEndpoint, e = s.PlatformConfiguration.AuthzEndpoint()
if e != nil {
err = errors.Join(err, sdk.ErrPlatformAuthzEndpointNotFound)
}

c.tokenEndpoint, err = s.PlatformConfiguration.TokenEndpoint()
if err != nil {
errs = append(errs, errors.Join(err, sdk.ErrPlatformTokenEndpointNotFound))
c.tokenEndpoint, e = s.PlatformConfiguration.TokenEndpoint()
if e != nil {
err = errors.Join(err, sdk.ErrPlatformTokenEndpointNotFound)
}

c.publicClientID = publicClientID
if c.publicClientID == "" {
c.publicClientID, err = s.PlatformConfiguration.PublicClientID()
if err != nil {
errs = append(errs, errors.Join(err, sdk.ErrPlatformPublicClientIDNotFound))
c.publicClientID, e = s.PlatformConfiguration.PublicClientID()
if e != nil {
err = errors.Join(err, sdk.ErrPlatformPublicClientIDNotFound)
}
}

if len(errs) > 0 {
errs = append([]error{ErrProfileCredentialsNotFound}, errs...)
return c, errors.Join(errs...)
if err != nil {
return c, errors.Join(err, ErrProfileCredentialsNotFound)
}

return c, nil
Expand Down
15 changes: 9 additions & 6 deletions pkg/profiles/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const (
)

type profileConfig struct {
driver string
driver ProfileDriver
}

type Profile struct {
Expand All @@ -30,12 +30,15 @@ type CurrentProfileStore struct {
}

const (
PROFILE_DRIVER_KEYRING = "keyring"
PROFILE_DRIVER_IN_MEMORY = "in-memory"
PROFILE_DRIVER_DEFAULT = PROFILE_DRIVER_KEYRING
PROFILE_DRIVER_KEYRING ProfileDriver = "keyring"
PROFILE_DRIVER_IN_MEMORY ProfileDriver = "in-memory"
PROFILE_DRIVER_DEFAULT = PROFILE_DRIVER_KEYRING
)

type profileConfigVariadicFunc func(profileConfig) profileConfig
type (
profileConfigVariadicFunc func(profileConfig) profileConfig
ProfileDriver string
)

func WithInMemoryStore() profileConfigVariadicFunc {
return func(c profileConfig) profileConfig {
Expand All @@ -51,7 +54,7 @@ func WithKeyringStore() profileConfigVariadicFunc {
}
}

func newStoreFactory(driver string) NewStoreInterface {
func newStoreFactory(driver ProfileDriver) NewStoreInterface {
switch driver {
case PROFILE_DRIVER_KEYRING:
return NewKeyringStore
Expand Down

0 comments on commit 5cc1b5a

Please sign in to comment.