Skip to content

Commit b860ff5

Browse files
authored
{AKS} Add custom ca certs to aks create and aks update (#5561)
1 parent 2b3647b commit b860ff5

14 files changed

+966
-1
lines changed

linter_exclusions.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ aks create:
103103
node_public_ip_tags:
104104
rule_exclusions:
105105
- option_length_too_long
106+
custom_ca_trust_certificates:
107+
rule_exclusions:
108+
- option_length_too_long
106109
aks addon enable:
107110
parameters:
108111
appgw_watch_namespace:
@@ -218,6 +221,9 @@ aks update:
218221
ksm_metric_labels_allow_list:
219222
rule_exclusions:
220223
- option_length_too_long
224+
custom_ca_trust_certificates:
225+
rule_exclusions:
226+
- option_length_too_long
221227
arcdata dc create:
222228
parameters:
223229
logs_ui_private_key_file:

src/aks-preview/HISTORY.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ Pending
1414

1515
* Update the minimum required cli core version to `2.43.0`.
1616

17+
0.5.119
18+
+++++++
19+
20+
* Add `--custom-ca-trust-certificates` option for custom CA in aks create and aks update
21+
* Bumped dependency on azure-cli-core
22+
1723
0.5.118
1824
+++++++
1925

src/aks-preview/azext_aks_preview/_consts.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,20 @@
196196
# tag_name gives latest version released.
197197
# Moving away from 1:n release to avoid unwanted breaking changes with auto upgrades.
198198
CONST_DRAFT_CLI_VERSION = "v0.0.22"
199+
200+
CONST_CUSTOM_CA_TEST_CERT = '-----BEGIN CERTIFICATE-----\n' \
201+
'MIICljCCAX4CCQC9zUAgqqqrWzANBgkqhkiG9w0BAQsFADANMQswCQYDVQQGEwJQ\n' \
202+
'TDAeFw0yMjA5MTQwNjIzMjdaFw0yMjA5MTUwNjIzMjdaMA0xCzAJBgNVBAYTAlBM\n' \
203+
'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAopKNIIbvvcPCw9fc4KLX\n' \
204+
'KDtRZobp5L+/1hCN+3OGhk5NvSTpSUrFifxqc0o3IF7YkO3K1n2jAvCMXO16Bf9b\n' \
205+
'OAR7VkCrwGFVkXNjM4wvXAX8CNNvjqd1zDPXSKdE7Wd8k3fTzx6nGUM0UgljIPhH\n' \
206+
'yh4a4Zujd5Ig2P/ZSX0pGJm47JTtMu7MDFHVM5wRWcCrN/H0TCYPIvEOs0B8AZxc\n' \
207+
'p3TF7A6veT5U9pVhQ3Xl9JN6LvvLqPxG3ea10rdv9DYzaiXmSY3ujI3Ri1Q11uWC\n' \
208+
'dtrFIpFu5cHW2OBW+jBXxL0v8xQmkxTLik4BR/PLCl30wxKQNsq3pjDgu0mutKuu\n' \
209+
'5wIDAQABMA0GCSqGSIb3DQEBCwUAA4IBAQAVEAIs/hLwTVCwpEXdoXR24LelNNuB\n' \
210+
'/8ptK6lyjE11XwfMN3yy7F2oB1lrA4rI3j9obpDsHDJBNB13bi/lKgvAcbIn/Tyu\n' \
211+
'RKThtUdPgxNnqDUyxnb3OofMF3gB8ePTu+jZpd3zrlEuxdl40ByATCSyOgR6DHMt\n' \
212+
'SDd+joypnOHFAeSM+V0AaTelXSCK9OAWSAp5e6S76a6lRx+D5Xl3hBedBI0tX59h\n' \
213+
'tEYNEGZaRElFU79WcEF0cH+ZW0+jJ95xE3thZffRz6QI6yF63m8aC9l9bbdJS2zg\n' \
214+
'Yv8W+lCZi//ODeOBUugr++z9uj+vGk47JDSpV0n4JOun3ALUDJ0gqmcS\n' \
215+
'-----END CERTIFICATE-----'

src/aks-preview/azext_aks_preview/_help.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,10 @@
477477
- name: --enable-custom-ca-trust
478478
type: bool
479479
short-summary: Enable Custom CA Trust on agent node pool.
480+
- name: --ca-certs --custom-ca-trust-certificates
481+
type: string
482+
short-summary: Path to a file containing up to 10 blank line separated certificates. Only valid for linux nodes.
483+
long-summary: These certificates are used by Custom CA Trust features and will be added to trust stores of nodes. Requires Custom CA Trust to be enabled on the node.
480484
- name: --enable-keda
481485
type: bool
482486
short-summary: Enable KEDA workload auto-scaler.
@@ -914,6 +918,10 @@
914918
type: string
915919
short-summary: Public key path or key contents to install on node VMs for SSH access. For example,
916920
'ssh-rsa AAAAB...snip...UcyupgH azureuser@linuxvm'.
921+
- name: --ca-certs --custom-ca-trust-certificates
922+
type: string
923+
short-summary: Path to a file containing up to 10 blank line separated certificates. Only valid for linux nodes.
924+
long-summary: These certificates are used by Custom CA Trust features and will be added to trust stores of nodes. Requires Custom CA Trust to be enabled on the node.
917925
examples:
918926
- name: Reconcile the cluster back to its current state.
919927
text: az aks update -g MyResourceGroup -n MyManagedCluster

src/aks-preview/azext_aks_preview/_params.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
validate_vm_set_type,
124124
validate_vnet_subnet_id,
125125
validate_enable_custom_ca_trust,
126+
validate_custom_ca_trust_certificates,
126127
validate_defender_config_parameter,
127128
validate_defender_disable_and_enable_parameters,
128129
validate_azuremonitorworkspaceresourceid,
@@ -348,6 +349,7 @@ def load_arguments(self, _):
348349
c.argument('workload_runtime', arg_type=get_enum_type(workload_runtimes), default=CONST_WORKLOAD_RUNTIME_OCI_CONTAINER)
349350
# no validation for aks create because it already only supports Linux.
350351
c.argument('enable_custom_ca_trust', action='store_true')
352+
c.argument('custom_ca_trust_certificates', options_list=["--custom-ca-trust-certificates", "--ca-certs"], is_preview=True, help="path to file containing list of new line separated CAs")
351353
c.argument('enable_vpa', action='store_true', is_preview=True, help="enable vertical pod autoscaler for cluster")
352354
c.argument('nodepool_allowed_host_ports', validator=validate_allowed_host_ports, is_preview=True, help="allowed host ports for agentpool")
353355
c.argument('nodepool_asg_ids', validator=validate_application_security_groups, is_preview=True, help="application security groups for agentpool")
@@ -459,6 +461,7 @@ def load_arguments(self, _):
459461
c.argument('enable_vpa', action='store_true', is_preview=True, help="enable vertical pod autoscaler for cluster")
460462
c.argument('disable_vpa', action='store_true', is_preview=True, help="disable vertical pod autoscaler for cluster")
461463
c.argument('cluster_snapshot_id', validator=validate_cluster_snapshot_id, is_preview=True)
464+
c.argument('custom_ca_trust_certificates', options_list=["--custom-ca-trust-certificates", "--ca-certs"], validator=validate_custom_ca_trust_certificates, is_preview=True, help="path to file containing list of new line separated CAs")
462465

463466
with self.argument_context('aks upgrade') as c:
464467
c.argument('kubernetes_version', completer=get_k8s_upgrades_completion_list)

src/aks-preview/azext_aks_preview/_validators.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,14 @@ def validate_enable_custom_ca_trust(namespace):
663663
'--enable_custom_ca_trust can only be set for Linux nodepools')
664664

665665

666+
def validate_custom_ca_trust_certificates(namespace):
667+
"""Validates Custom CA Trust Certificates can only be used on Linux."""
668+
if namespace.custom_ca_trust_certificates is not None and namespace.custom_ca_trust_certificates != "":
669+
if hasattr(namespace, 'os_type') and namespace.os_type != "Linux":
670+
raise ArgumentUsageError(
671+
'--custom-ca-trust-certificates can only be set for linux nodepools')
672+
673+
666674
def validate_disable_windows_outbound_nat(namespace):
667675
"""Validates disable_windows_outbound_nat can only be used on Windows."""
668676
if namespace.disable_windows_outbound_nat:

src/aks-preview/azext_aks_preview/custom.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,7 @@ def aks_create(
680680
enable_node_restriction=False,
681681
enable_vpa=False,
682682
enable_cilium_dataplane=False,
683+
custom_ca_trust_certificates=None,
683684
# nodepool
684685
host_group_id=None,
685686
crg_id=None,
@@ -819,6 +820,7 @@ def aks_update(
819820
disable_vpa=False,
820821
cluster_snapshot_id=None,
821822
ssh_key_value=None,
823+
custom_ca_trust_certificates=None
822824
):
823825
# DO NOT MOVE: get all the original parameters and save them as a dictionary
824826
raw_parameters = locals()

src/aks-preview/azext_aks_preview/managed_cluster_decorator.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# --------------------------------------------------------------------------------------------
55

66
import os
7+
from base64 import b64encode
78
from types import SimpleNamespace
89
from typing import Dict, List, Optional, Tuple, TypeVar, Union
910

@@ -41,6 +42,7 @@
4142
from azure.cli.core.commands import AzCliCommand
4243
from azure.cli.core.profiles import ResourceType
4344
from azure.cli.core.util import get_file_json
45+
from azure.cli.core.util import read_file_content
4446
from knack.log import get_logger
4547
from knack.prompting import prompt_y_n
4648

@@ -1979,6 +1981,31 @@ def get_disable_keda(self) -> bool:
19791981
"""
19801982
return self._get_disable_keda(enable_validation=True)
19811983

1984+
def get_custom_ca_trust_certificates(self) -> Union[List[bytes], None]:
1985+
"""Obtain the value of custom ca trust certificates.
1986+
1987+
:return: List[str] or None
1988+
"""
1989+
custom_ca_certs_file_path = self.raw_param.get("custom_ca_trust_certificates")
1990+
if not custom_ca_certs_file_path:
1991+
return None
1992+
if not os.path.isfile(custom_ca_certs_file_path):
1993+
raise InvalidArgumentValueError(
1994+
"{} is not valid file, or not accessible.".format(
1995+
custom_ca_certs_file_path
1996+
)
1997+
)
1998+
# CAs are supposed to be separated with a new line, we filter out empty strings (e.g. some stray new line). We only allow up to 10 CAs
1999+
file_content = read_file_content(custom_ca_certs_file_path).split(os.linesep + os.linesep)
2000+
certs = [str.encode(x) for x in file_content if len(x) > 1]
2001+
if len(certs) > 10:
2002+
raise InvalidArgumentValueError(
2003+
"Only up to 10 new-line separated CAs can be passed, got {} instead.".format(
2004+
len(certs)
2005+
)
2006+
)
2007+
return certs
2008+
19822009
def get_defender_config(self) -> Union[ManagedClusterSecurityProfileDefender, None]:
19832010
"""Obtain the value of defender.
19842011
@@ -2559,6 +2586,22 @@ def set_up_defender(self, mc: ManagedCluster) -> ManagedCluster:
25592586

25602587
return mc
25612588

2589+
def set_up_custom_ca_trust_certificates(self, mc: ManagedCluster) -> ManagedCluster:
2590+
"""Set up Custom CA Trust Certificates for the ManagedCluster object.
2591+
2592+
:return: the ManagedCluster object
2593+
"""
2594+
self._ensure_mc(mc)
2595+
2596+
ca_certs = self.context.get_custom_ca_trust_certificates()
2597+
if ca_certs:
2598+
if mc.security_profile is None:
2599+
mc.security_profile = self.models.ManagedClusterSecurityProfile()
2600+
2601+
mc.security_profile.custom_ca_trust_certificates = ca_certs
2602+
2603+
return mc
2604+
25622605
def set_up_node_restriction(self, mc: ManagedCluster) -> ManagedCluster:
25632606
"""Set up security profile nodeRestriction for the ManagedCluster object.
25642607
@@ -2645,6 +2688,8 @@ def construct_mc_profile_preview(self, bypass_restore_defaults: bool = False) ->
26452688
mc = self.set_up_vpa(mc)
26462689
# set up kube-proxy config
26472690
mc = self.set_up_kube_proxy_config(mc)
2691+
# set up custom ca trust certificates
2692+
mc = self.set_up_custom_ca_trust_certificates(mc)
26482693

26492694
# DO NOT MOVE: keep this at the bottom, restore defaults
26502695
mc = self._restore_defaults_in_mc(mc)
@@ -3069,6 +3114,22 @@ def update_defender(self, mc: ManagedCluster) -> ManagedCluster:
30693114

30703115
return mc
30713116

3117+
def update_custom_ca_trust_certificates(self, mc: ManagedCluster) -> ManagedCluster:
3118+
"""Update Custom CA Trust Certificates for the ManagedCluster object.
3119+
3120+
:return: the ManagedCluster object
3121+
"""
3122+
self._ensure_mc(mc)
3123+
3124+
ca_certs = self.context.get_custom_ca_trust_certificates()
3125+
if ca_certs:
3126+
if mc.security_profile is None:
3127+
mc.security_profile = self.models.ManagedClusterSecurityProfile()
3128+
3129+
mc.security_profile.custom_ca_trust_certificates = ca_certs
3130+
3131+
return mc
3132+
30723133
def update_azure_monitor_profile(self, mc: ManagedCluster) -> ManagedCluster:
30733134
"""Update azure monitor profile for the ManagedCluster object.
30743135
:return: the ManagedCluster object
@@ -3246,5 +3307,7 @@ def update_mc_profile_preview(self) -> ManagedCluster:
32463307
mc = self.update_outbound_type_in_network_profile(mc)
32473308
# update kube proxy config
32483309
mc = self.update_kube_proxy_config(mc)
3310+
# update custom ca trust certificates
3311+
mc = self.update_custom_ca_trust_certificates(mc)
32493312

32503313
return mc
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIICljCCAX4CCQC9zUAgqqqrWzANBgkqhkiG9w0BAQsFADANMQswCQYDVQQGEwJQ
3+
TDAeFw0yMjA5MTQwNjIzMjdaFw0yMjA5MTUwNjIzMjdaMA0xCzAJBgNVBAYTAlBM
4+
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAopKNIIbvvcPCw9fc4KLX
5+
KDtRZobp5L+/1hCN+3OGhk5NvSTpSUrFifxqc0o3IF7YkO3K1n2jAvCMXO16Bf9b
6+
OAR7VkCrwGFVkXNjM4wvXAX8CNNvjqd1zDPXSKdE7Wd8k3fTzx6nGUM0UgljIPhH
7+
yh4a4Zujd5Ig2P/ZSX0pGJm47JTtMu7MDFHVM5wRWcCrN/H0TCYPIvEOs0B8AZxc
8+
p3TF7A6veT5U9pVhQ3Xl9JN6LvvLqPxG3ea10rdv9DYzaiXmSY3ujI3Ri1Q11uWC
9+
dtrFIpFu5cHW2OBW+jBXxL0v8xQmkxTLik4BR/PLCl30wxKQNsq3pjDgu0mutKuu
10+
5wIDAQABMA0GCSqGSIb3DQEBCwUAA4IBAQAVEAIs/hLwTVCwpEXdoXR24LelNNuB
11+
/8ptK6lyjE11XwfMN3yy7F2oB1lrA4rI3j9obpDsHDJBNB13bi/lKgvAcbIn/Tyu
12+
RKThtUdPgxNnqDUyxnb3OofMF3gB8ePTu+jZpd3zrlEuxdl40ByATCSyOgR6DHMt
13+
SDd+joypnOHFAeSM+V0AaTelXSCK9OAWSAp5e6S76a6lRx+D5Xl3hBedBI0tX59h
14+
tEYNEGZaRElFU79WcEF0cH+ZW0+jJ95xE3thZffRz6QI6yF63m8aC9l9bbdJS2zg
15+
Yv8W+lCZi//ODeOBUugr++z9uj+vGk47JDSpV0n4JOun3ALUDJ0gqmcS
16+
-----END CERTIFICATE-----
17+
18+
-----BEGIN CERTIFICATE-----
19+
MIICljCCAX4CCQC9zUAgqqqrWzANBgkqhkiG9w0BAQsFADANMQswCQYDVQQGEwJQ
20+
TDAeFw0yMjA5MTQwNjIzMjdaFw0yMjA5MTUwNjIzMjdaMA0xCzAJBgNVBAYTAlBM
21+
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAopKNIIbvvcPCw9fc4KLX
22+
KDtRZobp5L+/1hCN+3OGhk5NvSTpSUrFifxqc0o3IF7YkO3K1n2jAvCMXO16Bf9b
23+
OAR7VkCrwGFVkXNjM4wvXAX8CNNvjqd1zDPXSKdE7Wd8k3fTzx6nGUM0UgljIPhH
24+
yh4a4Zujd5Ig2P/ZSX0pGJm47JTtMu7MDFHVM5wRWcCrN/H0TCYPIvEOs0B8AZxc
25+
p3TF7A6veT5U9pVhQ3Xl9JN6LvvLqPxG3ea10rdv9DYzaiXmSY3ujI3Ri1Q11uWC
26+
dtrFIpFu5cHW2OBW+jBXxL0v8xQmkxTLik4BR/PLCl30wxKQNsq3pjDgu0mutKuu
27+
5wIDAQABMA0GCSqGSIb3DQEBCwUAA4IBAQAVEAIs/hLwTVCwpEXdoXR24LelNNuB
28+
/8ptK6lyjE11XwfMN3yy7F2oB1lrA4rI3j9obpDsHDJBNB13bi/lKgvAcbIn/Tyu
29+
RKThtUdPgxNnqDUyxnb3OofMF3gB8ePTu+jZpd3zrlEuxdl40ByATCSyOgR6DHMt
30+
SDd+joypnOHFAeSM+V0AaTelXSCK9OAWSAp5e6S76a6lRx+D5Xl3hBedBI0tX59h
31+
tEYNEGZaRElFU79WcEF0cH+ZW0+jJ95xE3thZffRz6QI6yF63m8aC9l9bbdJS2zg
32+
Yv8W+lCZi//ODeOBUugr++z9uj+vGk47JDSpV0n4JOun3ALUDJ0gqmcS
33+
-----END CERTIFICATE-----

0 commit comments

Comments
 (0)