Skip to content

Commit

Permalink
containerapp refactor containerapp auth show/update (#6552)
Browse files Browse the repository at this point in the history
  • Loading branch information
Greedygre authored Jul 26, 2023
1 parent 6f084f4 commit 97ed55b
Show file tree
Hide file tree
Showing 5 changed files with 3,534 additions and 61 deletions.
112 changes: 112 additions & 0 deletions src/containerapp/azext_containerapp/containerapp_auth_decorator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
from typing import Any, Dict

from azure.cli.core.commands import AzCliCommand

from ._client_factory import handle_raw_exception
from .base_resource import BaseResource


class ContainerAppAuthDecorator(BaseResource):
def __init__(self, cmd: AzCliCommand, client: Any, raw_parameters: Dict, models: str):
super().__init__(cmd, client, raw_parameters, models)
self.existing_auth = {}

def show(self):
auth_settings = {}
try:
auth_settings = 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"]
except:
pass
return auth_settings

def construct_payload(self):
from ._utils import set_field_in_auth_settings, update_http_settings_in_auth_settings
self.existing_auth = {}
try:
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"]
except:
self.existing_auth["platform"] = {}
self.existing_auth["platform"]["enabled"] = True
self.existing_auth["globalValidation"] = {}
self.existing_auth["login"] = {}

self.existing_auth = set_field_in_auth_settings(self.existing_auth, self.get_argument_set_string())

if self.get_argument_enabled() is not None:
if "platform" not in self.existing_auth:
self.existing_auth["platform"] = {}
self.existing_auth["platform"]["enabled"] = self.get_argument_enabled()

if self.get_argument_runtime_version() is not None:
if "platform" not in self.existing_auth:
self.existing_auth["platform"] = {}
self.existing_auth["platform"]["runtimeVersion"] = self.get_argument_runtime_version()

if self.get_argument_config_file_path() is not None:
if "platform" not in self.existing_auth:
self.existing_auth["platform"] = {}
self.existing_auth["platform"]["configFilePath"] = self.get_argument_config_file_path()

if self.get_argument_unauthenticated_client_action() is not None:
if "globalValidation" not in self.existing_auth:
self.existing_auth["globalValidation"] = {}
self.existing_auth["globalValidation"]["unauthenticatedClientAction"] = self.get_argument_unauthenticated_client_action()

if self.get_argument_redirect_provider() is not None:
if "globalValidation" not in self.existing_auth:
self.existing_auth["globalValidation"] = {}
self.existing_auth["globalValidation"]["redirectToProvider"] = self.get_argument_redirect_provider()

if self.get_argument_excluded_paths() is not None:
if "globalValidation" not in self.existing_auth:
self.existing_auth["globalValidation"] = {}
self.existing_auth["globalValidation"]["excludedPaths"] = self.get_argument_excluded_paths().split(",")

self.existing_auth = update_http_settings_in_auth_settings(self.existing_auth, self.get_argument_require_https(),
self.get_argument_proxy_convention(), self.get_argument_proxy_custom_host_header(),
self.get_argument_proxy_custom_proto_header())

def create_or_update(self):
try:
return self.client.create_or_update(cmd=self.cmd, resource_group_name=self.get_argument_resource_group_name(),
container_app_name=self.get_argument_name(), auth_config_name="current",
auth_config_envelope=self.existing_auth)
except Exception as e:
handle_raw_exception(e)

def get_argument_set_string(self):
return self.get_param("set_string")

def get_argument_enabled(self):
return self.get_param("enabled")

def get_argument_runtime_version(self):
return self.get_param("runtime_version")

def get_argument_config_file_path(self):
return self.get_param("config_file_path")

def get_argument_unauthenticated_client_action(self):
return self.get_param("unauthenticated_client_action")

def get_argument_redirect_provider(self):
return self.get_param("redirect_provider")

def get_argument_require_https(self):
return self.get_param("require_https")

def get_argument_proxy_convention(self):
return self.get_param("proxy_convention")

def get_argument_proxy_custom_host_header(self):
return self.get_param("proxy_custom_host_header")

def get_argument_proxy_custom_proto_header(self):
return self.get_param("proxy_custom_proto_header")

def get_argument_excluded_paths(self):
return self.get_param("excluded_paths")
73 changes: 19 additions & 54 deletions src/containerapp/azext_containerapp/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from msrestazure.tools import parse_resource_id, is_valid_resource_id
from msrest.exceptions import DeserializationError

from .containerapp_auth_decorator import ContainerAppAuthDecorator
from .containerapp_decorator import ContainerAppCreateDecorator, BaseContainerAppDecorator
from ._client_factory import handle_raw_exception, handle_non_404_exception
from ._clients import ManagedEnvironmentClient, ContainerAppClient, GitHubActionClient, DaprComponentClient, StorageClient, AuthClient, WorkloadProfileClient, ContainerAppsJobClient
Expand Down Expand Up @@ -5297,64 +5298,28 @@ def update_auth_config(cmd, resource_group_name, name, set_string=None, enabled=
redirect_provider=None, require_https=None,
proxy_convention=None, proxy_custom_host_header=None,
proxy_custom_proto_header=None, excluded_paths=None):
from ._utils import set_field_in_auth_settings, update_http_settings_in_auth_settings
existing_auth = {}
try:
existing_auth = AuthClient.get(cmd=cmd, resource_group_name=resource_group_name, container_app_name=name, auth_config_name="current")["properties"]
except:
existing_auth["platform"] = {}
existing_auth["platform"]["enabled"] = True
existing_auth["globalValidation"] = {}
existing_auth["login"] = {}

existing_auth = set_field_in_auth_settings(existing_auth, set_string)

if enabled is not None:
if "platform" not in existing_auth:
existing_auth["platform"] = {}
existing_auth["platform"]["enabled"] = enabled

if runtime_version is not None:
if "platform" not in existing_auth:
existing_auth["platform"] = {}
existing_auth["platform"]["runtimeVersion"] = runtime_version

if config_file_path is not None:
if "platform" not in existing_auth:
existing_auth["platform"] = {}
existing_auth["platform"]["configFilePath"] = config_file_path

if unauthenticated_client_action is not None:
if "globalValidation" not in existing_auth:
existing_auth["globalValidation"] = {}
existing_auth["globalValidation"]["unauthenticatedClientAction"] = unauthenticated_client_action

if redirect_provider is not None:
if "globalValidation" not in existing_auth:
existing_auth["globalValidation"] = {}
existing_auth["globalValidation"]["redirectToProvider"] = redirect_provider

if excluded_paths is not None:
if "globalValidation" not in existing_auth:
existing_auth["globalValidation"] = {}
existing_auth["globalValidation"]["excludedPaths"] = excluded_paths.split(",")
raw_parameters = locals()
containerapp_auth_decorator = ContainerAppAuthDecorator(
cmd=cmd,
client=AuthClient,
raw_parameters=raw_parameters,
models="azext_containerapp._sdk_models"
)

existing_auth = update_http_settings_in_auth_settings(existing_auth, require_https,
proxy_convention, proxy_custom_host_header,
proxy_custom_proto_header)
try:
return AuthClient.create_or_update(cmd=cmd, resource_group_name=resource_group_name, container_app_name=name, auth_config_name="current", auth_config_envelope=existing_auth)
except Exception as e:
handle_raw_exception(e)
containerapp_auth_decorator.construct_payload()
return containerapp_auth_decorator.create_or_update()


def show_auth_config(cmd, resource_group_name, name):
auth_settings = {}
try:
auth_settings = AuthClient.get(cmd=cmd, resource_group_name=resource_group_name, container_app_name=name, auth_config_name="current")["properties"]
except:
pass
return auth_settings
raw_parameters = locals()
containerapp_auth_decorator = ContainerAppAuthDecorator(
cmd=cmd,
client=AuthClient,
raw_parameters=raw_parameters,
models="azext_containerapp._sdk_models"
)

return containerapp_auth_decorator.show()


# Compose
Expand Down
Loading

0 comments on commit 97ed55b

Please sign in to comment.