Skip to content

Commit

Permalink
fix: check exists of values in maps
Browse files Browse the repository at this point in the history
  • Loading branch information
duffney committed Sep 4, 2024
1 parent 8fc826e commit 53fe1ec
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 13 deletions.
10 changes: 7 additions & 3 deletions pkg/keymanagementprovider/refresh/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,16 @@ func Register(name string, factory RefresherFactory) {

// CreateRefresherFromConfig creates a new instance of the refresher using the provided configuration
func CreateRefresherFromConfig(refresherConfig map[string]interface{}) (Refresher, error) {
refresherType, ok := refresherConfig["type"].(string)
refresherTypeValue, exists := refresherConfig["type"]
if !exists {
return nil, fmt.Errorf("refresherType not found in config")
}
refresherType, ok := refresherTypeValue.(string)
if !ok {
return nil, fmt.Errorf("refresher type is not a string")
return nil, fmt.Errorf("refresherType is not a string")
}
if !ok || refresherType == "" {
return nil, fmt.Errorf("refresher type cannot be empty")
return nil, fmt.Errorf("refresherType cannot be empty")
}
factory, ok := refresherFactories[refresherType]
if !ok {
Expand Down
35 changes: 27 additions & 8 deletions pkg/keymanagementprovider/refresh/kubeRefresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,26 +100,45 @@ func (kr *KubeRefresher) GetStatus() interface{} {

// Create creates a new KubeRefresher instance
func (kr *KubeRefresher) Create(config map[string]interface{}) (Refresher, error) {
provider, ok := config["provider"].(kmp.KeyManagementProvider)
if !ok {
providerValue, exists := config["provider"]
if !exists {
return nil, fmt.Errorf("provider is required in config")
}

providerType, ok := config["providerType"].(string)
provider, ok := providerValue.(kmp.KeyManagementProvider)
if !ok {
return nil, fmt.Errorf("request is required in config")
return nil, fmt.Errorf("provider is not of type KeyManagementProvider")
}

provierTypeValue, exists := config["providerType"]
if !exists {
return nil, fmt.Errorf("providerType is required in config")
}

providerRefreshInterval, ok := config["providerRefreshInterval"].(string)
providerType, ok := provierTypeValue.(string)
if !ok {
return nil, fmt.Errorf("refreshInterval is required in config")
return nil, fmt.Errorf("providerType is not of type string")
}

providerRefreshIntervaleValue, exists := config["providerRefreshInterval"]
if !exists {
return nil, fmt.Errorf("providerRefreshInterval is required in config")
}

resource, ok := config["resource"].(string)
providerRefreshInterval, ok := providerRefreshIntervaleValue.(string)
if !ok {
return nil, fmt.Errorf("providerRefreshInterval is not of type string")
}

resourceValue, exists := config["resource"]
if !exists {
return nil, fmt.Errorf("resource is required in config")
}

resource, ok := resourceValue.(string)
if !ok {
return nil, fmt.Errorf("resource is not of type string")
}

return &KubeRefresher{
Provider: provider,
ProviderType: providerType,
Expand Down
48 changes: 46 additions & 2 deletions pkg/keymanagementprovider/refresh/kubeRefresh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func TestKubeRefresher_Create(t *testing.T) {
"resource": "kmpname",
},
expectedError: true,
expectedErrMsg: "request is required in config",
expectedErrMsg: "providerType is required in config",
},
{
name: "ProviderRefreshIntervalMissing",
Expand All @@ -192,7 +192,7 @@ func TestKubeRefresher_Create(t *testing.T) {
"resource": "kmpname",
},
expectedError: true,
expectedErrMsg: "refreshInterval is required in config",
expectedErrMsg: "providerRefreshInterval is required in config",
},
{
name: "ResourceMissing",
Expand All @@ -204,6 +204,50 @@ func TestKubeRefresher_Create(t *testing.T) {
expectedError: true,
expectedErrMsg: "resource is required in config",
},
{
name: "ProviderInvalid",
config: map[string]interface{}{
"provider": 123,
"providerType": "inline",
"providerRefreshInterval": "",
"resource": "kmpname",
},
expectedError: true,
expectedErrMsg: "provider is not of type KeyManagementProvider",
},
{
name: "ProviderTypeInvalid",
config: map[string]interface{}{
"provider": provider,
"providerType": 123,
"providerRefreshInterval": "",
"resource": "kmpname",
},
expectedError: true,
expectedErrMsg: "providerType is not of type string",
},
{
name: "ProviderRefreshIntervalInvalid",
config: map[string]interface{}{
"provider": provider,
"providerType": "inline",
"providerRefreshInterval": 123,
"resource": "kmpname",
},
expectedError: true,
expectedErrMsg: "providerRefreshInterval is not of type string",
},
{
name: "ResourceInvalid",
config: map[string]interface{}{
"provider": provider,
"providerType": "inline",
"providerRefreshInterval": "",
"resource": 123,
},
expectedError: true,
expectedErrMsg: "resource is not of type string",
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 53fe1ec

Please sign in to comment.