Skip to content

Commit

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

Signed-off-by: Han Verstraete (OpenFaaS Ltd) <[email protected]>
  • Loading branch information
welteki committed May 30, 2023
1 parent 2ca3f90 commit bb174fc
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 24 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
26 changes: 14 additions & 12 deletions config/config_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,15 @@ type ConfigFile struct {
}

type AuthConfig struct {
Gateway string `yaml:"gateway,omitempty"`
Auth AuthType `yaml:"auth,omitempty"`
Token string `yaml:"token,omitempty"`
Gateway string `yaml:"gateway,omitempty"`
Auth AuthType `yaml:"auth,omitempty"`
Token string `yaml:"token,omitempty"`
Oauth2Config []ConfigEntry `yaml:"oauth,omitempty"`
}

type ConfigEntry 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 bb174fc

Please sign in to comment.