Skip to content

Commit

Permalink
Adding support for Client IP Rules (#205)
Browse files Browse the repository at this point in the history
This commit adds support for Client IP Rules / Client IP Headers. They can be specified using `client_ip_rules` (ordered list). For example:

```
client_ip_rules = ["x-client-ip", "x-another-header"]
```

---------

Co-authored-by: Daniel Corbett <[email protected]>
  • Loading branch information
aaronmaxlevy and daniel-corbett authored Apr 18, 2024
1 parent c2d19c3 commit 4c0233c
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/data-sources/sites.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Read-Only:
- `block_duration_secs` (Number)
- `block_http_code` (Number)
- `block_redirect_url` (String)
- `client_ip_rules` (Set of String)
- `created` (String)
- `display_name` (String)
- `events_uri` (String)
Expand Down
2 changes: 2 additions & 0 deletions docs/resources/site.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ resource "sigsci_site" "my-site" {
block_duration_seconds = 86400
agent_anon_mode = ""
agent_level = "block"
client_ip_rules = ["X-Client-IP"]
}
```

Expand All @@ -38,6 +39,7 @@ resource "sigsci_site" "my-site" {
- `block_duration_seconds` (Number) Duration to block an IP in seconds
- `block_http_code` (Number) HTTP response code to send when traffic is being blocked
- `block_redirect_url` (String) URL to redirect to when blocking with a '301' or '302' HTTP status code
- `client_ip_rules` (List of String) Headers used for assigning client IPs to requests
- `immediate_block` (Boolean) Immediately block requests that contain attack signals

### Read-Only
Expand Down
1 change: 1 addition & 0 deletions examples/resources/sigsci_site/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ resource "sigsci_site" "my-site" {
block_duration_seconds = 86400
agent_anon_mode = ""
agent_level = "block"
client_ip_rules = ["X-Client-IP"]
}
9 changes: 9 additions & 0 deletions provider/datasource_sites.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ func dataSourceSites() *schema.Resource {
Computed: true,
Description: "URL to redirect to when blockHTTPCode is 301 or 302",
},
"client_ip_rules": {
Type: schema.TypeSet,
Computed: true,
Description: "Headers used for assigning client IPs to requests",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"created": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -177,6 +185,7 @@ func flattenSites(data []sigsci.Site, filter string) []map[string]any {
"block_duration_secs": site.BlockDurationSeconds,
"block_http_code": site.BlockHTTPCode,
"block_redirect_url": site.BlockRedirectURL,
"client_ip_rules": flattenClientIPRules(site.ClientIPRules),
"created": site.Created.String(),
"display_name": site.DisplayName,
"events_uri": site.Events["uri"],
Expand Down
16 changes: 16 additions & 0 deletions provider/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,22 @@ func expandRuleRateLimit(rateLimitResource *schema.Set) *sigsci.RateLimit {
}
}

func expandClientIPRules(headers []interface{}) sigsci.ClientIPRules {
rulesArray := make(sigsci.ClientIPRules, len(headers))
for i, e := range headers {
rulesArray[i].Header = e.(string)
}
return rulesArray
}

func flattenClientIPRules(rules sigsci.ClientIPRules) []interface{} {
interfaceArray := make([]interface{}, len(rules))
for i, val := range rules {
interfaceArray[i] = val.Header
}
return interfaceArray
}

func flattenRuleRateLimit(rateLimit *sigsci.RateLimit) []interface{} {
if rateLimit == nil {
return nil
Expand Down
14 changes: 14 additions & 0 deletions provider/resource_site.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ func resourceSite() *schema.Resource {
Description: "URL to redirect to when blocking with a '301' or '302' HTTP status code",
Optional: true,
},
"client_ip_rules": {
Type: schema.TypeList,
Description: "Headers used for assigning client IPs to requests",
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"immediate_block": {
Type: schema.TypeBool,
Description: "Immediately block requests that contain attack signals",
Expand Down Expand Up @@ -121,6 +129,7 @@ func createSite(d *schema.ResourceData, m interface{}) error {
BlockHTTPCode: d.Get("block_http_code").(int),
BlockDurationSeconds: d.Get("block_duration_seconds").(int),
BlockRedirectURL: d.Get("block_redirect_url").(string),
ClientIPRules: expandClientIPRules(d.Get("client_ip_rules").([]interface{})),
ImmediateBlock: d.Get("immediate_block").(bool),
})
if err != nil {
Expand Down Expand Up @@ -166,6 +175,10 @@ func readSite(d *schema.ResourceData, m interface{}) error {
if err != nil {
return err
}
err = d.Set("client_ip_rules", flattenClientIPRules(site.ClientIPRules))
if err != nil {
return err
}
err = d.Set("agent_anon_mode", site.AgentAnonMode)
if err != nil {
return err
Expand Down Expand Up @@ -209,6 +222,7 @@ func updateSite(d *schema.ResourceData, m interface{}) error {
BlockHTTPCode: d.Get("block_http_code").(int),
BlockRedirectURL: d.Get("block_redirect_url").(string),
AgentAnonMode: d.Get("agent_anon_mode").(string),
ClientIPRules: expandClientIPRules(d.Get("client_ip_rules").([]interface{})),
ImmediateBlock: d.Get("immediate_block").(bool),
})
if err != nil {
Expand Down

0 comments on commit 4c0233c

Please sign in to comment.