Skip to content

Commit

Permalink
Infer acr credentials if it is acr and credentials are not provided
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinsID committed Mar 10, 2022
1 parent 4fa1f12 commit 0e69649
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
17 changes: 16 additions & 1 deletion src/containerapp/azext_containerapp/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

from distutils.filelist import findall
from operator import is_
from azure.cli.command_modules.appservice.custom import (_get_acr_cred)
from azure.cli.core.azclierror import (ResourceNotFoundError, ValidationError, RequiredArgumentMissingError)

from azure.cli.core.commands.client_factory import get_subscription_id
from knack.log import get_logger
from msrestazure.tools import parse_resource_id
Expand Down Expand Up @@ -444,3 +444,18 @@ def _get_app_from_revision(revision):
revision.pop()
revision = "--".join(revision)
return revision


def _infer_acr_credentials(cmd, registry_server):
# If registry is Azure Container Registry, we can try inferring credentials
if '.azurecr.io' not in registry_server:
raise RequiredArgumentMissingError('Registry url is required if using Azure Container Registry, otherwise Registry username and password are required.')
logger.warning('No credential was provided to access Azure Container Registry. Trying to look up credentials...')
parsed = urlparse(registry_server)
registry_name = (parsed.netloc if parsed.scheme else parsed.path).split('.')[0]

try:
registry_user, registry_pass = _get_acr_cred(cmd.cli_ctx, registry_name)
return (registry_user, registry_pass)
except Exception as ex:
raise RequiredArgumentMissingError('Failed to retrieve credentials for container registry {}. Please provide the registry username and password'.format(registry_name))
11 changes: 6 additions & 5 deletions src/containerapp/azext_containerapp/_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,20 @@ def validate_registry_server(namespace):
if "create" in namespace.command.lower():
if namespace.registry_server:
if not namespace.registry_user or not namespace.registry_pass:
raise ValidationError("Usage error: --registry-login-server, --registry-password and --registry-username are required together")
if ".azurecr.io" not in namespace.registry_server:
raise ValidationError("Usage error: --registry-login-server, --registry-password and --registry-username are required together if not using Azure Container Registry")

def validate_registry_user(namespace):
if "create" in namespace.command.lower():
if namespace.registry_user:
if not namespace.registry_server or not namespace.registry_pass:
raise ValidationError("Usage error: --registry-login-server, --registry-password and --registry-username are required together")
if not namespace.registry_server or (not namespace.registry_pass and ".azurecr.io" not in namespace.registry_server):
raise ValidationError("Usage error: --registry-login-server, --registry-password and --registry-username are required together if not using Azure Container Registry")

def validate_registry_pass(namespace):
if "create" in namespace.command.lower():
if namespace.registry_pass:
if not namespace.registry_user or not namespace.registry_server:
raise ValidationError("Usage error: --registry-login-server, --registry-password and --registry-username are required together")
if not namespace.registry_server or (not namespace.registry_user and ".azurecr.io" not in namespace.registry_server):
raise ValidationError("Usage error: --registry-login-server, --registry-password and --registry-username are required together if not using Azure Container Registry")

def validate_target_port(namespace):
if "create" in namespace.command.lower():
Expand Down
13 changes: 11 additions & 2 deletions src/containerapp/azext_containerapp/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from azure.cli.command_modules.appservice.custom import (_get_acr_cred)
from azure.cli.core.azclierror import (RequiredArgumentMissingError, ResourceNotFoundError, ValidationError)
from azure.cli.core.commands.client_factory import get_subscription_id
from azure.cli.command_modules.appservice.custom import (_get_acr_cred)
from azure.cli.core.util import sdk_no_wait
from knack.util import CLIError
from knack.log import get_logger
Expand Down Expand Up @@ -37,7 +37,7 @@
_generate_log_analytics_if_not_provided, _get_existing_secrets, _convert_object_from_snake_to_camel_case,
_object_to_dict, _add_or_update_secrets, _remove_additional_attributes, _remove_readonly_attributes,
_add_or_update_env_vars, _add_or_update_tags, update_nested_dictionary, _update_traffic_Weights,
_get_app_from_revision, raise_missing_token_suggestion)
_get_app_from_revision, raise_missing_token_suggestion, _infer_acr_credentials)

logger = get_logger(__name__)

Expand Down Expand Up @@ -370,6 +370,11 @@ def create_containerapp(cmd,
registries_def = None
if registry_server is not None:
registries_def = RegistryCredentialsModel

# Infer credentials if not supplied and its azurecr
if not registry_user or not registry_pass:
registry_user, registry_pass = _infer_acr_credentials(cmd, registry_server)

registries_def["server"] = registry_server
registries_def["username"] = registry_user

Expand Down Expand Up @@ -648,6 +653,10 @@ def update_containerapp(cmd,
if not registry_server:
raise ValidationError("Usage error: --registry-login-server is required when adding or updating a registry")

# Infer credentials if not supplied and its azurecr
if not registry_user or not registry_pass:
registry_user, registry_pass = _infer_acr_credentials(cmd, registry_server)

# Check if updating existing registry
updating_existing_registry = False
for r in registries_def:
Expand Down

0 comments on commit 0e69649

Please sign in to comment.