Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions pkg/config/control_plane.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ func (s *SharedSSOConfig) UnmarshalJSON(data []byte) error {
return err
}

provider := m["provider"].(string)
provider, ok := m["provider"].(string)
if !ok {
return fmt.Errorf("provider field in SharedSSOConfig must be a string, got %T", m["provider"])
}
v, ok := model.ProjectSSOConfig_Provider_value[provider]
if !ok {
return fmt.Errorf("unsupported provider %s", provider)
Expand All @@ -88,7 +91,11 @@ func (s *SharedSSOConfig) UnmarshalJSON(data []byte) error {
if !ok {
return fmt.Errorf("name field in SharedSSOConfig is required")
}
s.Name = name.(string)
nameStr, ok := name.(string)
if !ok {
return fmt.Errorf("name field in SharedSSOConfig must be a string, got %T", name)
}
s.Name = nameStr
delete(m, "name")

data, err := json.Marshal(m)
Expand Down
42 changes: 42 additions & 0 deletions pkg/config/shared_sso_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2024 The PipeCD Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package config

import (
"encoding/json"
"testing"
)

// Each body previously panicked in UnmarshalJSON, which decodes into
// map[string]interface{} and asserts on the lookups.
func TestSharedSSOConfigMalformed(t *testing.T) {
for name, body := range map[string]string{
"provider missing": `{"name":"github"}`,
"provider a number": `{"name":"github","provider":42}`,
"name a number": `{"name":42,"provider":"GITHUB"}`,
} {
t.Run(name, func(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Fatalf("panicked on user config: %v", r)
}
}()
var c SharedSSOConfig
if err := json.Unmarshal([]byte(body), &c); err == nil {
t.Fatal("want an error, got nil")
}
})
}
}
11 changes: 9 additions & 2 deletions pkg/configv1/control_plane.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ func (s *SharedSSOConfig) UnmarshalJSON(data []byte) error {
return err
}

provider := m["provider"].(string)
provider, ok := m["provider"].(string)
if !ok {
return fmt.Errorf("provider field in SharedSSOConfig must be a string, got %T", m["provider"])
}
Comment thread
omlahore marked this conversation as resolved.
v, ok := model.ProjectSSOConfig_Provider_value[provider]
if !ok {
return fmt.Errorf("unsupported provider %s", provider)
Expand All @@ -88,7 +91,11 @@ func (s *SharedSSOConfig) UnmarshalJSON(data []byte) error {
if !ok {
return fmt.Errorf("name field in SharedSSOConfig is required")
}
s.Name = name.(string)
nameStr, ok := name.(string)
if !ok {
return fmt.Errorf("name field in SharedSSOConfig must be a string, got %T", name)
}
s.Name = nameStr
delete(m, "name")

data, err := json.Marshal(m)
Expand Down
42 changes: 42 additions & 0 deletions pkg/configv1/shared_sso_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2024 The PipeCD Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package config

import (
"encoding/json"
"testing"
)

// Each body previously panicked in UnmarshalJSON, which decodes into
// map[string]interface{} and asserts on the lookups.
func TestSharedSSOConfigMalformed(t *testing.T) {
for name, body := range map[string]string{
"provider missing": `{"name":"github"}`,
"provider a number": `{"name":"github","provider":42}`,
"name a number": `{"name":42,"provider":"GITHUB"}`,
} {
t.Run(name, func(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Fatalf("panicked on user config: %v", r)
}
}()
var c SharedSSOConfig
if err := json.Unmarshal([]byte(body), &c); err == nil {
t.Fatal("want an error, got nil")
}
})
}
}