diff --git a/CHANGELOG.md b/CHANGELOG.md index af68651c..2793250f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ All notable changes to this project will be documented in this file. ### Added - The OLM bundle declares that the operator provides a CSI driver and sets `createdAt` ([#779]). +- `autoTls` certificates now contain the Pod's ServiceAccount as a SPIFFE ID URI SAN + (`spiffe:///ns//sa/`), in the same format as Istio ([#XXX]). ### Fixed diff --git a/docs/modules/secret-operator/pages/scope.adoc b/docs/modules/secret-operator/pages/scope.adoc index e542ebf9..efabff90 100644 --- a/docs/modules/secret-operator/pages/scope.adoc +++ b/docs/modules/secret-operator/pages/scope.adoc @@ -61,3 +61,5 @@ xref:#node[] and xref:#pod[] would contain the following values in its `subjectA * The node's IP address * The node's fully qualified domain name (`my-node.example.com`, without a trailing dot) * The pod's fully qualified domain name (`my-pod.my-service.my-namespace.svc.cluster.local`, without a trailing dot) + +Independent of the scopes, the xref:secretclass.adoc#backend-autotls[`autoTls`] backend always adds the SPIFFE ID of the Pod's ServiceAccount (`spiffe://cluster.local/ns/my-namespace/sa/my-service-account`). diff --git a/docs/modules/secret-operator/pages/secretclass.adoc b/docs/modules/secret-operator/pages/secretclass.adoc index 142a0995..acfdac13 100644 --- a/docs/modules/secret-operator/pages/secretclass.adoc +++ b/docs/modules/secret-operator/pages/secretclass.adoc @@ -40,6 +40,16 @@ CAUTION: Attributes of the certificate (such as the expiration date, fingerprint xref:scope.adoc[Scopes] are used to populate the claims (such as `subjectAlternateName`) of the provisioned certificates. +Regardless of the scopes, the `subjectAlternateName` also contains the Pod's ServiceAccount as a https://spiffe.io/docs/latest/spiffe-about/spiffe-concepts/[SPIFFE ID{external-link-icon}^] URI, using the same format as https://istio.io/latest/docs/concepts/security/[Istio{external-link-icon}^]. +The Kubernetes cluster domain is used as the trust domain: + +``` +URI:spiffe://cluster.local/ns/my-namespace/sa/my-service-account +``` + +This identifies the workload independently of its addresses, and can be used for authorization, for example of TLS client certificates. +Keep in mind that every Pod that is allowed to use the same ServiceAccount gets a certificate with the same SPIFFE ID. + [#backend-tls-certificate-key-pair-generation] ==== `TLS certificate key pair generation` diff --git a/rust/operator-binary/src/backend/auto_tls/mod.rs b/rust/operator-binary/src/backend/auto_tls/mod.rs index 6f79e308..ed3fe107 100644 --- a/rust/operator-binary/src/backend/auto_tls/mod.rs +++ b/rust/operator-binary/src/backend/auto_tls/mod.rs @@ -488,6 +488,14 @@ impl SecretBackend for TlsGenerate { Address::Ip(ip) => san_ext.ip(&ip.to_string()), }; } + if let Some(service_account_name) = &pod_info.service_account_name { + has_san = true; + san_ext.uri(&spiffe_id( + &pod_info.kubernetes_cluster_domain, + &pod_info.namespace, + service_account_name, + )); + } if has_san { exts.push(san_ext.build(&ctx)?); } @@ -570,6 +578,19 @@ fn cert_lifetime_cap_by_ca( (cert_lifetime > remaining_ca_lifetime).then_some(remaining_ca_lifetime) } +/// Returns the [SPIFFE ID](https://github.com/spiffe/spiffe/blob/main/standards/SPIFFE-ID.md) +/// of a ServiceAccount, using the same format as Istio: +/// `spiffe:///ns//sa/`. +/// +/// The Kubernetes cluster domain is used as trust domain. SPIFFE requires the trust domain to be +/// lowercase and without a trailing dot, which a [`DomainName`] does not guarantee. +/// +/// [`DomainName`]: stackable_operator::commons::networking::DomainName +fn spiffe_id(cluster_domain: &str, namespace: &str, service_account: &str) -> String { + let trust_domain = cluster_domain.trim_end_matches('.').to_lowercase(); + format!("spiffe://{trust_domain}/ns/{namespace}/sa/{service_account}") +} + #[cfg(test)] mod tests { use rstest::rstest; @@ -597,4 +618,15 @@ mod tests { expected_cap ); } + + #[rstest] + #[case::default_cluster_domain("cluster.local")] + #[case::trailing_dot("cluster.local.")] + #[case::uppercase("Cluster.LOCAL")] + fn spiffe_id(#[case] cluster_domain: &str) { + assert_eq!( + super::spiffe_id(cluster_domain, "my-namespace", "my-service-account"), + "spiffe://cluster.local/ns/my-namespace/sa/my-service-account" + ); + } } diff --git a/rust/operator-binary/src/backend/pod_info.rs b/rust/operator-binary/src/backend/pod_info.rs index 1c4c1848..ffb19316 100644 --- a/rust/operator-binary/src/backend/pod_info.rs +++ b/rust/operator-binary/src/backend/pod_info.rs @@ -107,6 +107,8 @@ pub struct PodInfo { pub pod_name: String, pub pod_uid: String, pub service_name: Option, + /// Can be empty if the ServiceAccount admission plugin is disabled + pub service_account_name: Option, pub namespace: String, pub node_name: String, pub node_ips: Vec, @@ -159,6 +161,11 @@ impl PodInfo { pod_name, pod_uid, service_name: pod.spec.as_ref().and_then(|spec| spec.subdomain.clone()), + service_account_name: pod + .spec + .as_ref() + .and_then(|spec| spec.service_account_name.clone()) + .filter(|name| !name.is_empty()), namespace, node_name, node_ips: node diff --git a/tests/templates/kuttl/tls/10_consumer.yaml.j2 b/tests/templates/kuttl/tls/10_consumer.yaml.j2 index 1c198d21..17c46f07 100644 --- a/tests/templates/kuttl/tls/10_consumer.yaml.j2 +++ b/tests/templates/kuttl/tls/10_consumer.yaml.j2 @@ -49,6 +49,11 @@ spec: cat "/stackable/tls-3d/$CERT_NAME" | openssl x509 -noout -text | grep "DNS:my-tls-service.$NAMESPACE.svc.cluster.local" cat "/stackable/tls-42h/$CERT_NAME" | openssl x509 -noout -text | grep "DNS:my-tls-service.$NAMESPACE.svc.cluster.local" + echo "Test SPIFFE ID of the ServiceAccount in the SAN" + + cat "/stackable/tls-3d/$CERT_NAME" | openssl x509 -noout -text | grep "URI:spiffe://cluster.local/ns/$NAMESPACE/sa/integration-tests-sa" + openssl x509 -in /stackable/tls-domain-components/tls.crt -noout -text | grep "URI:spiffe://cluster.local/ns/$NAMESPACE/sa/integration-tests-sa" + function assert_trusted_roots_contain { subject=$1