-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
vdk-control-cli: autodetect if authentication is needed
It will prompt for login only if control service require authentication. This happens if control service returns 401. This makes it easier to use since this removes the necessity of explicit flag to enable/disable authentication. And make integration with other library easier. For example same authentication is used by properties api . So how it works 1. If users uses vdk login we'd cache their api token (and api token authorization url which may be provided by plugin) 2. if users users vdk login with interactive flow - we cache the necessary data as well 3. If users has not used vdk login but has set VDK_API_TOKEN and VDK_API_TOKEN_AUTHORIZATION_URL we'd use it in api-token folow to login (similar to step 1) With 1 and 2 having higher priority than environment variables (3) Error is not thrown until we try to connect to Control Service and it returns 401: If Control Service require authentication then error would be thrown like this ``` vdkcli list --all Usage: vdkcli list [OPTIONS] Error: ¯\_(ツ)_/¯ what: Control Service Error why: The request has not been applied because it lacks valid authentication credentials. consequences: Operation cannot complete. countermeasures: Try to login again using VDK CLI login command. Make sure you have permission to execute the given operation. ``` Otherwise request would succeed Testing Done: unit tests. Test using vdkcli list --all -u rest-api-with-auth and saw above error produced and then vdkcli list --all -u rest-api-without-auth and saw it succeeds. Signed-off-by: Antoni Ivanov <[email protected]>
- Loading branch information
1 parent
a79362f
commit 1369020
Showing
7 changed files
with
104 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
projects/vdk-control-cli/src/vdk/internal/control/auth/apikey_auth.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Copyright 2021 VMware, Inc. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
from typing import Optional | ||
|
||
from vdk.internal.control.auth.auth import Authentication | ||
from vdk.internal.control.auth.login_types import LoginTypes | ||
from vdk.internal.control.configuration.vdk_config import VDKConfig | ||
|
||
|
||
class ApiKeyAuthentication: | ||
""" | ||
Class that execute authentication process using API token. | ||
It will use the API token to get temporary access token using api token authorization URL. | ||
See Authentication class as well. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
api_token_authorization_url: Optional[str] = None, | ||
api_token: Optional[str] = None, | ||
): | ||
""" | ||
:param api_token_authorization_url: Authorization URL - Same as login --api-token-authorization-server-url. | ||
:param api_token: API Token - Same as login --api-token. | ||
""" | ||
self.__api_token = api_token | ||
self.__api_token_authorization_url = api_token_authorization_url | ||
self.__auth = Authentication() | ||
|
||
def authentication_process(self) -> None: | ||
""" | ||
Executes the authentication process and caches the generated access token so it can be used during REST calls. | ||
""" | ||
self.__auth.update_api_token_authorization_url( | ||
self.__api_token_authorization_url | ||
) | ||
self.__auth.update_api_token(self.__api_token) | ||
self.__auth.update_auth_type(LoginTypes.API_TOKEN.value) | ||
self.__auth.acquire_and_cache_access_token() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters