From 6d7cd990b7e5ad51b87b085e17f4851915a79afa Mon Sep 17 00:00:00 2001 From: nathan Date: Sun, 23 Jun 2019 12:57:36 +0200 Subject: [PATCH] Added user access firewall rules --- src/dorkbox/api/CloudflareActions.kt | 13 +++ src/dorkbox/api/firewall/AccessRule.kt | 109 ++++++++++++++++++++++ src/dorkbox/api/firewall/Configuration.kt | 65 +++++++++++++ src/dorkbox/api/firewall/Scope.kt | 69 ++++++++++++++ 4 files changed, 256 insertions(+) create mode 100644 src/dorkbox/api/firewall/AccessRule.kt create mode 100644 src/dorkbox/api/firewall/Configuration.kt create mode 100644 src/dorkbox/api/firewall/Scope.kt diff --git a/src/dorkbox/api/CloudflareActions.kt b/src/dorkbox/api/CloudflareActions.kt index 6888f70..e366370 100644 --- a/src/dorkbox/api/CloudflareActions.kt +++ b/src/dorkbox/api/CloudflareActions.kt @@ -20,6 +20,7 @@ import dorkbox.api.dns.CreateDnsRecord import dorkbox.api.dns.DeleteDnsRecord import dorkbox.api.dns.DnsRecord import dorkbox.api.dns.UpdateDnsRecord +import dorkbox.api.firewall.AccessRule import dorkbox.api.user.BillingHistory import dorkbox.api.user.BillingProfile import dorkbox.api.user.User @@ -162,4 +163,16 @@ interface CloudflareActions { @Path("identifier") identifier: String ): Call> + /** + * Lists the access rules for the firewall. + * + * https://api.cloudflare.com/#dns-records-for-a-zone-delete-dns-record + */ + @Headers("Content-Type: application/json") + @GET("user/firewall/access_rules/rules") + fun listAccessRules( + @Header("X-Auth-Email") email: String, + @Header("X-Auth-Key") key: String + ): Call>> + } diff --git a/src/dorkbox/api/firewall/AccessRule.kt b/src/dorkbox/api/firewall/AccessRule.kt new file mode 100644 index 0000000..5ff9c0c --- /dev/null +++ b/src/dorkbox/api/firewall/AccessRule.kt @@ -0,0 +1,109 @@ +/* + * Copyright 2019 dorkbox, llc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package dorkbox.api.firewall + +import com.squareup.moshi.Json +import dorkbox.api.core.ISO8601 +import java.time.LocalDateTime + +/** + * https://api.cloudflare.com/#user-level-firewall-access-rule-properties + */ +class AccessRule { + + /** + * Access rule identifier tag + */ + @field:[Json(name = "id")] + val id = "" + + /** + * A personal note about the rule. Typically used as a reminder or explanation for the rule. + */ + @field:[Json(name = "notes")] + val notes = "" + + /** + * The possible modes the rule can be in. + * + * valid values: block, challenge, whitelist, js_challenge + */ + @field:[Json(name = "allowed_modes")] + val allowedModes = listOf() + + /** + * The action to apply to a matched request + * + * valid values: block, challenge, whitelist, js_challenge + */ + @field:[Json(name = "mode")] + val mode = "" + + /** + * Rule configuration + */ + @field:[Json(name = "configuration")] + val configuration = Configuration() + + @field:[Json(name = "scope")] + val scope = Scope() + + /** + * When the record was last modified + */ + @field:[Json(name = "modified_on") ISO8601] + var modifiedOn = LocalDateTime.now() + + /** + * When the record was created + */ + @field:[Json(name = "created_on") ISO8601] + var createdOn = LocalDateTime.now() + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + + other as AccessRule + + if (id != other.id) return false + if (notes != other.notes) return false + if (allowedModes != other.allowedModes) return false + if (mode != other.mode) return false + if (configuration != other.configuration) return false + if (scope != other.scope) return false + if (modifiedOn != other.modifiedOn) return false + if (createdOn != other.createdOn) return false + + return true + } + + override fun hashCode(): Int { + var result = id.hashCode() + result = 31 * result + notes.hashCode() + result = 31 * result + allowedModes.hashCode() + result = 31 * result + mode.hashCode() + result = 31 * result + configuration.hashCode() + result = 31 * result + scope.hashCode() + result = 31 * result + (modifiedOn?.hashCode() ?: 0) + result = 31 * result + (createdOn?.hashCode() ?: 0) + return result + } + + override fun toString(): String { + return "AccessRule(id='$id', notes='$notes', allowedModes=$allowedModes, mode='$mode', configuration=$configuration, scope=$scope, modifiedOn=$modifiedOn, createdOn=$createdOn)" + } +} diff --git a/src/dorkbox/api/firewall/Configuration.kt b/src/dorkbox/api/firewall/Configuration.kt new file mode 100644 index 0000000..0ebd297 --- /dev/null +++ b/src/dorkbox/api/firewall/Configuration.kt @@ -0,0 +1,65 @@ +/* + * Copyright 2019 dorkbox, llc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package dorkbox.api.firewall + +import com.squareup.moshi.Json + +/** + * https://api.cloudflare.com/#user-level-firewall-access-rule-properties + */ +class Configuration { + + /** + * The request property to target + * + * ip, ip_range, asn, country + * + */ + @field:[Json(name = "target")] + val target = "" + + + /** + * IP : The IP address to target in requests (198.51.100.4) + * IP_RANGE : The IP range to target in requests. Limited to /16 and /24 (198.51.100.4/16) + * ASN : The AS number to target in requests. (AS12345) + * COUNTRY : US, DE, etc + */ + @field:[Json(name = "value")] + val value = "" + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + + other as Configuration + + if (target != other.target) return false + if (value != other.value) return false + + return true + } + + override fun hashCode(): Int { + var result = target.hashCode() + result = 31 * result + value.hashCode() + return result + } + + override fun toString(): String { + return "Configuration(target='$target', value='$value')" + } +} diff --git a/src/dorkbox/api/firewall/Scope.kt b/src/dorkbox/api/firewall/Scope.kt new file mode 100644 index 0000000..e48da95 --- /dev/null +++ b/src/dorkbox/api/firewall/Scope.kt @@ -0,0 +1,69 @@ +/* + * Copyright 2019 dorkbox, llc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package dorkbox.api.firewall + +import com.squareup.moshi.Json + +/** + * https://api.cloudflare.com/#user-level-firewall-access-rule-properties + */ +class Scope { + + /** + * User identifier tag + */ + @field:[Json(name = "id")] + val id = "" + + /** + * Your contact email address + */ + @field:[Json(name = "email")] + val email = "" + + + /** + * The scope of the rule + * + * valid values: user + */ + @field:[Json(name = "type")] + val type = "user" + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + + other as Scope + + if (id != other.id) return false + if (email != other.email) return false + if (type != other.type) return false + + return true + } + + override fun hashCode(): Int { + var result = id.hashCode() + result = 31 * result + email.hashCode() + result = 31 * result + type.hashCode() + return result + } + + override fun toString(): String { + return "Scope(id='$id', email='$email', type='$type')" + } +}