1010import jwt
1111import 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
16+ from .context import StackletContext
17+ from .utils import default_options , setup_logging
1918
2019
2120@click .group ()
2221@default_options ()
2322@click .pass_context
24- def cli (* args , ** kwargs ):
23+ def cli (
24+ ctx ,
25+ config ,
26+ output ,
27+ cognito_user_pool_id ,
28+ cognito_client_id ,
29+ cognito_region ,
30+ api ,
31+ v ,
32+ ):
2533 """
2634 Stacklet CLI
2735
@@ -77,7 +85,25 @@ def cli(*args, **kwargs):
7785 --after $after_token \\
7886 list
7987 """
80- click_group_entry (* args , ** kwargs )
88+ setup_logging (v )
89+
90+ config_items = [cognito_user_pool_id , cognito_client_id , cognito_region , api ]
91+ if any (config_items ) and not all (config_items ):
92+ raise Exception (
93+ "All options must be set for config items: --cognito-user-pool-id, "
94+ + "--cognito-client-id, --cognito-region, and --api"
95+ )
96+
97+ raw_config = None
98+ if all (config_items ):
99+ raw_config = {
100+ "cognito_user_pool_id" : cognito_user_pool_id ,
101+ "cognito_client_id" : cognito_client_id ,
102+ "region" : cognito_region ,
103+ "api" : api ,
104+ }
105+
106+ ctx .obj = StackletContext (raw_config = raw_config , config_file = config , output_format = output )
81107
82108
83109@cli .command (short_help = "Configure stacklet-admin cli" )
@@ -89,9 +115,7 @@ def cli(*args, **kwargs):
89115@click .option ("--auth-url" , prompt = "(SSO) Auth Url" , default = "" )
90116@click .option ("--cubejs" , prompt = "Stacklet cube.js endpoint" , default = "" )
91117@click .option ("--location" , prompt = "Config File Location" , default = "~/.stacklet/config.json" ) # noqa
92- @click .pass_context
93118def configure (
94- ctx ,
95119 api ,
96120 region ,
97121 cognito_client_id ,
@@ -263,26 +287,24 @@ def auto_configure(url, prefix, idp, location):
263287
264288
265289@cli .command ()
266- @click .pass_context
267- def show (ctx ):
290+ @click .pass_obj
291+ def show (obj ):
268292 """
269293 Show your config
270294 """
271- context = StackletContext (ctx .obj ["config" ], ctx .obj ["raw_config" ])
272- 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 })
295+ fmt = obj .formatter ()
296+ if id_token := obj .credentials .id_token ():
297+ id_details = jwt .decode (id_token , options = {"verify_signature" : False })
276298 click .echo (fmt (id_details ))
277299 click .echo ()
278- click .echo (fmt (context .config .to_json ()))
300+ click .echo (fmt (obj .config .to_json ()))
279301
280302
281303@cli .command (short_help = "Login to Stacklet" )
282304@click .option ("--username" , required = False )
283305@click .option ("--password" , hide_input = True , required = False )
284- @click .pass_context
285- def login (ctx , username , password ):
306+ @click .pass_obj
307+ def login (obj , username , password ):
286308 """
287309 Login to Stacklet
288310
@@ -295,18 +317,19 @@ def login(ctx, username, password):
295317
296318 If password is not passed in, your password will be prompted
297319 """
298- context = StackletContext (ctx .obj ["config" ], ctx .obj ["raw_config" ])
320+ config = obj .config
321+ can_sso_login = obj .can_sso_login ()
299322 # sso login
300- if context . can_sso_login () and not any ([username , password ]):
323+ if can_sso_login and not any ([username , password ]):
301324 from stacklet .client .platform .vendored .auth import BrowserAuthenticator
302325
303326 BrowserAuthenticator (
304- authority_url = context . config .auth_url ,
305- client_id = context . config .cognito_client_id ,
306- idp_id = context . config .idp_id ,
327+ authority_url = config .auth_url ,
328+ client_id = config .cognito_client_id ,
329+ idp_id = config .idp_id ,
307330 )()
308331 return
309- elif not context . can_sso_login () and not any ([username , password ]):
332+ elif not can_sso_login and not any ([username , password ]):
310333 click .echo (
311334 "To login with SSO ensure that your configuration includes "
312335 + "auth_url, and cognito_client_id values."
@@ -318,15 +341,12 @@ def login(ctx, username, password):
318341 username = click .prompt ("Username" )
319342 if not password :
320343 password = click .prompt ("Password" , hide_input = True )
321- manager = CognitoUserManager .from_context (context )
322- res = manager .login (
344+ manager = CognitoUserManager .from_context (obj )
345+ id_token , access_token = manager .login (
323346 user = username ,
324347 password = password ,
325348 )
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 )
349+ obj .credentials .write (id_token , access_token )
330350
331351
332352for c in commands :
0 commit comments