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 , 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
332355for c in commands :
0 commit comments