Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

show api error message for invalid cert name #115

Merged
merged 7 commits into from
Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/containerapp/HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Release History

0.3.6
++++++

* Fixed bug with 'az containerapp env certificate upload' where it shows a misleading message for invalid certificate name

0.3.5
++++++
Expand Down
3 changes: 3 additions & 0 deletions src/containerapp/azext_containerapp/_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@
UNAUTHENTICATED_CLIENT_ACTION = ['RedirectToLoginPage', 'AllowAnonymous', 'RejectWith401', 'RejectWith404']
FORWARD_PROXY_CONVENTION = ['NoProxy', 'Standard', 'Custom']
CHECK_CERTIFICATE_NAME_AVAILABILITY_TYPE = "Microsoft.App/managedEnvironments/certificates"

NAME_INVALID = "Invalid"
NAME_ALREADY_EXISTS = "AlreadyExists"
4 changes: 2 additions & 2 deletions src/containerapp/azext_containerapp/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from datetime import datetime
from dateutil.relativedelta import relativedelta
from azure.cli.core.azclierror import (ValidationError, RequiredArgumentMissingError, CLIInternalError,
ResourceNotFoundError, ArgumentUsageError, FileOperationError, CLIError)
ResourceNotFoundError, FileOperationError, CLIError)
from azure.cli.core.commands.client_factory import get_subscription_id
from azure.cli.command_modules.appservice.utils import _normalize_location
from azure.cli.command_modules.network._client_factory import network_client_factory
Expand Down Expand Up @@ -1262,7 +1262,7 @@ def check_cert_name_availability(cmd, resource_group_name, name, cert_name):
r = ManagedEnvironmentClient.check_name_availability(cmd, resource_group_name, name, name_availability_request)
except CLIError as e:
handle_raw_exception(e)
return r["nameAvailable"]
return r


def validate_hostname(cmd, resource_group_name, name, hostname):
Expand Down
24 changes: 16 additions & 8 deletions src/containerapp/azext_containerapp/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@
_update_revision_env_secretrefs, _get_acr_cred, safe_get, await_github_action, repo_url_to_name,
validate_container_app_name, _update_weights, get_vnet_location, register_provider_if_needed,
generate_randomized_cert_name, _get_name, load_cert_file, check_cert_name_availability,
validate_hostname, patch_new_custom_domain, get_custom_domains, get_resource_group)
validate_hostname, patch_new_custom_domain, get_custom_domains)

from ._ssh_utils import (SSH_DEFAULT_ENCODING, WebSocketConnection, read_ssh, get_stdin_writer, SSH_CTRL_C_MSG,
SSH_BACKUP_ENCODING)
from ._constants import (MAXIMUM_SECRET_LENGTH, MICROSOFT_SECRET_SETTING_NAME, FACEBOOK_SECRET_SETTING_NAME, GITHUB_SECRET_SETTING_NAME,
GOOGLE_SECRET_SETTING_NAME, TWITTER_SECRET_SETTING_NAME, APPLE_SECRET_SETTING_NAME, CONTAINER_APPS_RP)
GOOGLE_SECRET_SETTING_NAME, TWITTER_SECRET_SETTING_NAME, APPLE_SECRET_SETTING_NAME, CONTAINER_APPS_RP,
NAME_INVALID, NAME_ALREADY_EXISTS)

logger = get_logger(__name__)

Expand Down Expand Up @@ -2385,18 +2386,25 @@ def upload_certificate(cmd, name, resource_group_name, certificate_file, certifi

cert_name = None
if certificate_name:
if not check_cert_name_availability(cmd, resource_group_name, name, certificate_name):
msg = 'A certificate with the name {} already exists in {}. If continue with this name, it will be overwritten by the new certificate file.\nOverwrite?'
overwrite = prompt_y_n(msg.format(certificate_name, name))
if overwrite:
cert_name = certificate_name
name_availability = check_cert_name_availability(cmd, resource_group_name, name, certificate_name)
if not name_availability["nameAvailable"]:
if name_availability["reason"] == NAME_ALREADY_EXISTS:
msg = '{}. If continue with this name, it will be overwritten by the new certificate file.\nOverwrite?'
overwrite = prompt_y_n(msg.format(name_availability["message"]))
if overwrite:
cert_name = certificate_name
else:
raise ValidationError(name_availability["message"] or "Certificate name '{}' is invalid. Please try another name.".format(certificate_name))
else:
cert_name = certificate_name

while not cert_name:
random_name = generate_randomized_cert_name(thumbprint, name, resource_group_name)
if check_cert_name_availability(cmd, resource_group_name, name, random_name):
check_result = check_cert_name_availability(cmd, resource_group_name, name, random_name)
if check_result["nameAvailable"]:
cert_name = random_name
elif not check_result["nameAvailable"] and (check_result["reason"] == NAME_INVALID):
raise ValidationError(check_result["message"])

certificate = ContainerAppCertificateEnvelopeModel
certificate["properties"]["password"] = certificate_password
Expand Down