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

Add support for Get/Update API Shield Schema Validation Settings #1418

Merged
merged 1 commit into from
Oct 13, 2023
Merged
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
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)
}
}
Loading