|
4 | 4 | # -------------------------------------------------------------------------------------------- |
5 | 5 |
|
6 | 6 | import os |
| 7 | +from base64 import b64encode |
7 | 8 | from types import SimpleNamespace |
8 | 9 | from typing import Dict, List, Optional, Tuple, TypeVar, Union |
9 | 10 |
|
|
41 | 42 | from azure.cli.core.commands import AzCliCommand |
42 | 43 | from azure.cli.core.profiles import ResourceType |
43 | 44 | from azure.cli.core.util import get_file_json |
| 45 | +from azure.cli.core.util import read_file_content |
44 | 46 | from knack.log import get_logger |
45 | 47 | from knack.prompting import prompt_y_n |
46 | 48 |
|
@@ -1979,6 +1981,31 @@ def get_disable_keda(self) -> bool: |
1979 | 1981 | """ |
1980 | 1982 | return self._get_disable_keda(enable_validation=True) |
1981 | 1983 |
|
| 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 | + |
1982 | 2009 | def get_defender_config(self) -> Union[ManagedClusterSecurityProfileDefender, None]: |
1983 | 2010 | """Obtain the value of defender. |
1984 | 2011 |
|
@@ -2559,6 +2586,22 @@ def set_up_defender(self, mc: ManagedCluster) -> ManagedCluster: |
2559 | 2586 |
|
2560 | 2587 | return mc |
2561 | 2588 |
|
| 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 | + |
2562 | 2605 | def set_up_node_restriction(self, mc: ManagedCluster) -> ManagedCluster: |
2563 | 2606 | """Set up security profile nodeRestriction for the ManagedCluster object. |
2564 | 2607 |
|
@@ -2645,6 +2688,8 @@ def construct_mc_profile_preview(self, bypass_restore_defaults: bool = False) -> |
2645 | 2688 | mc = self.set_up_vpa(mc) |
2646 | 2689 | # set up kube-proxy config |
2647 | 2690 | 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) |
2648 | 2693 |
|
2649 | 2694 | # DO NOT MOVE: keep this at the bottom, restore defaults |
2650 | 2695 | mc = self._restore_defaults_in_mc(mc) |
@@ -3069,6 +3114,22 @@ def update_defender(self, mc: ManagedCluster) -> ManagedCluster: |
3069 | 3114 |
|
3070 | 3115 | return mc |
3071 | 3116 |
|
| 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 | + |
3072 | 3133 | def update_azure_monitor_profile(self, mc: ManagedCluster) -> ManagedCluster: |
3073 | 3134 | """Update azure monitor profile for the ManagedCluster object. |
3074 | 3135 | :return: the ManagedCluster object |
@@ -3246,5 +3307,7 @@ def update_mc_profile_preview(self) -> ManagedCluster: |
3246 | 3307 | mc = self.update_outbound_type_in_network_profile(mc) |
3247 | 3308 | # update kube proxy config |
3248 | 3309 | mc = self.update_kube_proxy_config(mc) |
| 3310 | + # update custom ca trust certificates |
| 3311 | + mc = self.update_custom_ca_trust_certificates(mc) |
3249 | 3312 |
|
3250 | 3313 | return mc |
0 commit comments