-
Notifications
You must be signed in to change notification settings - Fork 615
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ip_access_rules: Add ListIPAccessRules api call
- Loading branch information
Showing
3 changed files
with
325 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:enhancement | ||
ip_access_rules: Add ListIpAccessRules() to list IP Access Rules | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package cloudflare | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/goccy/go-json" | ||
) | ||
|
||
type ListIpAccessRulesOrderOption string | ||
type ListIpAccessRulesMatchOption string | ||
type RulesModeOption string | ||
|
||
const ( | ||
ConfigurationTarget ListIpAccessRulesOrderOption = "configuration.target" | ||
ConfigurationValue ListIpAccessRulesOrderOption = "configuration.value" | ||
Mode ListIpAccessRulesOrderOption = "mode" | ||
MatchOptionAll ListIpAccessRulesMatchOption = "all" | ||
MatchOptionAny ListIpAccessRulesMatchOption = "any" | ||
RuleModeBlock RulesModeOption = "block" | ||
RuleModeChallenge RulesModeOption = "challenge" | ||
RuleModeJsChallenge RulesModeOption = "js_challenge" | ||
RuleModeManagedChallenge RulesModeOption = "managed_challenge" | ||
RuleModeWhitelist RulesModeOption = "whitelist" | ||
) | ||
|
||
type EGSPaginationJSON struct { | ||
Page int `url:"page,omitempty"` | ||
PerPage int `url:"per_page,omitempty"` | ||
} | ||
|
||
type ListIpAccessRulesFilters struct { | ||
ConfigurationTarget string `json:"configuration.target,omitempty"` | ||
ConfigurationValue string `json:"configuration.value,omitempty"` | ||
Match ListIpAccessRulesMatchOption `json:"match,omitempty"` | ||
Mode RulesModeOption `json:"mode,omitempty"` | ||
Notes string `json:"notes,omitempty"` | ||
} | ||
|
||
type ListIpAccessRulesParams struct { | ||
Direction string `url:"direction,omitempty"` | ||
EGSPaginationJSON EGSPaginationJSON `url:"egs-pagination.json,omitempty"` | ||
Filters ListIpAccessRulesFilters `url:"filters,omitempty"` | ||
Order ListIpAccessRulesOrderOption `url:"order,omitempty"` | ||
Page int `url:"page,omitempty"` | ||
PerPage int `url:"per_page,omitempty"` | ||
} | ||
|
||
type IPAccessRuleConfiguration struct { | ||
Target string `json:"target"` | ||
Value string `json:"value"` | ||
} | ||
|
||
type IpAccessRule struct { | ||
AllowedModes []RulesModeOption `json:"allowed_modes"` | ||
Configuration IPAccessRuleConfiguration `json:"configuration"` | ||
CreatedOn string `json:"created_on"` | ||
ID string `json:"id"` | ||
Mode RulesModeOption `json:"mode"` | ||
ModifiedOn string `json:"modified_on"` | ||
Notes string `json:"notes"` | ||
} | ||
|
||
type ListIpAccessRulesResponse struct { | ||
Result []IpAccessRule `json:"result"` | ||
ResultInfo `json:"result_info"` | ||
Response | ||
} | ||
|
||
// ListIpAccessRules | ||
// | ||
// Fetches IP Access rules of a zone. You can filter the results using several optional parameters. | ||
// | ||
// API reference: https://developers.cloudflare.com/api/operations/ip-access-rules-for-a-zone-list-ip-access-rules | ||
func (api *API) ListIpAccessRules(ctx context.Context, rc *ResourceContainer, params ListIpAccessRulesParams) ([]IpAccessRule, error) { | ||
|
||
if rc.Identifier == "" { | ||
return []IpAccessRule{}, ErrMissingZoneID | ||
} | ||
|
||
uri := buildURI(fmt.Sprintf("/zones/%s/firewall/access_rules/rules", rc.Identifier), params) | ||
|
||
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) | ||
if err != nil { | ||
return []IpAccessRule{}, err | ||
} | ||
|
||
result := ListIpAccessRulesResponse{} | ||
|
||
err = json.Unmarshal(res, &result) | ||
if err != nil { | ||
return []IpAccessRule{}, fmt.Errorf("%s: %w", errUnmarshalError, err) | ||
} | ||
|
||
return result.Result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
package cloudflare | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestListIPAccessRules(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
handler := func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method) | ||
w.Header().Set("content-type", "application/json") | ||
fmt.Fprintf(w, `{ | ||
"result":[ | ||
{ | ||
"allowed_modes": [ | ||
"whitelist", | ||
"block", | ||
"challenge", | ||
"js_challenge", | ||
"managed_challenge" | ||
], | ||
"configuration": { | ||
"target": "ip", | ||
"value": "198.51.100.1" | ||
}, | ||
"created_on": "2014-01-01T05:20:00.12345Z", | ||
"id": "f2d427378e7542acb295380d352e2ebd", | ||
"mode": "whitelist", | ||
"modified_on": "2014-01-01T05:20:00.12345Z", | ||
"notes": "Whitelisting this IP." | ||
}, | ||
{ | ||
"allowed_modes": [ | ||
"whitelist", | ||
"block", | ||
"challenge", | ||
"js_challenge", | ||
"managed_challenge" | ||
], | ||
"configuration": { | ||
"target": "ip", | ||
"value": "198.51.100.2" | ||
}, | ||
"created_on": "2014-01-01T05:20:00.12345Z", | ||
"id": "92f17202ed8bd63d69a66b86a49a8f6b", | ||
"mode": "block", | ||
"modified_on": "2014-01-01T05:20:00.12345Z", | ||
"notes": "This rule is enabled because of an event that occurred on date X." | ||
}, | ||
{ | ||
"allowed_modes": [ | ||
"whitelist", | ||
"block", | ||
"challenge", | ||
"js_challenge", | ||
"managed_challenge" | ||
], | ||
"configuration": { | ||
"target": "ip", | ||
"value": "198.51.100.3" | ||
}, | ||
"created_on": "2014-01-01T05:20:00.12345Z", | ||
"id": "4ae338944d6143378c3cf05a7c77d983", | ||
"mode": "challenge", | ||
"modified_on": "2014-01-01T05:20:00.12345Z", | ||
"notes": "This rule is enabled because of an event that occurred on date Y." | ||
}, | ||
{ | ||
"allowed_modes": [ | ||
"whitelist", | ||
"block", | ||
"challenge", | ||
"js_challenge", | ||
"managed_challenge" | ||
], | ||
"configuration": { | ||
"target": "ip", | ||
"value": "198.51.100.4" | ||
}, | ||
"created_on": "2014-01-01T05:20:00.12345Z", | ||
"id": "52161eb6af4241bb9d4b32394be72fdf", | ||
"mode": "js_challenge", | ||
"modified_on": "2014-01-01T05:20:00.12345Z", | ||
"notes": "This rule is enabled because of an event that occurred on date Z." | ||
}, | ||
{ | ||
"allowed_modes": [ | ||
"whitelist", | ||
"block", | ||
"challenge", | ||
"js_challenge", | ||
"managed_challenge" | ||
], | ||
"configuration": { | ||
"target": "ip", | ||
"value": "198.51.100.5" | ||
}, | ||
"created_on": "2014-01-01T05:20:00.12345Z", | ||
"id": "cbf4b7a5a2a24e59a03044d6d44ceb09", | ||
"mode": "managed_challenge", | ||
"modified_on": "2014-01-01T05:20:00.12345Z", | ||
"notes": "This rule is enabled because we like the challenge page." | ||
} | ||
], | ||
"success":true, | ||
"errors":null, | ||
"messages":null, | ||
"result_info":{ | ||
"page":1, | ||
"per_page":25, | ||
"count":5, | ||
"total_count":5 | ||
} | ||
} | ||
`) | ||
} | ||
|
||
mux.HandleFunc("/zones/d56084adb405e0b7e32c52321bf07be6/firewall/access_rules/rules", handler) | ||
want := []IpAccessRule{ | ||
{ | ||
AllowedModes: []RulesModeOption{ | ||
RuleModeWhitelist, | ||
RuleModeBlock, | ||
RuleModeChallenge, | ||
RuleModeJsChallenge, | ||
RuleModeManagedChallenge, | ||
}, | ||
ID: "f2d427378e7542acb295380d352e2ebd", | ||
Configuration: IPAccessRuleConfiguration{ | ||
Target: "ip", | ||
Value: "198.51.100.1", | ||
}, | ||
CreatedOn: "2014-01-01T05:20:00.12345Z", | ||
Mode: "whitelist", | ||
ModifiedOn: "2014-01-01T05:20:00.12345Z", | ||
Notes: "Whitelisting this IP.", | ||
}, | ||
{ | ||
AllowedModes: []RulesModeOption{ | ||
RuleModeWhitelist, | ||
RuleModeBlock, | ||
RuleModeChallenge, | ||
RuleModeJsChallenge, | ||
RuleModeManagedChallenge, | ||
}, | ||
ID: "92f17202ed8bd63d69a66b86a49a8f6b", | ||
Configuration: IPAccessRuleConfiguration{ | ||
Target: "ip", | ||
Value: "198.51.100.2", | ||
}, | ||
CreatedOn: "2014-01-01T05:20:00.12345Z", | ||
Mode: "block", | ||
ModifiedOn: "2014-01-01T05:20:00.12345Z", | ||
Notes: "This rule is enabled because of an event that occurred on date X.", | ||
}, | ||
{ | ||
AllowedModes: []RulesModeOption{ | ||
RuleModeWhitelist, | ||
RuleModeBlock, | ||
RuleModeChallenge, | ||
RuleModeJsChallenge, | ||
RuleModeManagedChallenge, | ||
}, | ||
ID: "4ae338944d6143378c3cf05a7c77d983", | ||
Configuration: IPAccessRuleConfiguration{ | ||
Target: "ip", | ||
Value: "198.51.100.3", | ||
}, | ||
CreatedOn: "2014-01-01T05:20:00.12345Z", | ||
Mode: "challenge", | ||
ModifiedOn: "2014-01-01T05:20:00.12345Z", | ||
Notes: "This rule is enabled because of an event that occurred on date Y.", | ||
}, | ||
{ | ||
AllowedModes: []RulesModeOption{ | ||
RuleModeWhitelist, | ||
RuleModeBlock, | ||
RuleModeChallenge, | ||
RuleModeJsChallenge, | ||
RuleModeManagedChallenge, | ||
}, | ||
ID: "52161eb6af4241bb9d4b32394be72fdf", | ||
Configuration: IPAccessRuleConfiguration{ | ||
Target: "ip", | ||
Value: "198.51.100.4", | ||
}, | ||
CreatedOn: "2014-01-01T05:20:00.12345Z", | ||
Mode: "js_challenge", | ||
ModifiedOn: "2014-01-01T05:20:00.12345Z", | ||
Notes: "This rule is enabled because of an event that occurred on date Z.", | ||
}, | ||
{ | ||
AllowedModes: []RulesModeOption{ | ||
RuleModeWhitelist, | ||
RuleModeBlock, | ||
RuleModeChallenge, | ||
RuleModeJsChallenge, | ||
RuleModeManagedChallenge, | ||
}, | ||
ID: "cbf4b7a5a2a24e59a03044d6d44ceb09", | ||
Configuration: IPAccessRuleConfiguration{ | ||
Target: "ip", | ||
Value: "198.51.100.5", | ||
}, | ||
CreatedOn: "2014-01-01T05:20:00.12345Z", | ||
Mode: "managed_challenge", | ||
ModifiedOn: "2014-01-01T05:20:00.12345Z", | ||
Notes: "This rule is enabled because we like the challenge page.", | ||
}, | ||
} | ||
|
||
actual, err := client.ListIpAccessRules(context.Background(), | ||
ZoneIdentifier("d56084adb405e0b7e32c52321bf07be6"), ListIpAccessRulesParams{}) | ||
|
||
if assert.NoError(t, err) { | ||
assert.Equal(t, want, actual) | ||
} | ||
} |