Skip to content

Commit 96b8283

Browse files
authored
Merge pull request #67 from 2ndWatch/update-cli-auth
Add cli auth and adjust user api token handling
2 parents 763cdca + 4ca0e64 commit 96b8283

File tree

4 files changed

+47
-11
lines changed

4 files changed

+47
-11
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ cloudendure api login
5252
or
5353

5454
```sh
55-
export CLOUDENDURE_TOKEN=<your_ce_api_token>
55+
export CLOUDENDURE_USER_API_TOKEN=<your_ce_user_api_token>
5656
export CLOUDENDURE_DESTINATION_ACCOUNT=<destination_aws_account_id>
5757

5858
ce api login
@@ -69,7 +69,7 @@ cloudendure api login --user=<your_ce_user> --password=<your_ce_password>
6969
or
7070

7171
```sh
72-
ce api login --token=<your_ce_token>
72+
ce api login --token=<your_ce_user_api_token>
7373
```
7474

7575
Logging in for the first time will generate the `~/.cloudendure.yml` file.

cloudendure/api.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class CloudEndureAPI:
4747

4848
TOP_LEVEL: List[str] = ["projects", "blueprints"]
4949

50-
def __init__(self, *args, **kwargs) -> None:
50+
def __init__(self, config: CloudEndureConfig, *args, **kwargs) -> None:
5151
"""Initialize the CloudEndure API client.
5252
5353
Attributes:
@@ -57,7 +57,7 @@ def __init__(self, *args, **kwargs) -> None:
5757
time_now: datetime.datetime = datetime.datetime.utcnow()
5858

5959
self.api_endpoint: str = f"{HOST}/api/{API_VERSION}"
60-
self.config: CloudEndureConfig = CloudEndureConfig()
60+
self.config: CloudEndureConfig = config
6161

6262
self.projects: List[str] = []
6363
self.session: Session = requests.Session()
@@ -75,27 +75,44 @@ def __init__(self, *args, **kwargs) -> None:
7575
if _xsrf_token:
7676
self.session.headers.update({"X-XSRF-TOKEN": _xsrf_token})
7777

78-
def login(self, username: str = "", password: str = "") -> bool:
78+
def login(self, username: str = "", password: str = "", token: str = "") -> bool:
7979
"""Login to the CloudEndure API console.
8080
8181
Args:
8282
username (str): The CloudEndure username to be used.
8383
Defaults to the environment specific default.
8484
password (str): The CloudEndure password to be used.
8585
Defaults to the environment specific default.
86+
token (str): The CloudEndure token to be used. This argument takes precedence.
87+
If provided, username and password will not be used.
88+
Defaults to the environment specific default.
8689
8790
Attributes:
8891
endpoint (str): The CloudEndure API endpoint to be used.
8992
_username (str): The CloudEndure API username.
9093
_password (str): The CloudEndure API password.
94+
_token (str): The CloudEndure API token.
9195
_auth (dict): The CloudEndure API username/password dictionary map.
9296
response (requests.Response): The CloudEndure API login request response object.
9397
_xsrf_token (str): The XSRF token to be used for subsequent API requests.
9498
99+
TODO:
100+
* Verify default XSRF-Token TTL and check validity before performing
101+
subsequent authentication requests.
102+
95103
"""
96104
_username: str = self.config.active_config["username"] or username
97105
_password: str = self.config.active_config["password"] or password
98-
_auth: Dict[str, str] = {"username": _username, "password": _password}
106+
_token: str = self.config.active_config["user_api_token"] or token
107+
_auth: Dict[str, str] = {}
108+
109+
if _token:
110+
_auth["userApiToken"] = _token
111+
elif _username and _password:
112+
_auth = {"username": _username, "password": _password}
113+
else:
114+
print("You must configure your authentication credentials!")
115+
return False
99116

100117
# Attempt to login to the CloudEndure API via a POST request.
101118
response: requests.Response = self.api_call(

cloudendure/cloudendure.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,23 @@
2929
class CloudEndure:
3030
"""Define the CloudEndure general object."""
3131

32-
def __init__(self, project_name: str = "", dry_run: bool = False) -> None:
32+
def __init__(
33+
self,
34+
project_name: str = "",
35+
dry_run: bool = False,
36+
username: str = "",
37+
password: str = "",
38+
token: str = "",
39+
) -> None:
3340
"""Initialize the CloudEndure object.
3441
3542
This entrypoint is primarily for use with the CLI.
3643
3744
"""
38-
self.config: CloudEndureConfig = CloudEndureConfig()
39-
self.api: CloudEndureAPI = CloudEndureAPI()
45+
self.config: CloudEndureConfig = CloudEndureConfig(
46+
username=username, password=password, token=token
47+
)
48+
self.api: CloudEndureAPI = CloudEndureAPI(self.config)
4049
self.is_authenticated = self.api.login()
4150

4251
# Determine the active project ID.

cloudendure/config.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
class CloudEndureConfig:
1818
"""Define the CloudEndure Config object."""
1919

20-
def __init__(self, *args, **kwargs) -> None:
20+
def __init__(
21+
self, username: str = "", password: str = "", token: str = "", *args, **kwargs
22+
) -> None:
2123
"""Initialize the Environment."""
2224
logger.info("Initializing the CloudEndure Configuration")
2325
_config_path: str = os.environ.get(
@@ -26,6 +28,9 @@ def __init__(self, *args, **kwargs) -> None:
2628
if _config_path.startswith("~"):
2729
self.config_path = os.path.expanduser(_config_path)
2830

31+
# Handle CloudEndure CLI input credentials.
32+
self.cli = {"username": username, "password": password, "user_api_token": token}
33+
2934
_config: PosixPath = Path(self.config_path)
3035
if not _config.exists():
3136
print(
@@ -40,6 +45,7 @@ def __init__(self, *args, **kwargs) -> None:
4045
"username": "",
4146
"password": "",
4247
"token": "",
48+
"user_api_token": "",
4349
"session_cookie": "",
4450
"project_name": "",
4551
"project_id": "",
@@ -112,7 +118,11 @@ def update_config(self) -> None:
112118
"""Update the configuration."""
113119
self.yaml_config_contents: Dict[str, Any] = self.read_yaml_config()
114120
self.env_config = self.get_env_vars()
115-
self.active_config = {**self.yaml_config_contents, **self.env_config}
121+
self.active_config = {
122+
**self.yaml_config_contents,
123+
**self.env_config,
124+
**self.cli,
125+
}
116126

117127
def update_token(self, token: str) -> bool:
118128
"""Update the CloudEndure token.

0 commit comments

Comments
 (0)