Skip to content

Commit 84defaa

Browse files
cameronmeissnerCameron Meissner
andauthored
cherry-pick: refactor: manually generate self-signed kubelet serving cert when opting out of certificate rotation (#5526)
Co-authored-by: Cameron Meissner <cameissner@microsoft.com>
1 parent b76978a commit 84defaa

File tree

360 files changed

+7083
-2576
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

360 files changed

+7083
-2576
lines changed

e2e/scenario_test.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,15 +1039,17 @@ func Test_Ubuntu2204DisableKubeletServingCertificateRotationWithTags(t *testing.
10391039
vmss.Tags["aks-disable-kubelet-serving-certificate-rotation"] = to.Ptr("true")
10401040
},
10411041
LiveVMValidators: []*LiveVMValidator{
1042+
FileHasContentsValidator("/etc/default/kubelet", "\\-\\-tls-cert-file=/etc/kubernetes/certs/kubeletserver.crt"),
1043+
FileHasContentsValidator("/etc/default/kubelet", "\\-\\-tls-private-key-file=/etc/kubernetes/certs/kubeletserver.key"),
10421044
FileExcludesContentsValidator("/etc/default/kubelet", "\\-\\-rotate-server-certificates=true", "\\-\\-rotate-server-certificates=true"),
10431045
FileExcludesContentsValidator("/etc/default/kubelet", "kubernetes.azure.com/kubelet-serving-ca=cluster", "kubernetes.azure.com/kubelet-serving-ca=cluster"),
1044-
FileHasContentsValidator("/etc/default/kubelet", "\\-\\-rotate-server-certificates=false"),
1046+
DirectoryValidator("/etc/kubernetes/certs", []string{"kubeletserver.crt", "kubeletserver.key"}),
10451047
},
10461048
},
10471049
})
10481050
}
10491051

1050-
func Test_Ubuntu2204DisableKubeletServingCertificateRotationWithTags_CustomKubeletConfig(t *testing.T) {
1052+
func Test_Ubuntu2204_DisableKubeletServingCertificateRotationWithTags_CustomKubeletConfig(t *testing.T) {
10511053
RunScenario(t, &Scenario{
10521054
Tags: Tags{
10531055
ServerTLSBootstrapping: true,
@@ -1080,10 +1082,12 @@ func Test_Ubuntu2204DisableKubeletServingCertificateRotationWithTags_CustomKubel
10801082
vmss.Tags["aks-disable-kubelet-serving-certificate-rotation"] = to.Ptr("true")
10811083
},
10821084
LiveVMValidators: []*LiveVMValidator{
1085+
FileHasContentsValidator("/etc/default/kubeletconfig.json", "\"tlsCertFile\": \"/etc/kubernetes/certs/kubeletserver.crt\""),
1086+
FileHasContentsValidator("/etc/default/kubeletconfig.json", "\"tlsPrivateKeyFile\": \"/etc/kubernetes/certs/kubeletserver.key\""),
10831087
FileExcludesContentsValidator("/etc/default/kubelet", "\\-\\-rotate-server-certificates=true", "\\-\\-rotate-server-certificates=true"),
10841088
FileExcludesContentsValidator("/etc/default/kubelet", "kubernetes.azure.com/kubelet-serving-ca=cluster", "kubernetes.azure.com/kubelet-serving-ca=cluster"),
10851089
FileExcludesContentsValidator("/etc/default/kubeletconfig.json", "\"serverTLSBootstrap\": true", "serverTLSBootstrap: true"),
1086-
FileHasContentsValidator("/etc/default/kubeletconfig.json", "\"serverTLSBootstrap\": false"),
1090+
DirectoryValidator("/etc/kubernetes/certs", []string{"kubeletserver.crt", "kubeletserver.key"}),
10871091
},
10881092
},
10891093
})
@@ -1109,9 +1113,11 @@ func Test_Ubuntu2204DisableKubeletServingCertificateRotationWithTags_AlreadyDisa
11091113
vmss.Tags["aks-disable-kubelet-serving-certificate-rotation"] = to.Ptr("true")
11101114
},
11111115
LiveVMValidators: []*LiveVMValidator{
1116+
FileHasContentsValidator("/etc/default/kubelet", "\\-\\-tls-cert-file=/etc/kubernetes/certs/kubeletserver.crt"),
1117+
FileHasContentsValidator("/etc/default/kubelet", "\\-\\-tls-private-key-file=/etc/kubernetes/certs/kubeletserver.key"),
11121118
FileExcludesContentsValidator("/etc/default/kubelet", "\\-\\-rotate-server-certificates=true", "\\-\\-rotate-server-certificates=true"),
11131119
FileExcludesContentsValidator("/etc/default/kubelet", "kubernetes.azure.com/kubelet-serving-ca=cluster", "kubernetes.azure.com/kubelet-serving-ca=cluster"),
1114-
FileExcludesContentsValidator("/etc/default/kubeletconfig.json", "\"serverTLSBootstrap\": true", "serverTLSBootstrap: true"),
1120+
DirectoryValidator("/etc/kubernetes/certs", []string{"kubeletserver.crt", "kubeletserver.key"}),
11151121
},
11161122
},
11171123
})
@@ -1145,9 +1151,12 @@ func Test_Ubuntu2204DisableKubeletServingCertificateRotationWithTags_AlreadyDisa
11451151
vmss.Tags["aks-disable-kubelet-serving-certificate-rotation"] = to.Ptr("true")
11461152
},
11471153
LiveVMValidators: []*LiveVMValidator{
1154+
FileHasContentsValidator("/etc/default/kubeletconfig.json", "\"tlsCertFile\": \"/etc/kubernetes/certs/kubeletserver.crt\""),
1155+
FileHasContentsValidator("/etc/default/kubeletconfig.json", "\"tlsPrivateKeyFile\": \"/etc/kubernetes/certs/kubeletserver.key\""),
11481156
FileExcludesContentsValidator("/etc/default/kubelet", "\\-\\-rotate-server-certificates=true", "\\-\\-rotate-server-certificates=true"),
11491157
FileExcludesContentsValidator("/etc/default/kubelet", "kubernetes.azure.com/kubelet-serving-ca=cluster", "kubernetes.azure.com/kubelet-serving-ca=cluster"),
11501158
FileExcludesContentsValidator("/etc/default/kubeletconfig.json", "\"serverTLSBootstrap\": true", "serverTLSBootstrap: true"),
1159+
DirectoryValidator("/etc/kubernetes/certs", []string{"kubeletserver.crt", "kubeletserver.key"}),
11511160
},
11521161
},
11531162
})

parts/linux/cloud-init/artifacts/cse_config.sh

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -164,15 +164,6 @@ EOF
164164
systemctl restart containerd
165165
}
166166

167-
# this simply generates a self-signed certificate used for serving by the kubelet
168-
configureKubeletServerCert() {
169-
KUBELET_SERVER_PRIVATE_KEY_PATH="/etc/kubernetes/certs/kubeletserver.key"
170-
KUBELET_SERVER_CERT_PATH="/etc/kubernetes/certs/kubeletserver.crt"
171-
172-
openssl genrsa -out $KUBELET_SERVER_PRIVATE_KEY_PATH 2048
173-
openssl req -new -x509 -days 7300 -key $KUBELET_SERVER_PRIVATE_KEY_PATH -out $KUBELET_SERVER_CERT_PATH -subj "/CN=${NODE_NAME}" -addext "subjectAltName=DNS:${NODE_NAME}"
174-
}
175-
176167
configureK8s() {
177168
mkdir -p "/etc/kubernetes/certs"
178169

@@ -249,15 +240,6 @@ EOF
249240
sed -i "/cloudProviderBackoffJitter/d" /etc/kubernetes/azure.json
250241
fi
251242

252-
# generate a kubelet serving certificate if we aren't relying on TLS bootstrapping to generate one for us.
253-
# NOTE: in the case where ENABLE_KUBELET_SERVING_CERTIFICATE_ROTATION is true but
254-
# the customer has disabled serving certificate rotation via nodepool tags,
255-
# the self-signed serving certificate will be bootstrapped by the kubelet instead of this function
256-
# TODO(cameissner): remove configureKubeletServerCert altogether
257-
if [ "${ENABLE_KUBELET_SERVING_CERTIFICATE_ROTATION}" != "true" ]; then
258-
configureKubeletServerCert
259-
fi
260-
261243
if [ "${IS_CUSTOM_CLOUD}" == "true" ]; then
262244
set +x
263245
AKS_CUSTOM_CLOUD_JSON_PATH="/etc/kubernetes/${TARGET_ENVIRONMENT}.json"
@@ -423,12 +405,25 @@ getPrimaryNicIP() {
423405
echo "$ip"
424406
}
425407

426-
configureKubeletServingCertificateRotation() {
408+
generateSelfSignedKubeletServingCertificate() {
409+
KUBELET_SERVER_PRIVATE_KEY_PATH="/etc/kubernetes/certs/kubeletserver.key"
410+
KUBELET_SERVER_CERT_PATH="/etc/kubernetes/certs/kubeletserver.crt"
411+
412+
openssl genrsa -out $KUBELET_SERVER_PRIVATE_KEY_PATH 2048
413+
openssl req -new -x509 -days 7300 -key $KUBELET_SERVER_PRIVATE_KEY_PATH -out $KUBELET_SERVER_CERT_PATH -subj "/CN=${NODE_NAME}" -addext "subjectAltName=DNS:${NODE_NAME}"
414+
}
415+
416+
configureKubeletServing() {
427417
if [ "${ENABLE_KUBELET_SERVING_CERTIFICATE_ROTATION}" != "true" ]; then
428-
echo "kubelet serving certificate rotation is disabled, nothing to configure"
418+
echo "kubelet serving certificate rotation is disabled, generating self-signed serving certificate with openssl"
419+
generateSelfSignedKubeletServingCertificate
429420
return 0
430421
fi
431422

423+
KUBELET_SERVING_CERTIFICATE_ROTATION_LABEL="kubernetes.azure.com/kubelet-serving-ca=cluster"
424+
KUBELET_SERVER_PRIVATE_KEY_PATH="/etc/kubernetes/certs/kubeletserver.key"
425+
KUBELET_SERVER_CERT_PATH="/etc/kubernetes/certs/kubeletserver.crt"
426+
432427
# check if kubelet serving certificate rotation is disabled by customer-specified nodepool tags
433428
export -f should_disable_kubelet_serving_certificate_rotation
434429
DISABLE_KUBELET_SERVING_CERTIFICATE_ROTATION=$(retrycmd_if_failure_no_stats 10 1 10 bash -cx should_disable_kubelet_serving_certificate_rotation)
@@ -437,27 +432,43 @@ configureKubeletServingCertificateRotation() {
437432
exit $ERR_LOOKUP_DISABLE_KUBELET_SERVING_CERTIFICATE_ROTATION_TAG
438433
fi
439434

440-
KUBELET_SERVING_CERTIFICATE_ROTATION_LABEL="kubernetes.azure.com/kubelet-serving-ca=cluster"
441-
442-
if [ "${DISABLE_KUBELET_SERVING_CERTIFICATE_ROTATION,,}" == "true" ]; then
443-
echo "kubelet serving certificate rotation is disabled by nodepool tags, reconfiguring kubelet flags and node labels"
435+
if [ "${DISABLE_KUBELET_SERVING_CERTIFICATE_ROTATION}" == "true" ]; then
436+
echo "kubelet serving certificate rotation is disabled by nodepool tags"
444437

445-
# set the --rotate-server-certificates flag to false if needed
438+
# set --rotate-server-certificates flag and serverTLSBootstrap config file field to false
439+
echo "reconfiguring kubelet flags and config as needed"
446440
KUBELET_FLAGS="${KUBELET_FLAGS/--rotate-server-certificates=true/--rotate-server-certificates=false}"
447-
448-
if [ "${KUBELET_CONFIG_FILE_ENABLED,,}" == "true" ]; then
441+
if [ "${KUBELET_CONFIG_FILE_ENABLED}" == "true" ]; then
449442
set +x
450-
# set the serverTLSBootstrap property to false if needed
451443
KUBELET_CONFIG_FILE_CONTENT=$(echo "$KUBELET_CONFIG_FILE_CONTENT" | base64 -d | jq 'if .serverTLSBootstrap == true then .serverTLSBootstrap = false else . end' | base64)
452444
set -x
453445
fi
454446

447+
# manually generate kubelet's self-signed serving certificate
448+
echo "generating self-signed serving certificate with openssl"
449+
generateSelfSignedKubeletServingCertificate
450+
451+
# make sure to eliminate the kubelet serving node label
452+
echo "removing node label $KUBELET_SERVING_CERTIFICATE_ROTATION_LABEL"
455453
removeKubeletNodeLabel $KUBELET_SERVING_CERTIFICATE_ROTATION_LABEL
456-
return 0
454+
else
455+
echo "kubelet serving certificate rotation is enabled"
456+
457+
# remove the --tls-cert-file and --tls-private-key-file flags, which are incompatible with serving certificate rotation
458+
# NOTE: this step will not be needed once these flags are no longer defaulted by the bootstrapper
459+
echo "removing --tls-cert-file and --tls-private-key-file from kubelet flags"
460+
removeKubeletFlag "--tls-cert-file=$KUBELET_SERVER_CERT_PATH"
461+
removeKubeletFlag "--tls-private-key-file=$KUBELET_SERVER_PRIVATE_KEY_PATH"
462+
if [ "${KUBELET_CONFIG_FILE_ENABLED}" == "true" ]; then
463+
set +x
464+
KUBELET_CONFIG_FILE_CONTENT=$(echo "$KUBELET_CONFIG_FILE_CONTENT" | base64 -d | jq 'del(.tlsCertFile)' | jq 'del(.tlsPrivateKeyFile)' | base64)
465+
set -x
466+
fi
467+
468+
# make sure to add the kubelet serving node label
469+
echo "adding node label $KUBELET_SERVING_CERTIFICATE_ROTATION_LABEL if needed"
470+
addKubeletNodeLabel $KUBELET_SERVING_CERTIFICATE_ROTATION_LABEL
457471
fi
458-
459-
echo "kubelet serving certificate rotation is enabled, will add node label if needed"
460-
addKubeletNodeLabel $KUBELET_SERVING_CERTIFICATE_ROTATION_LABEL
461472
}
462473

463474
ensureKubelet() {

parts/linux/cloud-init/artifacts/cse_helpers.sh

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ should_disable_kubelet_serving_certificate_rotation() {
470470
return $ret
471471
fi
472472
should_disable=$(echo "$body" | jq -r '.compute.tagsList[] | select(.name == "aks-disable-kubelet-serving-certificate-rotation") | .value')
473-
echo "$should_disable"
473+
echo "${should_disable,,}"
474474
}
475475

476476
isMarinerOrAzureLinux() {
@@ -644,4 +644,16 @@ removeKubeletNodeLabel() {
644644
fi
645645
}
646646

647+
# removes the specified FLAG_STRING (which should be in the form of 'key=value') from KUBELET_FLAGS
648+
removeKubeletFlag() {
649+
local FLAG_STRING=$1
650+
if grep -e ",${FLAG_STRING}" <<< "$KUBELET_FLAGS" > /dev/null 2>&1; then
651+
KUBELET_FLAGS="${KUBELET_FLAGS/,${FLAG_STRING}/}"
652+
elif grep -e "${FLAG_STRING}," <<< "$KUBELET_FLAGS" > /dev/null 2>&1; then
653+
KUBELET_FLAGS="${KUBELET_FLAGS/${FLAG_STRING},/}"
654+
elif grep -e "${FLAG_STRING}" <<< "$KUBELET_FLAGS" > /dev/null 2>&1; then
655+
KUBELET_FLAGS="${KUBELET_FLAGS/${FLAG_STRING}/}"
656+
fi
657+
}
658+
647659
#HELPERSEOF

parts/linux/cloud-init/artifacts/cse_main.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ mkdir -p "/etc/systemd/system/kubelet.service.d"
222222

223223
# IMPORTANT NOTE: We do this here since this function can mutate kubelet flags and node labels,
224224
# which is used by configureK8s and other functions. Thus, we need to make sure flag and label content is correct beforehand.
225-
logs_to_events "AKS.CSE.configureKubeletServingCertificateRotation" configureKubeletServingCertificateRotation
225+
logs_to_events "AKS.CSE.configureKubeletServing" configureKubeletServing
226226

227227
logs_to_events "AKS.CSE.configureK8s" configureK8s
228228

pkg/agent/baker.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -300,10 +300,6 @@ func ValidateAndSetLinuxNodeBootstrappingConfiguration(config *datamodel.NodeBoo
300300
if IsKubeletServingCertificateRotationEnabled(config) {
301301
// ensure the required feature gate is set
302302
kubeletFlags["--feature-gates"] = addFeatureGateString(kubeletFlags["--feature-gates"], "RotateKubeletServerCertificate", true)
303-
// backfill deletion of --tls-cert-file and --tls-private-key-file, which are incompatible with --rotate-server-certificates
304-
// these are set as defaults on the RP-side for Linux
305-
delete(kubeletFlags, "--tls-cert-file")
306-
delete(kubeletFlags, "--tls-private-key-file")
307303
}
308304

309305
if IsKubernetesVersionGe(config.ContainerService.Properties.OrchestratorProfile.OrchestratorVersion, "1.24.0") {
@@ -347,9 +343,6 @@ func validateAndSetWindowsNodeBootstrappingConfiguration(config *datamodel.NodeB
347343

348344
if IsKubeletServingCertificateRotationEnabled(config) {
349345
kubeletFlags["--feature-gates"] = addFeatureGateString(kubeletFlags["--feature-gates"], "RotateKubeletServerCertificate", true)
350-
// RP doesn't currently set these flags for windows, though we filter them out anyways just to be safe
351-
delete(kubeletFlags, "--tls-cert-file")
352-
delete(kubeletFlags, "--tls-private-key-file")
353346
}
354347
}
355348
}

pkg/agent/baker_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -876,8 +876,6 @@ var _ = Describe("Assert generated customData and cseCmd", func() {
876876
}, func(o *nodeBootstrappingOutput) {
877877
Expect(o.vars["ENABLE_KUBELET_SERVING_CERTIFICATE_ROTATION"]).To(Equal("true"))
878878
Expect(strings.Contains(o.vars["KUBELET_FLAGS"], "--rotate-server-certificates=true")).To(BeTrue())
879-
Expect(strings.Contains(o.vars["KUBELET_FLAGS"], "--tls-cert-file")).To(BeFalse())
880-
Expect(strings.Contains(o.vars["KUBELET_FLAGS"], "--tls-private-key-file")).To(BeFalse())
881879
}),
882880

883881
Entry("AKSUbuntu2204 with kubelet serving certificate rotation disabled and custom kubelet config",

0 commit comments

Comments
 (0)