Skip to content

Commit

Permalink
Add options section to auth config
Browse files Browse the repository at this point in the history
Add on options field to the auth config. This is required by the
Pro plugin to persist the settings for different oauth2 login flows.

Signed-off-by: Han Verstraete (OpenFaaS Ltd) <han@openfaas.com>
  • Loading branch information
welteki authored and alexellis committed Jun 1, 2023
1 parent 172b7cd commit d7f8ff9
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 21 deletions.
9 changes: 7 additions & 2 deletions commands/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,16 @@ func runLogin(cmd *cobra.Command, args []string) error {
}

token := config.EncodeAuth(username, password)
if err := config.UpdateAuthConfig(gateway, token, config.BasicAuthType); err != nil {
authConfig := config.AuthConfig{
Gateway: gateway,
Token: token,
Auth: config.BasicAuthType,
}
if err := config.UpdateAuthConfig(authConfig); err != nil {
return err
}

authConfig, err := config.LookupAuthConfig(gateway)
authConfig, err = config.LookupAuthConfig(gateway)
if err != nil {
return err
}
Expand Down
20 changes: 11 additions & 9 deletions config/config_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ type AuthConfig struct {
Gateway string `yaml:"gateway,omitempty"`
Auth AuthType `yaml:"auth,omitempty"`
Token string `yaml:"token,omitempty"`
Options []Option `yaml:"options,omitempty"`
}

type Option struct {
Name string `yaml:"name"`
Value string `yaml:"value"`
}

// New initializes a config file for the given file path
Expand Down Expand Up @@ -211,7 +217,9 @@ func DecodeAuth(input string) (string, string, error) {
}

// UpdateAuthConfig creates or updates the username and password for a given gateway
func UpdateAuthConfig(gateway, token string, authType AuthType) error {
func UpdateAuthConfig(authConfig AuthConfig) error {
gateway := authConfig.Gateway

_, err := url.ParseRequestURI(gateway)
if err != nil || len(gateway) < 1 {
return fmt.Errorf("invalid gateway URL")
Expand All @@ -231,12 +239,6 @@ func UpdateAuthConfig(gateway, token string, authType AuthType) error {
return err
}

auth := AuthConfig{
Gateway: gateway,
Auth: authType,
Token: token,
}

index := -1
for i, v := range cfg.AuthConfigs {
if gateway == v.Gateway {
Expand All @@ -246,9 +248,9 @@ func UpdateAuthConfig(gateway, token string, authType AuthType) error {
}

if index == -1 {
cfg.AuthConfigs = append(cfg.AuthConfigs, auth)
cfg.AuthConfigs = append(cfg.AuthConfigs, authConfig)
} else {
cfg.AuthConfigs[index] = auth
cfg.AuthConfigs[index] = authConfig
}

if err := cfg.save(); err != nil {
Expand Down
60 changes: 50 additions & 10 deletions config/config_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ func Test_LookupAuthConfig_GatewayWithNoConfig(t *testing.T) {
p := "some pass"
gatewayURL := strings.TrimRight("http://openfaas.test/", "/")
token := EncodeAuth(u, p)
err = UpdateAuthConfig(gatewayURL, token, BasicAuthType)
err = UpdateAuthConfig(AuthConfig{
Gateway: gatewayURL,
Token: token,
Auth: BasicAuthType,
})
if err != nil {
t.Fatalf("unexpected error when updating auth config: %s", err)
}
Expand Down Expand Up @@ -77,7 +81,11 @@ func Test_UpdateAuthConfig_Insert(t *testing.T) {
p := "some pass"
gatewayURL := strings.TrimRight("http://openfaas.test/", "/")
token := EncodeAuth(u, p)
err = UpdateAuthConfig(gatewayURL, token, BasicAuthType)
err = UpdateAuthConfig(AuthConfig{
Gateway: gatewayURL,
Token: token,
Auth: BasicAuthType,
})
if err != nil {
t.Fatalf("unexpected error when updating auth config: %s", err)
}
Expand Down Expand Up @@ -113,7 +121,11 @@ func Test_UpdateAuthConfig_Update(t *testing.T) {
p := "pass"
gatewayURL := strings.TrimRight("http://openfaas.test/", "/")
token := EncodeAuth(u, p)
err = UpdateAuthConfig(gatewayURL, token, BasicAuthType)
err = UpdateAuthConfig(AuthConfig{
Gateway: gatewayURL,
Token: token,
Auth: BasicAuthType,
})
if err != nil {
t.Fatalf("unexpected error when updating auth config: %s", err)
}
Expand All @@ -134,7 +146,11 @@ func Test_UpdateAuthConfig_Update(t *testing.T) {
u = "admin2"
p = "pass2"
token = EncodeAuth(u, p)
err = UpdateAuthConfig(gatewayURL, token, BasicAuthType)
err = UpdateAuthConfig(AuthConfig{
Gateway: gatewayURL,
Token: token,
Auth: BasicAuthType,
})
if err != nil {
t.Fatalf("unexpected error when updating auth config: %s", err)
}
Expand All @@ -156,7 +172,11 @@ func Test_UpdateAuthConfig_Update(t *testing.T) {

func Test_UpdateAuthConfig_InvaidGatewayURL(t *testing.T) {
gateway := "http//test.test"
err := UpdateAuthConfig(gateway, "a", "b")
err := UpdateAuthConfig(AuthConfig{
Gateway: gateway,
Token: "a",
Auth: "b",
})
if err == nil {
t.Errorf("Error was not returned")
}
Expand All @@ -169,7 +189,11 @@ func Test_UpdateAuthConfig_InvaidGatewayURL(t *testing.T) {

func Test_UpdateAuthConfig_EmptyGatewayURL(t *testing.T) {
gateway := ""
err := UpdateAuthConfig(gateway, "a", "b")
err := UpdateAuthConfig(AuthConfig{
Gateway: gateway,
Token: "a",
Auth: "b",
})
if err == nil {
t.Errorf("Error was not returned")
}
Expand Down Expand Up @@ -235,13 +259,21 @@ func Test_RemoveAuthConfig(t *testing.T) {
p := "pass"
token := EncodeAuth(u, p)
gatewayURL := strings.TrimRight("http://openfaas.test/", "/")
err = UpdateAuthConfig(gatewayURL, token, BasicAuthType)
err = UpdateAuthConfig(AuthConfig{
Gateway: gatewayURL,
Token: token,
Auth: BasicAuthType,
})
if err != nil {
t.Fatalf("unexpected error when updating auth config: %s", err)
}

gatewayURL2 := strings.TrimRight("http://openfaas.test2/", "/")
err = UpdateAuthConfig(gatewayURL2, token, BasicAuthType)
err = UpdateAuthConfig(AuthConfig{
Gateway: gatewayURL2,
Token: token,
Auth: BasicAuthType,
})
if err != nil {
t.Fatalf("unexpected error when updating auth config: %s", err)
}
Expand Down Expand Up @@ -296,7 +328,11 @@ func Test_RemoveAuthConfig_WithUnknownGateway(t *testing.T) {
p := "pass"
token := EncodeAuth(u, p)
gatewayURL := strings.TrimRight("http://openfaas.test/", "/")
err = UpdateAuthConfig(gatewayURL, token, BasicAuthType)
err = UpdateAuthConfig(AuthConfig{
Gateway: gatewayURL,
Token: token,
Auth: BasicAuthType,
})
if err != nil {
t.Fatalf("unexpected error when updating auth config: %s", err)
}
Expand Down Expand Up @@ -324,7 +360,11 @@ func Test_UpdateAuthConfig_Oauth2Insert(t *testing.T) {

token := "somebase64encodedstring"
gatewayURL := strings.TrimRight("http://openfaas.test/", "/")
err = UpdateAuthConfig(gatewayURL, token, Oauth2AuthType)
err = UpdateAuthConfig(AuthConfig{
Gateway: gatewayURL,
Token: token,
Auth: Oauth2AuthType,
})
if err != nil {
t.Fatalf("unexpected error when updating auth config: %s", err)
}
Expand Down

0 comments on commit d7f8ff9

Please sign in to comment.