16
16
17
17
import logging
18
18
import os .path
19
+ from copy import deepcopy
19
20
from pathlib import Path
20
- from typing import Dict , Optional
21
+ from typing import Dict , Optional , Tuple
21
22
22
23
import typer
23
24
from click import ( # type: ignore
35
36
strip_if_value_present ,
36
37
)
37
38
from snowflake .cli ._plugins .object .manager import ObjectManager
39
+ from snowflake .cli .api import exceptions
38
40
from snowflake .cli .api .cli_global_context import get_cli_context
39
41
from snowflake .cli .api .commands .flags import (
40
42
PLAIN_PASSWORD_MSG ,
@@ -288,7 +290,11 @@ def add(
288
290
raise UsageError (f"Connection { connection_name } already exists" )
289
291
290
292
if not no_interactive :
291
- _extend_add_with_key_pair (connection_name , connection_options )
293
+ connection_options , keypair_error = _extend_add_with_key_pair (
294
+ connection_name , connection_options
295
+ )
296
+ else :
297
+ keypair_error = ""
292
298
293
299
connections_file = add_connection_to_proper_file (
294
300
connection_name ,
@@ -297,6 +303,12 @@ def add(
297
303
if set_as_default :
298
304
set_config_value (path = ["default_connection_name" ], value = connection_name )
299
305
306
+ if keypair_error :
307
+ return MessageResult (
308
+ f"Wrote new password-based connection { connection_name } to { connections_file } , "
309
+ f"however there were some issues during key pair setup. Review the following error and check 'snow auth keypair' "
310
+ f"commands to setup key pair authentication:\n * { keypair_error } "
311
+ )
300
312
return MessageResult (
301
313
f"Wrote new connection { connection_name } to { connections_file } "
302
314
)
@@ -412,43 +424,57 @@ def _decrypt(passphrase: str | None):
412
424
raise ClickException (str (err ))
413
425
414
426
415
- def _extend_add_with_key_pair (connection_name : str , connection_options : Dict ):
416
- if (
427
+ def _extend_add_with_key_pair (
428
+ connection_name : str , connection_options : Dict
429
+ ) -> Tuple [Dict , str ]:
430
+ if not _should_extend_with_key_pair (connection_options ):
431
+ return connection_options , ""
432
+
433
+ configure_key_pair = typer .confirm (
434
+ "Do you want to configure key pair authentication?" ,
435
+ default = False ,
436
+ )
437
+ if not configure_key_pair :
438
+ return connection_options , ""
439
+
440
+ key_length = typer .prompt (
441
+ "Key length" ,
442
+ default = 2048 ,
443
+ show_default = True ,
444
+ )
445
+
446
+ output_path = typer .prompt (
447
+ "Output path" ,
448
+ default = KEY_PAIR_DEFAULT_PATH ,
449
+ show_default = True ,
450
+ value_proc = lambda value : SecurePath (value ),
451
+ )
452
+ private_key_passphrase = typer .prompt (
453
+ "Private key passphrase" ,
454
+ default = "" ,
455
+ hide_input = True ,
456
+ show_default = False ,
457
+ value_proc = lambda value : SecretType (value ),
458
+ )
459
+ connection = connect_to_snowflake (temporary_connection = True , ** connection_options )
460
+ try :
461
+ connection_options = AuthManager (connection = connection ).extend_connection_add (
462
+ connection_name = connection_name ,
463
+ connection_options = deepcopy (connection_options ),
464
+ key_length = key_length ,
465
+ output_path = output_path ,
466
+ private_key_passphrase = private_key_passphrase ,
467
+ )
468
+ except exceptions .CouldNotSetKeyPairError :
469
+ return connection_options , "The public key is set already."
470
+ except Exception as e :
471
+ return connection_options , str (e )
472
+ return connection_options , ""
473
+
474
+
475
+ def _should_extend_with_key_pair (connection_options : Dict ) -> bool :
476
+ return (
417
477
connection_options .get ("password" ) is not None
418
478
and connection_options .get ("private_key_file" ) is None
419
479
and connection_options .get ("private_key_path" ) is None
420
- ):
421
- configure_key_pair = typer .confirm (
422
- "Do you want to configure key pair authentication?" ,
423
- default = False ,
424
- )
425
- if configure_key_pair :
426
- key_length = typer .prompt (
427
- "Key length" ,
428
- default = 2048 ,
429
- show_default = True ,
430
- )
431
-
432
- output_path = typer .prompt (
433
- "Output path" ,
434
- default = KEY_PAIR_DEFAULT_PATH ,
435
- show_default = True ,
436
- value_proc = lambda value : SecurePath (value ),
437
- )
438
- private_key_passphrase = typer .prompt (
439
- "Private key passphrase" ,
440
- default = "" ,
441
- hide_input = True ,
442
- show_default = False ,
443
- value_proc = lambda value : SecretType (value ),
444
- )
445
- connection = connect_to_snowflake (
446
- temporary_connection = True , ** connection_options
447
- )
448
- AuthManager (connection = connection ).extend_connection_add (
449
- connection_name = connection_name ,
450
- connection_options = connection_options ,
451
- key_length = key_length ,
452
- output_path = output_path ,
453
- private_key_passphrase = private_key_passphrase ,
454
- )
480
+ )
0 commit comments