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 content scanning #3700

Merged
merged 4 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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/3700.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
leaked_credential_check: Add new methods to interact with the Content Scanning Cloudflare API
jacobbednarz marked this conversation as resolved.
Show resolved Hide resolved
```
189 changes: 189 additions & 0 deletions content_scanning.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
package cloudflare

import (
"context"
"fmt"
"net/http"

"github.com/goccy/go-json"
)

type ContentScanningStatusResult struct {
Value string `json:"value"`
Modified string `json:"modified"`
}

type ContentScanningEnableParams struct{}

type ContentScanningEnableResponse struct {
Response
Result *ContentScanningStatusResult `json:"result"` // nil
}

type ContentScanningDisableParams struct{}

type ContentScanningDisableResponse struct {
Response
Result *ContentScanningStatusResult `json:"result"` // nil
}

type ContentScanningStatusParams struct{}

type ContentScanningStatusResponse struct {
Response
Result *ContentScanningStatusResult `json:"result"` // object or nil
}

type ContentScanningCustomExpression struct {
ID string `json:"id"`
Payload string `json:"payload"`
}

type ContentScanningListCustomExpressionsParams struct{}

type ContentScanningListCustomExpressionsResponse struct {
Response
Result []ContentScanningCustomExpression `json:"result"`
}

type ContentScanningCustomPayload struct {
Payload string `json:"payload"`
}

type ContentScanningAddCustomExpressionsParams struct {
Payloads []ContentScanningCustomPayload
}

type ContentScanningAddCustomExpressionsResponse struct {
Response
Result []ContentScanningCustomExpression `json:"result"`
}

type ContentScanningDeleteCustomExpressionsParams struct {
ID string
}

type ContentScanningDeleteCustomExpressionsResponse struct {
Response
Result []ContentScanningCustomExpression `json:"result"`
}

// ContentScanningEnable enables Content Scanning.
//
// API reference: https://developers.cloudflare.com/api/operations/waf-content-scanning-enable
func (api *API) ContentScanningEnable(ctx context.Context, rc *ResourceContainer, params ContentScanningEnableParams) (ContentScanningEnableResponse, error) {
if rc.Identifier == "" {
return ContentScanningEnableResponse{}, ErrMissingZoneID
}

uri := fmt.Sprintf("/zones/%s/content-upload-scan/enable", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, nil)
if err != nil {
return ContentScanningEnableResponse{}, err
}
result := ContentScanningEnableResponse{}
if err := json.Unmarshal(res, &result); err != nil {
return ContentScanningEnableResponse{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return result, nil
}

// ContentScanningDisable disables Content Scanning.
//
// API reference: https://developers.cloudflare.com/api/operations/waf-content-scanning-disable
func (api *API) ContentScanningDisable(ctx context.Context, rc *ResourceContainer, params ContentScanningDisableParams) (ContentScanningDisableResponse, error) {
if rc.Identifier == "" {
return ContentScanningDisableResponse{}, ErrMissingZoneID
}

uri := fmt.Sprintf("/zones/%s/content-upload-scan/disable", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, nil)
if err != nil {
return ContentScanningDisableResponse{}, err
}
result := ContentScanningDisableResponse{}
if err := json.Unmarshal(res, &result); err != nil {
return ContentScanningDisableResponse{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return result, nil
}

// ContentScanningStatus reads the status of Content Scanning.
//
// API reference: https://developers.cloudflare.com/api/operations/waf-content-scanning-get-status
func (api *API) ContentScanningStatus(ctx context.Context, rc *ResourceContainer, params ContentScanningStatusParams) (ContentScanningStatusResponse, error) {
if rc.Identifier == "" {
return ContentScanningStatusResponse{}, ErrMissingZoneID
}

uri := fmt.Sprintf("/zones/%s/content-upload-scan/settings", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return ContentScanningStatusResponse{}, err
}
result := ContentScanningStatusResponse{}
if err := json.Unmarshal(res, &result); err != nil {
return ContentScanningStatusResponse{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return result, nil
}

// ContentScanningListCustomExpressions retrieves the list of existing custom scan expressions for Content Scanning
//
// API reference: https://developers.cloudflare.com/api/operations/waf-content-scanning-list-custom-scan-expressions
func (api *API) ContentScanningListCustomExpressions(ctx context.Context, rc *ResourceContainer, params ContentScanningListCustomExpressionsParams) ([]ContentScanningCustomExpression, error) {
if rc.Identifier == "" {
return []ContentScanningCustomExpression{}, ErrMissingZoneID
}

uri := fmt.Sprintf("/zones/%s/content-upload-scan/payloads", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []ContentScanningCustomExpression{}, err
}
result := ContentScanningListCustomExpressionsResponse{}
if err := json.Unmarshal(res, &result); err != nil {
return []ContentScanningCustomExpression{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return result.Result, nil
}

// ContentScanningAddCustomExpressions add new custom scan expressions for Content Scanning
//
// API reference: https://developers.cloudflare.com/api/operations/waf-content-scanning-list-custom-scan-expressions
func (api *API) ContentScanningAddCustomExpressions(ctx context.Context, rc *ResourceContainer, params ContentScanningAddCustomExpressionsParams) ([]ContentScanningCustomExpression, error) {
if rc.Identifier == "" {
return []ContentScanningCustomExpression{}, ErrMissingZoneID
}

uri := fmt.Sprintf("/zones/%s/content-upload-scan/payloads", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, params.Payloads)
if err != nil {
return []ContentScanningCustomExpression{}, err
}
result := ContentScanningAddCustomExpressionsResponse{}
if err := json.Unmarshal(res, &result); err != nil {
return []ContentScanningCustomExpression{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return result.Result, nil
}

// ContentScanningDeleteCustomExpressions delete custom scan expressions for Content Scanning
//
// API reference: https://developers.cloudflare.com/api/operations/waf-content-scanning-delete-custom-scan-expressions
func (api *API) ContentScanningDeleteCustomExpression(ctx context.Context, rc *ResourceContainer, params ContentScanningDeleteCustomExpressionsParams) ([]ContentScanningCustomExpression, error) {
if rc.Identifier == "" {
return []ContentScanningCustomExpression{}, ErrMissingZoneID
}

uri := fmt.Sprintf("/zones/%s/content-upload-scan/payloads/%s", rc.Identifier, params.ID)
res, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return []ContentScanningCustomExpression{}, err
}
result := ContentScanningDeleteCustomExpressionsResponse{}
if err := json.Unmarshal(res, &result); err != nil {
return []ContentScanningCustomExpression{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return result.Result, nil
}
Loading
Loading