Skip to content

Commit bd41f78

Browse files
committed
WIP
1 parent eb5bc0c commit bd41f78

20 files changed

Lines changed: 198 additions & 156 deletions

‎stacklet/client/platform/cli.py‎

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,27 @@
1010
import jwt
1111
import requests
1212

13-
from stacklet.client.platform.cognito import CognitoUserManager
14-
from stacklet.client.platform.commands import commands
15-
from stacklet.client.platform.config import StackletConfig
16-
from stacklet.client.platform.context import StackletContext
17-
from stacklet.client.platform.formatter import Formatter
18-
from stacklet.client.platform.utils import click_group_entry, default_options
13+
from .cognito import CognitoUserManager
14+
from .commands import commands
15+
from .config import StackletConfig, StackletCredentials
16+
from .context import StackletContext
17+
from .formatter import Formatter
18+
from .utils import default_options, setup_logging
1919

2020

2121
@click.group()
2222
@default_options()
2323
@click.pass_context
24-
def cli(*args, **kwargs):
24+
def cli(
25+
ctx,
26+
config,
27+
output,
28+
cognito_user_pool_id,
29+
cognito_client_id,
30+
cognito_region,
31+
api,
32+
v,
33+
):
2534
"""
2635
Stacklet CLI
2736
@@ -77,7 +86,25 @@ def cli(*args, **kwargs):
7786
--after $after_token \\
7887
list
7988
"""
80-
click_group_entry(*args, **kwargs)
89+
setup_logging(v)
90+
91+
config_items = [cognito_user_pool_id, cognito_client_id, cognito_region, api]
92+
if any(config_items) and not all(config_items):
93+
raise Exception(
94+
"All options must be set for config items: --cognito-user-pool-id, "
95+
+ "--cognito-client-id, --cognito-region, and --api"
96+
)
97+
98+
raw_config = None
99+
if all(config_items):
100+
raw_config = {
101+
"cognito_user_pool_id": cognito_user_pool_id,
102+
"cognito_client_id": cognito_client_id,
103+
"region": cognito_region,
104+
"api": api,
105+
}
106+
107+
ctx.obj = StackletContext(raw_config=raw_config, config_file=config)
81108

82109

83110
@cli.command(short_help="Configure stacklet-admin cli")
@@ -268,14 +295,12 @@ def show(ctx):
268295
"""
269296
Show your config
270297
"""
271-
context = StackletContext(ctx.obj["config"], ctx.obj["raw_config"])
272298
fmt = Formatter.registry.get(ctx.obj["output"])()
273-
if os.path.exists(os.path.expanduser(StackletContext.DEFAULT_ID)):
274-
with open(os.path.expanduser(StackletContext.DEFAULT_ID), "r") as f:
275-
id_details = jwt.decode(f.read(), options={"verify_signature": False})
299+
if id_token := StackletCredentials().id_token():
300+
id_details = jwt.decode(id_token, options={"verify_signature": False})
276301
click.echo(fmt(id_details))
277302
click.echo()
278-
click.echo(fmt(context.config.to_json()))
303+
click.echo(fmt(ctx.obj.config.to_json()))
279304

280305

281306
@cli.command(short_help="Login to Stacklet")
@@ -295,18 +320,19 @@ def login(ctx, username, password):
295320
296321
If password is not passed in, your password will be prompted
297322
"""
298-
context = StackletContext(ctx.obj["config"], ctx.obj["raw_config"])
323+
config = ctx.obj.config
324+
can_sso_login = ctx.obj.can_sso_login()
299325
# sso login
300-
if context.can_sso_login() and not any([username, password]):
326+
if can_sso_login and not any([username, password]):
301327
from stacklet.client.platform.vendored.auth import BrowserAuthenticator
302328

303329
BrowserAuthenticator(
304-
authority_url=context.config.auth_url,
305-
client_id=context.config.cognito_client_id,
306-
idp_id=context.config.idp_id,
330+
authority_url=config.auth_url,
331+
client_id=config.cognito_client_id,
332+
idp_id=config.idp_id,
307333
)()
308334
return
309-
elif not context.can_sso_login() and not any([username, password]):
335+
elif not can_sso_login and not any([username, password]):
310336
click.echo(
311337
"To login with SSO ensure that your configuration includes "
312338
+ "auth_url, and cognito_client_id values."
@@ -318,15 +344,12 @@ def login(ctx, username, password):
318344
username = click.prompt("Username")
319345
if not password:
320346
password = click.prompt("Password", hide_input=True)
321-
manager = CognitoUserManager.from_context(context)
322-
res = manager.login(
347+
manager = CognitoUserManager.from_context(ctx.obj)
348+
id_token, access_token = manager.login(
323349
user=username,
324350
password=password,
325351
)
326-
if not os.path.exists(os.path.dirname(os.path.expanduser(StackletContext.DEFAULT_CREDENTIALS))):
327-
os.makedirs(os.path.dirname(os.path.expanduser(StackletContext.DEFAULT_CREDENTIALS)))
328-
with open(os.path.expanduser(StackletContext.DEFAULT_CREDENTIALS), "w+") as f: # noqa
329-
f.write(res)
352+
StackletCredentials().write(id_token, access_token)
330353

331354

332355
for c in commands:

‎stacklet/client/platform/client.py‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,27 @@
33

44
# platform client using cli
55

6-
from pathlib import Path
76

87
import jmespath
98

109
# import the commands so the registry is populated when the client is instantiated
11-
import stacklet.client.platform.commands # noqa
12-
from stacklet.client.platform.context import StackletContext
13-
from stacklet.client.platform.executor import StackletGraphqlExecutor
14-
from stacklet.client.platform.utils import _PAGINATION_OPTIONS, get_token
10+
from . import commands # noqa
11+
from .config import DEFAULT_CONFIG_FILE, StackletCredentials
12+
from .context import StackletContext
13+
from .executor import StackletGraphqlExecutor
14+
from .utils import _PAGINATION_OPTIONS
1515

1616

1717
def platform_client(pager=False, expr=False):
1818
# for more pythonic experience, pass expr=True to de-graphqlize the result
1919
# for automatic pagination handling, pass pager=True
20-
if (
21-
not Path(StackletContext.DEFAULT_CONFIG).expanduser().exists()
22-
or not Path(StackletContext.DEFAULT_CREDENTIALS).expanduser().exists()
23-
):
20+
creds = StackletCredentials()
21+
token = creds.api_token()
22+
if not DEFAULT_CONFIG_FILE.exists() or not token:
2423
raise ValueError("Please configure and authenticate on stacklet-admin cli")
2524

2625
d = {}
2726
ctx = StackletContext(raw_config={})
28-
token = get_token()
2927
executor = StackletGraphqlExecutor(ctx, token)
3028

3129
for k, snippet in StackletGraphqlExecutor.registry.items():

‎stacklet/client/platform/cognito.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,15 @@ def create_user(self, user, password, email, phone_number, permanent=True):
7171
self.log.debug(res)
7272
return True
7373

74-
def login(self, user, password):
74+
def login(self, user, password) -> tuple[str, str]:
7575
res = self.client.initiate_auth(
7676
ClientId=self.user_pool_client_id,
7777
AuthFlow="USER_PASSWORD_AUTH",
7878
AuthParameters={"USERNAME": user, "PASSWORD": password},
7979
)
8080
self.log.debug("Authentication Success")
81-
return res["AuthenticationResult"]["AccessToken"]
81+
auth = res["AuthenticationResult"]
82+
return auth["IdToken"], auth["AccessToken"]
8283

8384
def ensure_group(self, user, group) -> bool:
8485
try:

‎stacklet/client/platform/commands/account.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from stacklet.client.platform.executor import StackletGraphqlExecutor, _run_graphql, snippet_options
99
from stacklet.client.platform.graphql import StackletGraphqlSnippet
10-
from stacklet.client.platform.utils import click_group_entry, default_options
10+
from stacklet.client.platform.utils import default_options
1111

1212

1313
@StackletGraphqlExecutor.registry.register("list-accounts")
@@ -287,7 +287,6 @@ def account(*args, **kwargs):
287287
$ stacklet account --output json list
288288
289289
"""
290-
click_group_entry(*args, **kwargs)
291290

292291

293292
@account.command()

‎stacklet/client/platform/commands/accountgroup.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from stacklet.client.platform.executor import StackletGraphqlExecutor, _run_graphql, snippet_options
77
from stacklet.client.platform.graphql import StackletGraphqlSnippet
8-
from stacklet.client.platform.utils import click_group_entry, default_options
8+
from stacklet.client.platform.utils import default_options
99

1010

1111
@StackletGraphqlExecutor.registry.register("list-account-groups")
@@ -308,7 +308,9 @@ class RemoveAccountGroupItem(StackletGraphqlSnippet):
308308
@default_options()
309309
@click.pass_context
310310
def account_group(*args, **kwargs):
311-
click_group_entry(*args, **kwargs)
311+
"""
312+
Manage account groups
313+
"""
312314

313315

314316
@account_group.command()

‎stacklet/client/platform/commands/binding.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from stacklet.client.platform.executor import StackletGraphqlExecutor, _run_graphql, snippet_options
77
from stacklet.client.platform.graphql import StackletGraphqlSnippet
8-
from stacklet.client.platform.utils import click_group_entry, default_options
8+
from stacklet.client.platform.utils import default_options
99

1010

1111
@StackletGraphqlExecutor.registry.register("list-bindings")
@@ -265,7 +265,6 @@ def binding(*args, **kwargs):
265265
"""
266266
Query and run mutations against bindings in Stacklet Platform
267267
"""
268-
click_group_entry(*args, **kwargs)
269268

270269

271270
@binding.command()

‎stacklet/client/platform/commands/cube.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import requests
1313

1414
from stacklet.client.platform.context import StackletContext
15-
from stacklet.client.platform.utils import click_group_entry, default_options, get_token
15+
from stacklet.client.platform.utils import default_options, get_token
1616

1717

1818
@click.group()
@@ -22,7 +22,6 @@ def cubejs(*args, **kwargs):
2222
"""
2323
Run arbitrary cubejs queries
2424
"""
25-
click_group_entry(*args, **kwargs)
2625

2726

2827
@cubejs.command()

‎stacklet/client/platform/commands/graphql.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from stacklet.client.platform.executor import _run_graphql
1010
from stacklet.client.platform.graphql import AdHocSnippet
11-
from stacklet.client.platform.utils import click_group_entry, default_options
11+
from stacklet.client.platform.utils import default_options
1212

1313

1414
@click.group()
@@ -18,7 +18,6 @@ def graphql(*args, **kwargs):
1818
"""
1919
Run arbitrary graphql snippets
2020
"""
21-
click_group_entry(*args, **kwargs)
2221

2322

2423
@graphql.command()

‎stacklet/client/platform/commands/policy.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from stacklet.client.platform.exceptions import InvalidInputException
77
from stacklet.client.platform.executor import StackletGraphqlExecutor, _run_graphql, snippet_options
88
from stacklet.client.platform.graphql import StackletGraphqlSnippet
9-
from stacklet.client.platform.utils import click_group_entry, default_options
9+
from stacklet.client.platform.utils import default_options
1010

1111

1212
@StackletGraphqlExecutor.registry.register("list-policies")
@@ -144,7 +144,9 @@ class ShowPolicy(StackletGraphqlSnippet):
144144
@default_options()
145145
@click.pass_context
146146
def policy(*args, **kwargs):
147-
click_group_entry(*args, **kwargs)
147+
"""
148+
Manage policies
149+
"""
148150

149151

150152
@policy.command()

‎stacklet/client/platform/commands/policycollection.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from stacklet.client.platform.executor import StackletGraphqlExecutor, _run_graphql, snippet_options
77
from stacklet.client.platform.graphql import StackletGraphqlSnippet
8-
from stacklet.client.platform.utils import click_group_entry, default_options
8+
from stacklet.client.platform.utils import default_options
99

1010

1111
@StackletGraphqlExecutor.registry.register("list-policy-collections")
@@ -256,7 +256,9 @@ class RemovePolicyCollection(StackletGraphqlSnippet):
256256
@default_options()
257257
@click.pass_context
258258
def policy_collection(*args, **kwargs):
259-
click_group_entry(*args, **kwargs)
259+
"""
260+
Manage policy collections
261+
"""
260262

261263

262264
@policy_collection.command()

0 commit comments

Comments
 (0)