Skip to content

Commit fa7579c

Browse files
authored
Merge pull request #11 from projectsyn/fix/external-certificates
Fix external certificate support for admission webhook
2 parents 58a9fb2 + b4897df commit fa7579c

7 files changed

Lines changed: 32 additions & 9 deletions

File tree

docs/modules/ROOT/pages/how-tos/external-certificates.adoc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ webhook=patch-operator-webhook-service.${operator_ns}.svc
4646
metrics=patch-operator-controller-manager-metrics-service.${operator_ns}.svc
4747
4848
openssl req -x509 -newkey rsa:4096 -nodes -keyout webhook.key -out webhook.crt -days ${lifetime} \
49-
-subj "/CN=$webhook" -addext "subjectAltName = DNS:$webhook,DNS:${webhook}.cluster.local"
49+
-subj "/CN=webhook" -addext "subjectAltName = DNS:$webhook,DNS:${webhook}.cluster.local"
5050
openssl req -x509 -newkey rsa:4096 -nodes -keyout metrics.key -out metrics.crt -days ${lifetime} \
51-
-subj "/CN=$metrics" -addext "subjectAltName = DNS:$metrics,DNS:${metrics}.cluster.local"
51+
-subj "/CN=metrics" -addext "subjectAltName = DNS:$metrics,DNS:${metrics}.cluster.local"
5252
----
5353
<1> Update if you're installing the patch operator in a different namespace.
5454
You can extract the actual operator namespace from the inventory with `kapitan inventory -t patch-operator | yq '.parameters.patch_operator.namespace'`.
@@ -64,8 +64,7 @@ vault login -metod=oidc <2>
6464
6565
parent="clusters/kv/${TENANT_ID}/${CLUSTER_ID}"
6666
67-
vault kv put $parent/patch-operator webhook-key=@webhook.key
68-
vault kv put $parent/patch-operator metrics-key=@metrics.key
67+
vault kv put $parent/patch-operator webhook-key=@webhook.key metrics-key=@metrics.key
6968
----
7069
<1> Replace with the URL of your Project Syn Vault instance.
7170
<2> This assumes that your Vault instance is setup with OIDC login for users.

docs/modules/ROOT/pages/references/parameters.adoc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ Supported keys are `tls.key`, `tls.crt` and `ca.crt`.
4141
The component will generate secrets with type `kubernetes.io/tls` and the provided keys.
4242
If key `ca.crt` is missing, the component assumes that `tls.crt` is a self-signed certificate.
4343

44+
[IMPORTANT]
45+
====
46+
The certificate must be provided directly in the hierarchy, rather than as a secret reference.
47+
48+
When field `tls.crt` or `ca.crt` are provided as secret references, the component will generate invalid admission webhook configurations if the certificates are provided as PEM-encoded.
49+
To get valid admission webhook configs with secret references, the certificates would have to be base64-encoded PEM-encoded certificates.
50+
However, by base64-encoding the certificates in Vault, we'd get an invalid certificate secret, since we always emit the secrets with the certificate values in field `stringData`.
51+
====
52+
4453
== `charts`
4554

4655
[horizontal]

postprocess/fixup-helm-chart.jsonnet

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,19 @@ local fixupFn(obj) =
3131
w {
3232
clientConfig+: {
3333
// use ca.crt if specified, and assume self-signed cert otherwise.
34-
caBundle: std.get(
34+
local caBundle = std.get(
3535
external_certs.webhook,
3636
'ca.crt',
3737
external_certs.webhook['tls.crt']
3838
),
39+
// caBundle is expected to be base64-encoded, we encode here, if
40+
// the provided caBundle value looks like a PEM-encoded
41+
// certificate (i.e. starts with "-----BEGIN CERTIFICATE-----").
42+
caBundle:
43+
if std.startsWith(caBundle, '-----BEGIN CERTIFICATE-----') then
44+
std.base64(caBundle)
45+
else
46+
caBundle,
3947
},
4048
}
4149
for w in super.webhooks

tests/external-certificates.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ parameters:
77
webhook:
88
tls.key: ?{vaultkv:${cluster:tenant}/${cluster:name}/patch-operator/webhook-cert/key}
99
tls.crt: ?{vaultkv:${cluster:tenant}/${cluster:name}/patch-operator/webhook-cert/cert}
10-
ca.crt: ?{vaultkv:${cluster:tenant}/${cluster:name}/patch-operator/webhook-cert/cacert}
10+
ca.crt: |-
11+
-----BEGIN CERTIFICATE-----
12+
MII.....
13+
-----END CERTIFICATE-----
1114
1215
helm_values:
1316
enableCertManager: false

tests/golden/external-certificates/patch-operator/patch-operator/01_helm_chart/patch-operator/templates/admissionregistration.k8s.io_v1_mutatingwebhookconfiguration_patch-operator-mutating-webhook-configuration.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ webhooks:
99
- admissionReviewVersions:
1010
- v1
1111
clientConfig:
12-
caBundle: ?{vaultkv:t-silent-test-1234/c-green-test-1234/patch-operator/webhook-cert/cacert}
12+
caBundle: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSS4uLi4uCi0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0=
1313
service:
1414
name: patch-operator-webhook-service
1515
namespace: syn-patch-operator

tests/golden/external-certificates/patch-operator/patch-operator/01_helm_chart/patch-operator/templates/admissionregistration.k8s.io_v1_validatingwebhookconfiguration_patch-operator-validating-webhook-configuration.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ webhooks:
99
- admissionReviewVersions:
1010
- v1
1111
clientConfig:
12-
caBundle: ?{vaultkv:t-silent-test-1234/c-green-test-1234/patch-operator/webhook-cert/cacert}
12+
caBundle: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSS4uLi4uCi0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0=
1313
service:
1414
name: patch-operator-webhook-service
1515
namespace: syn-patch-operator

tests/golden/external-certificates/patch-operator/patch-operator/20_external_certificates.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ metadata:
1919
name: webhook-server-cert
2020
name: webhook-server-cert
2121
stringData:
22-
ca.crt: t-silent-test-1234/c-green-test-1234/patch-operator/webhook-cert/cacert
22+
ca.crt: '-----BEGIN CERTIFICATE-----
23+
24+
MII.....
25+
26+
-----END CERTIFICATE-----'
2327
tls.crt: t-silent-test-1234/c-green-test-1234/patch-operator/webhook-cert/cert
2428
tls.key: t-silent-test-1234/c-green-test-1234/patch-operator/webhook-cert/key
2529
type: kubernetes.io/tls

0 commit comments

Comments
 (0)