Skip to content

Commit

Permalink
APISHI-2361 Add support for Get/Update API Shield Schema Validation S…
Browse files Browse the repository at this point in the history
…ettings

This change adds support for the following API Shield related endpoints related to API Shield Schema Validation Settings:

- Retrieve API Shield Schema Validation Settings
- Update All API Shield Schema Validation Settings
- Update API Shield Schema Validation Settings
  • Loading branch information
djhworld committed Oct 11, 2023
1 parent aed34a4 commit c52383a
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/1418.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
api_shield_schema: Add support for Get/Update API Shield Schema Validation Settings
```
81 changes: 81 additions & 0 deletions api_shield_schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,84 @@ func (api *API) UpdateAPIShieldSchema(ctx context.Context, rc *ResourceContainer

return &asResponse.Result, nil
}

// Schema Validation Settings

// APIShieldSchemaValidationSettings represents zone level schema validation settings for
// API Shield Schema Validation 2.0.
type APIShieldSchemaValidationSettings struct {
// DefaultMitigationAction is the mitigation to apply when there is no operation-level
// mitigation action defined
DefaultMitigationAction string `json:"validation_default_mitigation_action" url:"-"`
// OverrideMitigationAction when set, will apply to all requests regardless of
// zone-level/operation-level setting
OverrideMitigationAction *string `json:"validation_override_mitigation_action" url:"-"`
}

// UpdateAPIShieldSchemaValidationSettingsParams represents the parameters to pass to update certain fields
// on schema validation settings on the zone
//
// API documentation: https://developers.cloudflare.com/api/operations/api-shield-schema-validation-patch-zone-level-settings
type UpdateAPIShieldSchemaValidationSettingsParams struct {
// DefaultMitigationAction is the mitigation to apply when there is no operation-level
// mitigation action defined
//
// passing a `nil` value will have no effect on this setting
DefaultMitigationAction *string `json:"validation_default_mitigation_action" url:"-"`

// OverrideMitigationAction when set, will apply to all requests regardless of
// zone-level/operation-level setting
//
// passing a `nil` value will have no effect on this setting
OverrideMitigationAction *string `json:"validation_override_mitigation_action" url:"-"`
}

// APIShieldSchemaValidationSettingsResponse represents the response from the GET api_gateway/settings/schema_validation endpoint.
type APIShieldSchemaValidationSettingsResponse struct {
Result APIShieldSchemaValidationSettings `json:"result"`
Response
}

// GetAPIShieldSchemaValidationSettings retrieves zone level schema validation settings
//
// API documentation: https://developers.cloudflare.com/api/operations/api-shield-schema-validation-retrieve-zone-level-settings
func (api *API) GetAPIShieldSchemaValidationSettings(ctx context.Context, rc *ResourceContainer) (*APIShieldSchemaValidationSettings, error) {
path := fmt.Sprintf("/zones/%s/api_gateway/settings/schema_validation", rc.Identifier)

uri := buildURI(path, nil)

res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, err
}

var asResponse APIShieldSchemaValidationSettingsResponse
err = json.Unmarshal(res, &asResponse)
if err != nil {
return nil, fmt.Errorf("%s: %w", errUnmarshalError, err)
}

return &asResponse.Result, nil
}

// UpdateAPIShieldSchemaValidationSettings updates certain fields on zone level schema validation settings
//
// API documentation: https://developers.cloudflare.com/api/operations/api-shield-schema-validation-patch-zone-level-settings
func (api *API) UpdateAPIShieldSchemaValidationSettings(ctx context.Context, rc *ResourceContainer, params UpdateAPIShieldSchemaValidationSettingsParams) (*APIShieldSchemaValidationSettings, error) {
path := fmt.Sprintf("/zones/%s/api_gateway/settings/schema_validation", rc.Identifier)

uri := buildURI(path, params)

res, err := api.makeRequestContext(ctx, http.MethodPatch, uri, params)
if err != nil {
return nil, err
}

var asResponse APIShieldSchemaValidationSettingsResponse
err = json.Unmarshal(res, &asResponse)
if err != nil {
return nil, fmt.Errorf("%s: %w", errUnmarshalError, err)
}

return &asResponse.Result, nil
}
78 changes: 78 additions & 0 deletions api_shield_schemas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,3 +502,81 @@ func TestMustProvideSchemaID(t *testing.T) {
err = client.DeleteAPIShieldSchema(context.Background(), ZoneIdentifier(testZoneID), DeleteAPIShieldSchemaParams{})
require.ErrorContains(t, err, "schema ID must be provided")
}

func TestGetAPIShieldSchemaValidationSettings(t *testing.T) {
endpoint := fmt.Sprintf("/zones/%s/api_gateway/settings/schema_validation", testZoneID)
response := `{
"success" : true,
"errors": [],
"messages": [],
"result": {
"validation_default_mitigation_action": "log",
"validation_override_mitigation_action": "none"
}
}`

setup()
t.Cleanup(teardown)
handler := func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method)
require.Empty(t, r.URL.Query())
w.Header().Set("content-type", "application/json")
fmt.Fprint(w, response)
}

mux.HandleFunc(endpoint, handler)

actual, err := client.GetAPIShieldSchemaValidationSettings(
context.Background(),
ZoneIdentifier(testZoneID),
)

none := "none"
expected := &APIShieldSchemaValidationSettings{
DefaultMitigationAction: "log",
OverrideMitigationAction: &none,
}

if assert.NoError(t, err) {
assert.Equal(t, expected, actual)
}
}

func TestUpdateAPIShieldSchemaValidationSettings(t *testing.T) {
endpoint := fmt.Sprintf("/zones/%s/api_gateway/settings/schema_validation", testZoneID)
response := `{
"success" : true,
"errors": [],
"messages": [],
"result": {
"validation_default_mitigation_action": "log",
"validation_override_mitigation_action": null
}
}`

setup()
t.Cleanup(teardown)
handler := func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, http.MethodPatch, r.Method, "Expected method 'PATCH', got %s", r.Method)
require.Empty(t, r.URL.Query())
w.Header().Set("content-type", "application/json")
fmt.Fprint(w, response)
}

mux.HandleFunc(endpoint, handler)

actual, err := client.UpdateAPIShieldSchemaValidationSettings(
context.Background(),
ZoneIdentifier(testZoneID),
UpdateAPIShieldSchemaValidationSettingsParams{}, // specifying nil is ok for fields
)

expected := &APIShieldSchemaValidationSettings{
DefaultMitigationAction: "log",
OverrideMitigationAction: nil,
}

if assert.NoError(t, err) {
assert.Equal(t, expected, actual)
}
}

0 comments on commit c52383a

Please sign in to comment.