Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON schema checks #234

Merged
merged 22 commits into from
Jan 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
simplify GetSupportedControllerFromString
  • Loading branch information
rbren committed Dec 23, 2019
commit d0dc7f4b0e20c3245a0a7dfbea3e486a988a1715
20 changes: 9 additions & 11 deletions pkg/config/supportedcontrollers.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ func (s *SupportedController) UnmarshalJSON(b []byte) error {
return err
}

*s, err = GetSupportedControllerFromString(j)
if err != nil {
return err
*s = GetSupportedControllerFromString(j)
if *s == Unsupported {
return fmt.Errorf("Unsupported controller kind: %s", j)
}
return nil
}
Expand Down Expand Up @@ -124,21 +124,19 @@ func (s SupportedController) ListSupportedAPIVersions() []runtime.Object {
}

// GetSupportedControllerFromString fuzzy matches a string with a SupportedController Enum
func GetSupportedControllerFromString(str string) (SupportedController, error) {
func GetSupportedControllerFromString(str string) SupportedController {
lowerStr := strings.ToLower(str)
controller, keyFound := stringLookupForSupportedControllers[lowerStr]
if !keyFound || controller == Unsupported {
return 0, fmt.Errorf("Value ('%v') in configuration was not found in Supported Controllers: (%v)", str, strings.Join(ControllerStrings, ","))
if !keyFound {
controller = Unsupported
}
return controller, nil
return controller
}

// CheckIfKindIsConfiguredForValidation takes a kind (in string format) and checks if Polaris is configured to scan this type of controller
func (c Configuration) CheckIfKindIsConfiguredForValidation(kind string) bool {
controller, err := GetSupportedControllerFromString(kind)
// if no errors then we found the kind in supported controller types
if err == nil {
// see if the kind exists in the controllers to scan config
controller := GetSupportedControllerFromString(kind)
if controller != Unsupported {
for _, controllerToScan := range c.ControllersToScan {
if controller == controllerToScan {
return true
Expand Down
23 changes: 8 additions & 15 deletions pkg/config/supportedcontrollers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,17 @@ func TestUnmarshalSupportedControllers(t *testing.T) {

func TestMarshalSupportedControllers(t *testing.T) {
for idx, controllerString := range ControllerStrings {
controllerType, err := GetSupportedControllerFromString(controllerString)
controllerType := GetSupportedControllerFromString(controllerString)
if idx == 0 {
if err == nil {
t.Errorf("Expected first element (%s) to fail as a non-valid supported controller. Reserved for 'Unsupported'", controllerString)
}
} else if err != nil {
t.Errorf("Unable to take the configured string (%s) and convert into Enum; Error: (%s)", controllerString, err)
assert.Equal(t, SupportedController(0), controllerType)
} else {
assert.NotEqual(t, SupportedController(0), controllerType)
}

object := checkMarshal{
Controllers: []SupportedController{controllerType},
}
_, err = json.Marshal(object)
_, err := json.Marshal(object)
if idx == 0 {
if err == nil {
t.Errorf("Expected (%s) to throw an error. Reserving the first element in the enum to be an invalid config", controllerString)
Expand All @@ -77,10 +75,8 @@ func TestMarshalSupportedControllers(t *testing.T) {
func TestCheckIfControllerKindIsConfiguredForValidation(t *testing.T) {
config := Configuration{}
for _, controllerString := range ControllerStrings[1:] {
controllerEnum, err := GetSupportedControllerFromString(controllerString)
if err != nil {
t.Errorf("Expected controller string (%s) to be convertable into enum: (%s)", controllerString, err)
}
controllerEnum := GetSupportedControllerFromString(controllerString)
assert.NotEqual(t, SupportedController(0), controllerEnum)
config.ControllersToScan = append(config.ControllersToScan, controllerEnum)
}

Expand Down Expand Up @@ -116,10 +112,7 @@ func TestGetSupportedControllerFromString(t *testing.T) {
}

for inputString, expectedType := range fixture {
resolvedType, err := GetSupportedControllerFromString(inputString)
if expectedType == Unsupported && err == nil {
t.Errorf("Expected (%s) to resolve to an unsupported type and throw an error.", inputString)
}
resolvedType := GetSupportedControllerFromString(inputString)
assert.Equal(t, expectedType, resolvedType, fmt.Sprintf("Expected (%s) to return (%s) controller type.", inputString, expectedType))
}
}
8 changes: 4 additions & 4 deletions pkg/webhook/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ func (v *Validator) Handle(ctx context.Context, req types.Request) types.Respons
}

// We should never hit this case unless something is misconfiured in CheckIfKindIsConfiguredForValidation
controllerType, err := config.GetSupportedControllerFromString(req.AdmissionRequest.Kind.Kind)
if err != nil {
msg := fmt.Errorf("Unexpected error occurred. Expected Kind to be a supported type (%s)", req.AdmissionRequest.Kind.Kind)
controllerType := config.GetSupportedControllerFromString(req.AdmissionRequest.Kind.Kind)
if controllerType == config.Unsupported {
msg := fmt.Errorf("Expected Kind (%s) to be a supported type", req.AdmissionRequest.Kind.Kind)
logrus.Error(msg)
return admission.ErrorResponse(http.StatusInternalServerError, err)
return admission.ErrorResponse(http.StatusInternalServerError, msg)
}

// For each type, perform the scan
Expand Down