Skip to content

Commit 2ea51be

Browse files
committed
Structured authentication configuration
1 parent 0c87d28 commit 2ea51be

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed

control_planes.tf

+34
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ locals {
9696
disable-kube-proxy = var.disable_kube_proxy
9797
disable = local.disable_extras
9898
kubelet-arg = concat(local.kubelet_arg, var.k3s_global_kubelet_args, var.k3s_control_plane_kubelet_args, v.kubelet_args)
99+
kube-apiserver-arg = local.kube_apiserver_arg
99100
kube-controller-manager-arg = local.kube_controller_manager_arg
100101
flannel-iface = local.flannel_iface
101102
node-ip = module.control_planes[k].private_ipv4_address
@@ -153,6 +154,38 @@ resource "null_resource" "control_plane_config" {
153154
]
154155
}
155156

157+
158+
resource "null_resource" "authentication_config" {
159+
for_each = local.control_plane_nodes
160+
161+
triggers = {
162+
control_plane_id = module.control_planes[each.key].id
163+
authentication_config = sha1(var.authentication_config)
164+
}
165+
166+
connection {
167+
user = "root"
168+
private_key = var.ssh_private_key
169+
agent_identity = local.ssh_agent_identity
170+
host = module.control_planes[each.key].ipv4_address
171+
port = var.ssh_port
172+
}
173+
174+
provisioner "file" {
175+
content = var.authentication_config
176+
destination = "/tmp/authentication_config.yaml"
177+
}
178+
179+
provisioner "remote-exec" {
180+
inline = [local.k3s_authentication_config_update_script]
181+
}
182+
183+
depends_on = [
184+
null_resource.first_control_plane,
185+
hcloud_network_subnet.control_plane
186+
]
187+
}
188+
156189
resource "null_resource" "control_planes" {
157190
for_each = local.control_plane_nodes
158191

@@ -195,6 +228,7 @@ resource "null_resource" "control_planes" {
195228
depends_on = [
196229
null_resource.first_control_plane,
197230
null_resource.control_plane_config,
231+
null_resource.authentication_config,
198232
hcloud_network_subnet.control_plane
199233
]
200234
}

kube.tf.example

+43
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,49 @@ module "kube-hetzner" {
634634
# "trust anchor --store /root/ca.crt",
635635
# ]
636636

637+
# Structured authentication configuration. Multiple authentication providers support requires v1.30+ of
638+
# kubernetes.
639+
# https://kubernetes.io/docs/reference/access-authn-authz/authentication/#using-authentication-configuration
640+
#
641+
# authentication_config = <<-EOT
642+
# apiVersion: apiserver.config.k8s.io/v1beta1
643+
# kind: AuthenticationConfiguration
644+
# jwt:
645+
# - issuer:
646+
# url: "https://token.actions.githubusercontent.com"
647+
# audiences:
648+
# - "https://github.com/octo-org"
649+
# claimMappings:
650+
# username:
651+
# claim: sub
652+
# prefix: "gh:"
653+
# groups:
654+
# claim: repository_owner
655+
# prefix: "gh:"
656+
# claimValidationRules:
657+
# - claim: repository
658+
# requiredValue: "octo-org/octo-repo"
659+
# - claim: "repository_visibility"
660+
# requiredValue: "public"
661+
# - claim: "ref"
662+
# requiredValue: "refs/heads/main"
663+
# - claim: "ref_type"
664+
# requiredValue: "branch"
665+
# - issuer:
666+
# url: "https://your.oidc.issuer"
667+
# audiences:
668+
# - "oidc_client_id"
669+
# claimMappings:
670+
# username:
671+
# claim: oidc_username_claim
672+
# prefix: "oidc:"
673+
# groups:
674+
# claim: oidc_groups_claim
675+
# prefix: "oidc:"
676+
# EOT
677+
678+
679+
637680
# Additional flags to pass to the k3s server command (the control plane).
638681
# k3s_exec_server_args = "--kube-apiserver-arg enable-admission-plugins=PodTolerationRestriction,PodNodeSelector"
639682

locals.tf

+24
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,8 @@ locals {
436436
kube_controller_manager_arg = "flex-volume-plugin-dir=/var/lib/kubelet/volumeplugins"
437437
flannel_iface = "eth1"
438438

439+
kube_apiserver_arg = var.authentication_config != "" ? ["authentication-config=/etc/rancher/k3s/authentication_config.yaml"] : []
440+
439441
cilium_values = var.cilium_values != "" ? var.cilium_values : <<EOT
440442
# Enable Kubernetes host-scope IPAM mode (required for K3s + Hetzner CCM)
441443
ipam:
@@ -805,6 +807,28 @@ else
805807
fi
806808
EOF
807809

810+
k3s_authentication_config_update_script = <<EOF
811+
DATE=`date +%Y-%m-%d_%H-%M-%S`
812+
if cmp -s /tmp/authentication_config.yaml /etc/rancher/k3s/authentication_config.yaml; then
813+
echo "No update required to the authentication_config.yaml file"
814+
else
815+
if [ -f "/etc/rancher/k3s/authentication_config.yaml" ]; then
816+
echo "Backing up /etc/rancher/k3s/authentication_config.yaml to /tmp/authentication_config_$DATE.yaml"
817+
cp /etc/rancher/k3s/authentication_config.yaml /tmp/authentication_config_$DATE.yaml
818+
fi
819+
echo "Updated authentication_config.yaml detected, restart of k3s service required"
820+
cp /tmp/authentication_config.yaml /etc/rancher/k3s/authentication_config.yaml
821+
if systemctl is-active --quiet k3s; then
822+
systemctl restart k3s || (echo "Error: Failed to restart k3s. Restoring /etc/rancher/k3s/authentication_config.yaml from backup" && cp /tmp/authentication_config_$DATE.yaml /etc/rancher/k3s/authentication_config.yaml && systemctl restart k3s)
823+
elif systemctl is-active --quiet k3s-agent; then
824+
systemctl restart k3s-agent || (echo "Error: Failed to restart k3s-agent. Restoring /etc/rancher/k3s/authentication_config.yaml from backup" && cp /tmp/authentication_config_$DATE.yaml /etc/rancher/k3s/authentication_config.yaml && systemctl restart k3s-agent)
825+
else
826+
echo "No active k3s or k3s-agent service found"
827+
fi
828+
echo "k3s service or k3s-agent service (re)started successfully"
829+
fi
830+
EOF
831+
808832
cloudinit_write_files_common = <<EOT
809833
# Script to rename the private interface to eth1 and unify NetworkManager connection naming
810834
- path: /etc/cloud/rename_interface.sh

variables.tf

+6
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ variable "ssh_additional_public_keys" {
5757
default = []
5858
}
5959

60+
variable "authentication_config" {
61+
description = "Strucutred authentication configuration. This can be used to define external authentication providers."
62+
type = string
63+
default = ""
64+
}
65+
6066
variable "hcloud_ssh_key_id" {
6167
description = "If passed, a key already registered within hetzner is used. Otherwise, a new one will be created by the module."
6268
type = string

0 commit comments

Comments
 (0)