|
3 | 3 | # Licensed under the MIT License. See License.txt in the project root for license information. |
4 | 4 | # -------------------------------------------------------------------------------------------- |
5 | 5 | # pylint: disable=line-too-long, useless-parent-delegation |
6 | | - |
| 6 | +from azure.cli.core.azclierror import ArgumentUsageError |
| 7 | +from azure.cli.core.commands.client_factory import get_subscription_id |
7 | 8 | from azure.cli.command_modules.containerapp.containerapp_auth_decorator import ContainerAppAuthDecorator |
| 9 | +from azure.cli.command_modules.containerapp._utils import safe_set, set_field_in_auth_settings, update_http_settings_in_auth_settings, _ensure_identity_resource_id |
| 10 | +from azure.cli.command_modules.containerapp._constants import BLOB_STORAGE_TOKEN_STORE_SECRET_SETTING_NAME |
8 | 11 |
|
9 | 12 |
|
10 | 13 | # decorator for preview auth show/update |
11 | 14 | class ContainerAppPreviewAuthDecorator(ContainerAppAuthDecorator): |
| 15 | + |
| 16 | + def parent_construct_payload(self): |
| 17 | + self.existing_auth = {} |
| 18 | + try: |
| 19 | + self.existing_auth = self.client.get(cmd=self.cmd, resource_group_name=self.get_argument_resource_group_name(), container_app_name=self.get_argument_name(), auth_config_name="current")["properties"] |
| 20 | + except: # pylint: disable=bare-except |
| 21 | + self.existing_auth["platform"] = {} |
| 22 | + self.existing_auth["platform"]["enabled"] = True |
| 23 | + self.existing_auth["globalValidation"] = {} |
| 24 | + self.existing_auth["login"] = {} |
| 25 | + |
| 26 | + self.existing_auth = set_field_in_auth_settings(self.existing_auth, self.get_argument_set_string()) |
| 27 | + |
| 28 | + if self.get_argument_enabled() is not None: |
| 29 | + if "platform" not in self.existing_auth: |
| 30 | + self.existing_auth["platform"] = {} |
| 31 | + self.existing_auth["platform"]["enabled"] = self.get_argument_enabled() |
| 32 | + |
| 33 | + if self.get_argument_runtime_version() is not None: |
| 34 | + if "platform" not in self.existing_auth: |
| 35 | + self.existing_auth["platform"] = {} |
| 36 | + self.existing_auth["platform"]["runtimeVersion"] = self.get_argument_runtime_version() |
| 37 | + |
| 38 | + if self.get_argument_config_file_path() is not None: |
| 39 | + if "platform" not in self.existing_auth: |
| 40 | + self.existing_auth["platform"] = {} |
| 41 | + self.existing_auth["platform"]["configFilePath"] = self.get_argument_config_file_path() |
| 42 | + |
| 43 | + if self.get_argument_unauthenticated_client_action() is not None: |
| 44 | + if "globalValidation" not in self.existing_auth: |
| 45 | + self.existing_auth["globalValidation"] = {} |
| 46 | + self.existing_auth["globalValidation"]["unauthenticatedClientAction"] = self.get_argument_unauthenticated_client_action() |
| 47 | + |
| 48 | + if self.get_argument_redirect_provider() is not None: |
| 49 | + if "globalValidation" not in self.existing_auth: |
| 50 | + self.existing_auth["globalValidation"] = {} |
| 51 | + self.existing_auth["globalValidation"]["redirectToProvider"] = self.get_argument_redirect_provider() |
| 52 | + |
| 53 | + if self.get_argument_excluded_paths() is not None: |
| 54 | + if "globalValidation" not in self.existing_auth: |
| 55 | + self.existing_auth["globalValidation"] = {} |
| 56 | + self.existing_auth["globalValidation"]["excludedPaths"] = self.get_argument_excluded_paths().split(",") |
| 57 | + |
| 58 | + self.existing_auth = update_http_settings_in_auth_settings(self.existing_auth, self.get_argument_require_https(), |
| 59 | + self.get_argument_proxy_convention(), self.get_argument_proxy_custom_host_header(), |
| 60 | + self.get_argument_proxy_custom_proto_header()) |
| 61 | + |
12 | 62 | def construct_payload(self): |
13 | | - super().construct_payload() |
| 63 | + self.parent_construct_payload() |
| 64 | + self.set_up_token_store() |
| 65 | + |
| 66 | + def set_up_token_store(self): |
| 67 | + if self.get_argument_token_store() is None: |
| 68 | + return |
| 69 | + |
| 70 | + if self.get_argument_token_store() is False: |
| 71 | + safe_set(self.existing_auth, "login", "tokenStore", "enabled", value=False) |
| 72 | + return |
| 73 | + |
| 74 | + safe_set(self.existing_auth, "login", "tokenStore", "enabled", value=True) |
| 75 | + safe_set(self.existing_auth, "login", "tokenStore", "azureBlobStorage", value={}) |
| 76 | + |
| 77 | + param_provided = sum(1 for param in [self.get_argument_sas_url_secret(), self.get_argument_sas_url_secret_name(), self.get_argument_blob_container_uri()] if param is not None) |
| 78 | + if param_provided != 1: |
| 79 | + raise ArgumentUsageError( |
| 80 | + 'Usage Error: only blob storage token store is supported. --sas-url-secret, --sas-url-secret-name and --blob-container-uri should provide exactly one when token store is enabled') |
| 81 | + |
| 82 | + if self.get_argument_blob_container_uri() is not None: |
| 83 | + safe_set(self.existing_auth, "login", "tokenStore", "azureBlobStorage", "blobContainerUri", value=self.get_argument_blob_container_uri()) |
| 84 | + |
| 85 | + identity = self.get_argument_blob_container_identity() |
| 86 | + if identity is not None: |
| 87 | + identity = identity.lower() |
| 88 | + subscription_id = get_subscription_id(self.cmd.cli_ctx) |
| 89 | + identity = _ensure_identity_resource_id(subscription_id, self.get_argument_resource_group_name(), identity) |
| 90 | + safe_set(self.existing_auth, "login", "tokenStore", "azureBlobStorage", "managedIdentityResourceId", value=identity) |
| 91 | + return |
| 92 | + |
| 93 | + sas_url_setting_name = BLOB_STORAGE_TOKEN_STORE_SECRET_SETTING_NAME |
| 94 | + if self.get_argument_sas_url_secret_name() is not None: |
| 95 | + sas_url_setting_name = self.get_argument_sas_url_secret_name() |
| 96 | + safe_set(self.existing_auth, "login", "tokenStore", "azureBlobStorage", "sasUrlSettingName", value=sas_url_setting_name) |
| 97 | + |
| 98 | + def get_argument_blob_container_uri(self): |
| 99 | + return self.get_param("blob_container_uri") |
| 100 | + |
| 101 | + def get_argument_blob_container_identity(self): |
| 102 | + return self.get_param("blob_container_identity") |
0 commit comments