Skip to content

Commit

Permalink
vdk-control-cli: Adopt new auth exceptions (#846)
Browse files Browse the repository at this point in the history
After the introduction of new exception types in the
`vdk-control-api-auth` plugin, more granular exception handling
is now possible.

This change adopts the new exception types in `vdk-control-cli`,
and removes unnecessary error message parsing.

Additionally, the exit status code when authentication failes was
changed from 0 to 1 to better reflect the fact that something unexpected
has happened.

Testing Done: Unit tests

Signed-off-by: Andon Andonov <[email protected]>
  • Loading branch information
doks5 authored May 30, 2022
1 parent 359e827 commit 415f9a1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# Copyright 2021 VMware, Inc.
# SPDX-License-Identifier: Apache-2.0
import sys

import click
from vdk.internal.control.auth.login_types import LoginTypes
from vdk.internal.control.exception.vdk_exception import VDKException
from vdk.internal.control.utils import cli_utils
from vdk.internal.control.utils.cli_utils import extended_option
from vdk.plugin.control_api_auth.auth_exception import VDKAuthException
from vdk.plugin.control_api_auth.auth_exception import VDKInvalidAuthParamError
from vdk.plugin.control_api_auth.authentication import Authentication


Expand Down Expand Up @@ -121,17 +124,16 @@ def login(
)
auth.authenticate()
click.echo("Login Successful")
except VDKInvalidAuthParamError:
click.echo(
f"Login type: {auth_type} requires arguments:, --client-secret, --client-id, "
"--oauth2-exchange-url, --oauth2-discovery-url"
)
sys.exit(1)
except VDKAuthException as e:
if (
"requires client_id and auth_discovery_url to be specified" in e.message
or "auth_url was not specified." in e.message
):
click.echo(
f"Login type: {auth_type} requires arguments:, --client-secret, --client-id, "
"--oauth2-exchange-url, --oauth2-discovery-url"
)
else:
click.echo(f"Login failed. Error was: {e.message}")
click.echo(f"Login failed. Error was: {e.message}")
sys.exit(1)

elif auth_type == LoginTypes.API_TOKEN.value:
api_token = cli_utils.get_or_prompt("Oauth2 API token", api_token)
if not api_token:
Expand All @@ -153,6 +155,7 @@ def login(
apikey_auth.authenticate()
except VDKAuthException as e:
click.echo(f"Login failed. Error was: {e.message}")
sys.exit(1)
click.echo("Login Successful")
else:
click.echo(f"Login type: {auth_type} not supported")
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ def test_login_credentials_exceptions(httpserver: PluginHTTPServer):
"client_secret",
],
)
test_utils.assert_click_status(result, 1)
assert (
"requires arguments:, --client-secret, --client-id, --oauth2-exchange-url, --oauth2-discovery-url"
in result.output
Expand All @@ -185,6 +186,7 @@ def test_login_credentials_exceptions(httpserver: PluginHTTPServer):
"client_secret",
],
)
test_utils.assert_click_status(result, 1)
assert (
"requires arguments:, --client-secret, --client-id, --oauth2-exchange-url, --oauth2-discovery-url"
in result.output
Expand All @@ -204,6 +206,7 @@ def test_login_credentials_exceptions(httpserver: PluginHTTPServer):
"client_secret",
],
)
test_utils.assert_click_status(result, 1)
assert (
"requires arguments:, --client-secret, --client-id, --oauth2-exchange-url, --oauth2-discovery-url"
in result.output
Expand Down

0 comments on commit 415f9a1

Please sign in to comment.