Skip to content

Commit

Permalink
http-gw: dont show m2mOAuthclient when it is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
jkralik committed Jul 4, 2024
1 parent 187615a commit ce697ce
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"TEST_CLOUD_SID": "adebc667-1f2b-41e3-bf5c-6d6eabc68cc6",
"TEST_OAUTH_SERVER_ID_TOKEN_PRIVATE_KEY": "${workspaceFolder}/.tmp/privKeys/idTokenKey.pem",
"TEST_OAUTH_SERVER_ACCESS_TOKEN_PRIVATE_KEY": "${workspaceFolder}/.tmp/privKeys/accessTokenKey.pem",
"M2M_OAUTH_SERVER_ACCESS_TOKEN_PRIVATE_KEY": "${workspaceFolder}/.tmp/privKeys/m2mAccessTokenKey.pem",
"M2M_OAUTH_SERVER_PRIVATE_KEY": "${workspaceFolder}/.tmp/privKeys/m2mAccessTokenKey.pem",
"TEST_HTTP_GW_WWW_ROOT": "${workspaceFolder}/.tmp/usr/local/www",
"TEST_MEMORY_COAP_GATEWAY_NUM_DEVICES": "1",
"TEST_MEMORY_COAP_GATEWAY_NUM_RESOURCES": "1",
Expand Down
4 changes: 1 addition & 3 deletions charts/plgd-hub/templates/http-gateway/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ data:
providerName: {{ .name | quote }}
{{- end }}
{{- end }}
{{- if $.Values.m2moauthserver.enabled }}
{{- $m2mClient := include "plgd-hub.m2moauthserver.getJwtPrivateKeyClient" $.Values.m2moauthserver.oauthSigner.clients | fromYaml }}
{{- $m2mClient := include "plgd-hub.m2moauthserver.getJwtPrivateKeyClient" $ | fromYaml }}
{{- if $m2mClient }}
m2mOAuthClient:
authority: {{.ui.webConfiguration.m2mOAuthClient.authority | default (include "plgd-hub.m2moauthserver.uri" $) | quote }}
Expand Down Expand Up @@ -132,7 +131,6 @@ data:
{{- end }}
{{- end }}
{{- end }}
{{- end }}
visibility:
mainSidebar:
certificates : {{ .ui.webConfiguration.visibility.mainSidebar.certificates }}
Expand Down
5 changes: 4 additions & 1 deletion charts/plgd-hub/templates/m2m-oauth-server/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,17 @@ app.kubernetes.io/instance: {{ .Release.Name }}


{{- define "plgd-hub.m2moauthserver.getJwtPrivateKeyClient" -}}
{{- $ := . -}}
{{- $clientID := dict }}
{{- range . }}
{{- if include "plgd-hub.m2moauthserver.enabled" $ }}
{{- range $.Values.m2moauthserver.oauthSigner.clients }}
{{- if .jwtPrivateKey }}
{{- if .jwtPrivateKey.enabled }}
{{- $clientID = . }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
{{- $clientID | toYaml }}
{{- end }}

Expand Down
11 changes: 10 additions & 1 deletion http-gateway/service/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ type OAuthClient struct {
}

func (c *OAuthClient) ToProto() *pb.OAuthClient {
if c == nil {
return nil
}
return &pb.OAuthClient{
ClientId: c.ClientID,
Audience: c.Audience,
Expand All @@ -130,6 +133,9 @@ func (c *OAuthClient) ToProto() *pb.OAuthClient {
}

func (c *OAuthClient) Validate() error {
if c == nil {
return nil
}
if c.ClientID == "" {
return fmt.Errorf("clientID('%v')", c.ClientID)
}
Expand Down Expand Up @@ -189,7 +195,7 @@ type WebConfiguration struct {
DeviceProvisioningService string `yaml:"deviceProvisioningService" json:"deviceProvisioningService"`
WebOAuthClient OAuthClient `yaml:"webOAuthClient" json:"webOauthClient"`
DeviceOAuthClient OAuthClient `yaml:"deviceOAuthClient" json:"deviceOauthClient"`
M2MOAuthClient OAuthClient `yaml:"m2mOAuthClient" json:"m2mOauthClient"`
M2MOAuthClient *OAuthClient `yaml:"m2mOAuthClient" json:"m2mOauthClient"`
Visibility VisibilityConfig `yaml:"visibility" json:"visibility"`
}

Expand All @@ -203,6 +209,9 @@ func (c *WebConfiguration) Validate() error {
if err := c.DeviceOAuthClient.Validate(); err != nil {
return fmt.Errorf("deviceOAuthClient.%w", err)
}
if err := c.M2MOAuthClient.Validate(); err != nil {
return fmt.Errorf("deviceOAuthClient.%w", err)
}
return nil
}

Expand Down
55 changes: 55 additions & 0 deletions http-gateway/service/getHubConfiguration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,58 @@ func TestRequestHandlerGetHubConfiguration(t *testing.T) {
})
}
}

func TestRequestHandlerGetHubConfigurationWithoutM2MOAuthClient(t *testing.T) {
rdCfg := rdTest.MakeConfig(t)
rdCfg.ExposedHubConfiguration.Authority = "https://" + config.OAUTH_SERVER_HOST + "?escape=test&test=escape"
httpCfg := httpgwTest.MakeConfig(t, true)
httpCfg.UI.WebConfiguration.M2MOAuthClient = nil
expected := rdCfg.ExposedHubConfiguration.ToProto(config.HubID())
expected.CurrentTime = 0
expected.WebOauthClient = httpCfg.UI.WebConfiguration.WebOAuthClient.ToProto()
expected.DeviceOauthClient = httpCfg.UI.WebConfiguration.DeviceOAuthClient.ToProto()
expected.M2MOauthClient = nil
expected.HttpGatewayAddress = httpCfg.UI.WebConfiguration.HTTPGatewayAddress
expected.Ui = &pb.UIConfiguration{
Visibility: httpCfg.UI.WebConfiguration.Visibility.ToProto(),
DeviceProvisioningService: httpCfg.UI.WebConfiguration.DeviceProvisioningService,
}
tests := []struct {
name string
accept string
want *pb.HubConfigurationResponse
}{
{
name: "valid",
want: expected,
},
{
name: "valid configuration",
accept: pkgHttp.ApplicationProtoJsonContentType,
want: expected,
},
}

ctx, cancel := context.WithTimeout(context.Background(), config.TEST_TIMEOUT)
defer cancel()

tearDown := service.SetUp(ctx, t, service.WithRDConfig(rdCfg))
defer tearDown()

shutdownHttp := httpgwTest.New(t, httpCfg)
defer shutdownHttp()

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
request := httpgwTest.NewRequest(http.MethodGet, uri.HubConfiguration, nil).Accept(tt.accept).Build()
resp := httpgwTest.HTTPDo(t, request)
defer func() {
_ = resp.Body.Close()
}()
var got pb.HubConfigurationResponse
err := httpTest.Unmarshal(resp.StatusCode, resp.Body, &got)
require.NoError(t, err)
pbTest.CmpHubConfigurationResponse(t, tt.want, &got)
})
}
}
2 changes: 1 addition & 1 deletion http-gateway/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func MakeWebConfigurationConfig() service.WebConfiguration {
ProviderName: config.DEVICE_PROVIDER,
GrantType: "authorization_code",
},
M2MOAuthClient: service.OAuthClient{
M2MOAuthClient: &service.OAuthClient{
Authority: testHttp.HTTPS_SCHEME + config.M2M_OAUTH_SERVER_HTTP_HOST,
ClientID: config.M2M_OAUTH_PRIVATE_KEY_CLIENT_ID,
Audience: config.OAUTH_MANAGER_AUDIENCE,
Expand Down

0 comments on commit ce697ce

Please sign in to comment.