Skip to content

Commit 4cc1b48

Browse files
author
Jenny Liu
committed
[AKS] Add Pod Security Standards support to Deployment Safeguards
1 parent 8b6c74b commit 4cc1b48

File tree

11 files changed

+1683
-3
lines changed

11 files changed

+1683
-3
lines changed

deploymentsafeguards.json

Lines changed: 448 additions & 0 deletions
Large diffs are not rendered by default.

src/aks-preview/azext_aks_preview/_client_factory.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,7 @@ def cf_load_balancers(cli_ctx, *_):
159159

160160
def cf_jwt_authenticators(cli_ctx, *_):
161161
return get_container_service_client(cli_ctx).jwt_authenticators
162+
163+
164+
def cf_deployment_safeguards(cli_ctx, *_):
165+
return get_container_service_client(cli_ctx).deployment_safeguards

src/aks-preview/azext_aks_preview/_consts.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,11 @@
325325
CONST_SAFEGUARDSLEVEL_WARNING = "Warning"
326326
CONST_SAFEGUARDSLEVEL_ENFORCEMENT = "Enforcement"
327327

328+
# Pod Security Standards Level Consts
329+
CONST_POD_SECURITY_STANDARDS_LEVEL_PRIVILEGED = "Privileged"
330+
CONST_POD_SECURITY_STANDARDS_LEVEL_BASELINE = "Baseline"
331+
CONST_POD_SECURITY_STANDARDS_LEVEL_RESTRICTED = "Restricted"
332+
328333
CONST_AZURE_SERVICE_MESH_MODE_DISABLED = "Disabled"
329334
CONST_AZURE_SERVICE_MESH_MODE_ISTIO = "Istio"
330335
CONST_AZURE_SERVICE_MESH_INGRESS_MODE_EXTERNAL = "External"

src/aks-preview/azext_aks_preview/_help.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,9 @@
564564
- name: --safeguards-excluded-ns
565565
type: string
566566
short-summary: Comma-separated list of Kubernetes namespaces to exclude from deployment safeguards
567+
- name: --pod-security-standards-level
568+
type: string
569+
short-summary: The Pod Security Standards level. Accepted Values are [Privileged, Baseline, Restricted]. Requires safeguards to be enabled
567570
- name: --enable-asm --enable-azure-service-mesh
568571
type: bool
569572
short-summary: Enable Azure Service Mesh.
@@ -762,6 +765,8 @@
762765
text: az aks create -g MyResourceGroup -n MyManagedCluster --safeguards-level Warning --enable-addons azure-policy
763766
- name: Create a kubernetes cluster with safeguards set to "Warning" and some namespaces excluded
764767
text: az aks create -g MyResourceGroup -n MyManagedCluster --safeguards-level Warning --safeguards-excluded-ns ns1,ns2 --enable-addons azure-policy
768+
- name: Create a kubernetes cluster with safeguards and Pod Security Standards set to "Baseline"
769+
text: az aks create -g MyResourceGroup -n MyManagedCluster --safeguards-level Warning --pod-security-standards-level Baseline --enable-addons azure-policy
765770
- name: Create a kubernetes cluster with Azure Service Mesh enabled.
766771
text: az aks create -g MyResourceGroup -n MyManagedCluster --enable-azure-service-mesh
767772
- name: Create a kubernetes cluster with Azure Monitor Metrics enabled.
@@ -1486,6 +1491,8 @@
14861491
text: az aks update -g MyResourceGroup -n MyManagedCluster --disable-azure-monitor-logs
14871492
- name: Update a kubernetes cluster to clear any namespaces excluded from safeguards. Assumes azure policy addon is already enabled
14881493
text: az aks update -g MyResourceGroup -n MyManagedCluster --safeguards-excluded-ns ""
1494+
- name: Update a kubernetes cluster with safeguards and Pod Security Standards set to "Baseline". Assumes azure policy addon is already enabled
1495+
text: az aks update -g MyResourceGroup -n MyManagedCluster --safeguards-level Warning --pod-security-standards-level Baseline
14891496
- name: Update a kubernetes cluster to enable a managed installation of Gateway API CRDs from the standard release channel.
14901497
text: az aks update -g MyResourceGroup -n MyManagedCluster --enable-gateway-api
14911498
- name: Update a kubernetes cluster to disable the managed installation of Gateway API CRDs.
@@ -4347,3 +4354,76 @@
43474354
- name: Show a specific JWT authenticator configuration
43484355
text: az aks jwtauthenticator show -g MyResourceGroup --cluster-name MyCluster --name myjwt
43494356
"""
4357+
4358+
helps['aks safeguards'] = """
4359+
type: group
4360+
short-summary: Manage Deployment Safeguards for a Managed Cluster (preview).
4361+
"""
4362+
4363+
helps['aks safeguards create'] = """
4364+
type: command
4365+
short-summary: Enable Deployment Safeguards for a Managed Cluster with Pod Security Standards support (preview).
4366+
parameters:
4367+
- name: --level
4368+
type: string
4369+
short-summary: The deployment safeguards level. Accepted values are Warn and Enforce.
4370+
- name: --excluded-namespaces --excluded-ns
4371+
type: string
4372+
short-summary: Space-separated list of namespaces to exclude from Deployment Safeguards.
4373+
- name: --pod-security-standards-level
4374+
type: string
4375+
short-summary: The Pod Security Standards level. Accepted values are Privileged, Baseline, Restricted.
4376+
examples:
4377+
- name: Create DeploymentSafeguards at Warn level with Pod Security Standards Baseline
4378+
text: az aks safeguards create -g MyResourceGroup -n MyCluster --level Warn --pod-security-standards-level Baseline
4379+
- name: Create DeploymentSafeguards at Enforce level with excluded namespaces
4380+
text: az aks safeguards create -g MyResourceGroup -n MyCluster --level Enforce --excluded-ns kube-system ns1 ns2
4381+
- name: Create DeploymentSafeguards with all security features enabled
4382+
text: az aks safeguards create -g MyResourceGroup -n MyCluster --level Enforce --pod-security-standards-level Restricted
4383+
"""
4384+
4385+
helps['aks safeguards update'] = """
4386+
type: command
4387+
short-summary: Update Deployment Safeguards configuration for a Managed Cluster with Pod Security Standards support (preview).
4388+
parameters:
4389+
- name: --level
4390+
type: string
4391+
short-summary: The deployment safeguards level. Accepted values are Warn and Enforce.
4392+
- name: --excluded-namespaces --excluded-ns
4393+
type: string
4394+
short-summary: Space-separated list of namespaces to exclude from Deployment Safeguards.
4395+
- name: --pod-security-standards-level
4396+
type: string
4397+
short-summary: The Pod Security Standards level. Accepted values are Privileged, Baseline, Restricted.
4398+
examples:
4399+
- name: Update DeploymentSafeguards to Enforce level
4400+
text: az aks safeguards update -g MyResourceGroup -n MyCluster --level Enforce
4401+
- name: Update Pod Security Standards level to Restricted
4402+
text: az aks safeguards update -g MyResourceGroup -n MyCluster --pod-security-standards-level Restricted
4403+
- name: Update excluded namespaces
4404+
text: az aks safeguards update -g MyResourceGroup -n MyCluster --excluded-ns kube-system custom-ns
4405+
"""
4406+
4407+
helps['aks safeguards show'] = """
4408+
type: command
4409+
short-summary: Show Deployment Safeguards configuration for a Managed Cluster (preview).
4410+
examples:
4411+
- name: Show DeploymentSafeguards configuration
4412+
text: az aks safeguards show -g MyResourceGroup -n MyCluster
4413+
"""
4414+
4415+
helps['aks safeguards delete'] = """
4416+
type: command
4417+
short-summary: Disable Deployment Safeguards for a Managed Cluster (preview).
4418+
examples:
4419+
- name: Delete DeploymentSafeguards
4420+
text: az aks safeguards delete -g MyResourceGroup -n MyCluster
4421+
"""
4422+
4423+
helps['aks safeguards list'] = """
4424+
type: command
4425+
short-summary: List DeploymentSafeguards by parent resource (preview).
4426+
examples:
4427+
- name: List DeploymentSafeguards for a cluster
4428+
text: az aks safeguards list -g MyResourceGroup -n MyCluster
4429+
"""

src/aks-preview/azext_aks_preview/_helpers.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,16 +351,19 @@ def check_is_apiserver_vnet_integration_cluster(mc: ManagedCluster) -> bool:
351351
return False
352352

353353

354-
def setup_common_safeguards_profile(level, version, excludedNamespaces, mc: ManagedCluster, models) -> ManagedCluster:
355-
if (level is not None or version is not None or excludedNamespaces is not None) and mc.safeguards_profile is None:
354+
def setup_common_safeguards_profile(level, version, excludedNamespaces, podSecurityStandardsLevel, mc: ManagedCluster, models) -> ManagedCluster:
355+
if (level is not None or version is not None or excludedNamespaces is not None or podSecurityStandardsLevel is not None) and mc.safeguards_profile is None:
356356
mc.safeguards_profile = models.SafeguardsProfile(
357357
level=level,
358-
version=version
358+
version=version,
359+
pod_security_standards_level=podSecurityStandardsLevel
359360
)
360361
# replace values with provided values
361362
if excludedNamespaces is not None:
362363
mc.safeguards_profile.excluded_namespaces = extract_comma_separated_string(
363364
excludedNamespaces, enable_strip=True, keep_none=True, default_value=[])
365+
if podSecurityStandardsLevel is not None:
366+
mc.safeguards_profile.pod_security_standards_level = podSecurityStandardsLevel
364367

365368
return mc
366369

src/aks-preview/azext_aks_preview/_params.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,13 @@
441441
CONST_SAFEGUARDSLEVEL_ENFORCEMENT,
442442
]
443443

444+
# consts for Pod Security Standards level
445+
pod_security_standards_levels = [
446+
CONST_POD_SECURITY_STANDARDS_LEVEL_PRIVILEGED,
447+
CONST_POD_SECURITY_STANDARDS_LEVEL_BASELINE,
448+
CONST_POD_SECURITY_STANDARDS_LEVEL_RESTRICTED,
449+
]
450+
444451
# azure service mesh
445452
ingress_gateway_types = [
446453
CONST_AZURE_SERVICE_MESH_INGRESS_MODE_EXTERNAL,
@@ -1030,6 +1037,11 @@ def load_arguments(self, _):
10301037
type=str,
10311038
is_preview=True
10321039
)
1040+
c.argument(
1041+
"pod_security_standards_level",
1042+
arg_type=get_enum_type(pod_security_standards_levels),
1043+
is_preview=True,
1044+
)
10331045
# azure monitor profile
10341046
c.argument(
10351047
"enable_azuremonitormetrics",
@@ -3117,6 +3129,33 @@ def load_arguments(self, _):
31173129
c.argument('config_file', options_list=['--config-file'], type=file_type, completer=FilesCompleter(),
31183130
help='Path to the JSON configuration file containing JWT authenticator properties.')
31193131

3132+
# AKS deployment safeguards parameters
3133+
with self.argument_context('aks safeguards') as c:
3134+
c.argument('resource_group_name', options_list=['--resource-group', '-g'])
3135+
c.argument('cluster_name', options_list=['--name', '-n'], help='The name of the Managed Cluster.')
3136+
c.argument('managed_cluster', options_list=['--cluster', '--managed-cluster', '-c'],
3137+
help='The fully qualified Azure Resource manager identifier of the Managed Cluster.')
3138+
3139+
with self.argument_context('aks safeguards create') as c:
3140+
c.argument('level', arg_type=get_enum_type(['Warn', 'Enforce']),
3141+
help='The deployment safeguards level.')
3142+
c.argument('excluded_namespaces', options_list=['--excluded-namespaces', '--excluded-ns'],
3143+
nargs='+',
3144+
help='User defined list of namespaces to exclude from Deployment Safeguards.')
3145+
c.argument('pod_security_standards_level', arg_type=get_enum_type(pod_security_standards_levels),
3146+
is_preview=True,
3147+
help='The Pod Security Standards level.')
3148+
3149+
with self.argument_context('aks safeguards update') as c:
3150+
c.argument('level', arg_type=get_enum_type(['Warn', 'Enforce']),
3151+
help='The deployment safeguards level.')
3152+
c.argument('excluded_namespaces', options_list=['--excluded-namespaces', '--excluded-ns'],
3153+
nargs='+',
3154+
help='User defined list of namespaces to exclude from Deployment Safeguards.')
3155+
c.argument('pod_security_standards_level', arg_type=get_enum_type(pod_security_standards_levels),
3156+
is_preview=True,
3157+
help='The Pod Security Standards level.')
3158+
31203159

31213160
def _get_default_install_location(exe_name):
31223161
system = platform.system()

src/aks-preview/azext_aks_preview/commands.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
cf_load_balancers,
1818
cf_identity_bindings,
1919
cf_jwt_authenticators,
20+
cf_deployment_safeguards,
2021
)
2122

2223
from azext_aks_preview._format import (
@@ -566,3 +567,38 @@ def load_command_table(self, _):
566567
"aks_jwtauthenticator_show",
567568
table_transformer=aks_jwtauthenticator_show_table_format
568569
)
570+
571+
# AKS deployment safeguards commands
572+
deployment_safeguards_sdk = CliCommandType(
573+
operations_tmpl="azext_aks_preview.vendored_sdks.azure_mgmt_preview_aks."
574+
"operations._deployment_safeguards_operations#DeploymentSafeguardsOperations.{}",
575+
operation_group="deployment_safeguards",
576+
client_factory=cf_deployment_safeguards,
577+
)
578+
with self.command_group(
579+
"aks safeguards", deployment_safeguards_sdk, client_factory=cf_deployment_safeguards, is_preview=True
580+
) as g:
581+
g.custom_command(
582+
"create",
583+
"aks_safeguards_create",
584+
supports_no_wait=True
585+
)
586+
g.custom_command(
587+
"update",
588+
"aks_safeguards_update",
589+
supports_no_wait=True
590+
)
591+
g.custom_show_command(
592+
"show",
593+
"aks_safeguards_show"
594+
)
595+
g.custom_command(
596+
"delete",
597+
"aks_safeguards_delete",
598+
supports_no_wait=True,
599+
confirmation=True
600+
)
601+
g.custom_command(
602+
"list",
603+
"aks_safeguards_list"
604+
)

0 commit comments

Comments
 (0)