WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit d0190b8

Browse files
committed
Add "myipv4" as possible firewall setting input
1 parent 8aaf0d9 commit d0190b8

File tree

5 files changed

+50
-3
lines changed

5 files changed

+50
-3
lines changed

data.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,21 @@ data "hcloud_ssh_keys" "keys_by_selector" {
3232
count = length(var.ssh_hcloud_key_label) > 0 ? 1 : 0
3333
with_selector = var.ssh_hcloud_key_label
3434
}
35+
36+
data "external" "my_ipv4" {
37+
count = var.fetch_myip ? 1 : 0
38+
39+
program = [
40+
"bash",
41+
"-c",
42+
<<-EOT
43+
IP=$(dig -4 +short myip.opendns.com @resolver1.opendns.com | head -n 1)
44+
if [[ "$IP" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
45+
echo "{\"ip\": \"$IP\"}"
46+
else
47+
echo "Error: Failed to retrieve a valid public IPv4 address. Retrieved: '$IP'. Please check network connectivity and ensure 'dig' is installed." >&2
48+
exit 1
49+
fi
50+
EOT
51+
]
52+
}

kube.tf.example

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,18 +770,23 @@ module "kube-hetzner" {
770770
# If you want to allow all outbound traffic you can set this to "false". Default is "true".
771771
# restrict_outbound_traffic = false
772772

773+
# Fetch the current user IP-address using myip.opendns.com so that it can be used in firewall rules with the string "myipv4". Default: true.
774+
# fetch_myip = true
775+
773776
# Allow access to the Kube API from the specified networks. The default is ["0.0.0.0/0", "::/0"].
774777
# Allowed values: null (disable Kube API rule entirely) or a list of allowed networks with CIDR notation.
775778
# For maximum security, it's best to disable it completely by setting it to null. However, in that case, to get access to the kube api,
776779
# you would have to connect to any control plane node via SSH, as you can run kubectl from within these.
777780
# Please be advised that this setting has no effect on the load balancer when the use_control_plane_lb variable is set to true. This is
778781
# because firewall rules cannot be applied to load balancers yet.
782+
# Note: You can use the string "myipv4" as an IP address in the array and it will be replaced with the CIDR/32 of your IP as reported by myip.opendns.com. Use of "myipv4" requires `dig` to be available and `fetch_myip = true`.
779783
# firewall_kube_api_source = null
780784

781785
# Allow SSH access from the specified networks. Default: ["0.0.0.0/0", "::/0"]
782786
# Allowed values: null (disable SSH rule entirely) or a list of allowed networks with CIDR notation.
783787
# Ideally you would set your IP there. And if it changes after cluster deploy, you can always update this variable and apply again.
784-
# firewall_ssh_source = ["1.2.3.4/32"]
788+
# Note: You can use the string "myipv4" as an IP address in the array and it will be replaced with the CIDR/32 of your IP as reported by myip.opendns.com. Use of "myipv4" requires `dig` to be available and `fetch_myip = true`.
789+
# firewall_ssh_source = ["myipv4", "1.2.3.4/32"]
785790

786791
# By default, SELinux is enabled in enforcing mode on all nodes. For container-specific SELinux issues,
787792
# consider using the pre-installed 'udica' tool to create custom, targeted SELinux policies instead of
@@ -790,6 +795,7 @@ module "kube-hetzner" {
790795

791796
# Adding extra firewall rules, like opening a port
792797
# More info on the format here https://registry.terraform.io/providers/hetznercloud/hcloud/latest/docs/resources/firewall
798+
# Note: You can use the string "myipv4" as an IP address in the `source_ips` or `destination_ips` arrays and it will be replaced with the CIDR/32 of your IP as reported by myip.opendns.com. Use of "myipv4" requires `dig` to be available and `fetch_myip = true`.
793799
# extra_firewall_rules = [
794800
# {
795801
# description = "For Postgres"

locals.tf

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ locals {
2727
dns_servers_ipv4 = [for ip in var.dns_servers : ip if provider::assert::ipv4(ip)]
2828
dns_servers_ipv6 = [for ip in var.dns_servers : ip if provider::assert::ipv6(ip)]
2929

30+
my_public_ipv4_cidr = var.fetch_myip ? "${data.external.my_ipv4[0].result.ip}/32" : null
31+
3032
additional_k3s_environment = join("\n",
3133
[
3234
for var_name, var_value in var.additional_k3s_environment :
@@ -474,8 +476,19 @@ locals {
474476
# merge the two lists
475477
firewall_rules_merged = merge(local.firewall_rules, local.extra_firewall_rules)
476478

477-
# convert the merged list back to a list
478-
firewall_rules_list = values(local.firewall_rules_merged)
479+
# replace "myipv4" with the actual value, merge to a list
480+
firewall_rules_list = [for key, rule in local.firewall_rules_merged : {
481+
description = rule.description
482+
direction = rule.direction
483+
protocol = rule.protocol
484+
port = rule.port
485+
source_ips = try([
486+
for ip in rule.source_ips : ip == "myipv4" && var.fetch_myip ? local.my_public_ipv4_cidr : ip
487+
], null)
488+
destination_ips = try([
489+
for ip in rule.destination_ips : ip == "myipv4" && var.fetch_myip ? local.my_public_ipv4_cidr : ip
490+
], null)
491+
} if rule != null]
479492

480493
labels = {
481494
"provisioner" = "terraform",

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,12 @@ variable "automatically_upgrade_os" {
660660
description = "Whether to enable or disable automatic os updates. Defaults to true. Should be disabled for single-node clusters"
661661
}
662662

663+
variable "fetch_myip" {
664+
type = bool
665+
default = true
666+
description = "Whether to fetch the public ip of the current client for the use of firewall configuration."
667+
}
668+
663669
variable "extra_firewall_rules" {
664670
type = list(any)
665671
default = []

versions.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ terraform {
99
source = "hetznercloud/hcloud"
1010
version = ">= 1.51.0"
1111
}
12+
external = {
13+
source = "hashicorp/external"
14+
version = "~> 2.0"
15+
}
1216
local = {
1317
source = "hashicorp/local"
1418
version = ">= 2.5.2"

0 commit comments

Comments
 (0)