@@ -117,7 +117,7 @@ def set_auth(
117
117
>>> ads.set_auth("api_key", oci_config_location = "other_config_location") # use non-default oci_config_location
118
118
119
119
>>> ads.set_auth("api_key", client_kwargs={"timeout": 60}) # default signer with connection and read timeouts set to 60 seconds for the client.
120
-
120
+ >>> ads.set_auth("api_key", )
121
121
>>> other_config = oci.config.from_file("other_config_location", "OTHER_PROFILE") # Create non-default config
122
122
>>> ads.set_auth(config=other_config) # Set api keys type of authentication based on provided config
123
123
@@ -157,7 +157,7 @@ def set_auth(
157
157
158
158
auth_state .oci_config = config
159
159
auth_state .oci_key_profile = profile
160
- if auth == AuthType .API_KEY and not signer and not signer_callable :
160
+ if auth == AuthType .API_KEY and not signer and not signer_callable and not signer_kwargs :
161
161
if os .path .exists (os .path .expanduser (oci_config_location )):
162
162
auth_state .oci_config_path = oci_config_location
163
163
else :
@@ -175,6 +175,7 @@ def api_keys(
175
175
oci_config : str = os .path .join (os .path .expanduser ("~" ), ".oci" , "config" ),
176
176
profile : str = DEFAULT_PROFILE ,
177
177
client_kwargs : Dict = None ,
178
+ kwargs : Dict = None
178
179
) -> Dict :
179
180
"""
180
181
Prepares authentication and extra arguments necessary for creating clients for different OCI services using API
@@ -188,6 +189,15 @@ def api_keys(
188
189
Profile name to select from the config file.
189
190
client_kwargs: Optional[Dict], default None
190
191
kwargs that are required to instantiate the Client if we need to override the defaults.
192
+ kwargs:
193
+ kwargs for API authentication signer.
194
+ - user: OCID of the user calling the API.
195
+ - tenancy: OCID of user's tenancy.
196
+ - fingerprint: Fingerprint for the public key that was added to this user.
197
+ - region: An Oracle Cloud Infrastructure region.
198
+ - pass_phrase: Passphrase used for the key, if it is encrypted.
199
+ - key_file: Full path and filename of the private key.
200
+ - key_content: The private key as PEM string.
191
201
192
202
Returns
193
203
-------
@@ -208,6 +218,7 @@ def api_keys(
208
218
oci_config_location = oci_config ,
209
219
oci_key_profile = profile ,
210
220
client_kwargs = client_kwargs ,
221
+ signer_kwargs = kwargs ,
211
222
)
212
223
signer_generator = AuthFactory ().signerGenerator (AuthType .API_KEY )
213
224
return signer_generator (signer_args ).create_signer ()
@@ -316,6 +327,7 @@ def create_signer(
316
327
oci_config_location = oci_config_location ,
317
328
oci_key_profile = profile ,
318
329
oci_config = config ,
330
+ signer_kwargs = signer_kwargs ,
319
331
client_kwargs = client_kwargs ,
320
332
)
321
333
if config :
@@ -386,6 +398,7 @@ def default_signer(client_kwargs: Optional[Dict] = None) -> Dict:
386
398
oci_config_location = auth_state .oci_config_path ,
387
399
oci_key_profile = auth_state .oci_key_profile ,
388
400
oci_config = auth_state .oci_config ,
401
+ signer_kwargs = auth_state .oci_signer_kwargs or {},
389
402
client_kwargs = {
390
403
** (auth_state .oci_client_kwargs or {}),
391
404
** (client_kwargs or {}),
@@ -470,11 +483,13 @@ def __init__(self, args: Optional[Dict] = None):
470
483
- oci_config_location - path to config file
471
484
- oci_key_profile - the profile to load from config file
472
485
- client_kwargs - optional parameters for OCI client creation in next steps
486
+ - signer_kwargs - optional parameters for signer
473
487
"""
474
488
self .oci_config = args .get ("oci_config" )
475
489
self .oci_config_location = args .get ("oci_config_location" )
476
490
self .oci_key_profile = args .get ("oci_key_profile" )
477
491
self .client_kwargs = args .get ("client_kwargs" )
492
+ self .signer_kwargs = args .get ("signer_kwargs" )
478
493
479
494
def create_signer (self ) -> Dict :
480
495
"""
@@ -503,18 +518,27 @@ def create_signer(self) -> Dict:
503
518
if self .oci_config :
504
519
configuration = ads .telemetry .update_oci_client_config (self .oci_config )
505
520
else :
506
- configuration = ads .telemetry .update_oci_client_config (
507
- oci .config .from_file (self .oci_config_location , self .oci_key_profile )
508
- )
521
+ try :
522
+ configuration = ads .telemetry .update_oci_client_config (
523
+ oci .config .from_file (self .oci_config_location , self .oci_key_profile )
524
+ )
525
+ except :
526
+ if not os .path .exists (os .path .expanduser (self .oci_config_location )):
527
+ logger .info (f"Failed to get config from folder { self .oci_config_location } . Using 'signer_kwargs' instead." )
528
+ configuration = ads .telemetry .update_oci_client_config (self .signer_kwargs )
529
+ else :
530
+ raise
531
+
509
532
logger .info (f"Using 'api_key' authentication." )
510
533
return {
511
534
"config" : configuration ,
512
535
"signer" : oci .signer .Signer (
513
- configuration [ "tenancy" ] ,
514
- configuration [ "user" ] ,
515
- configuration [ "fingerprint" ] ,
516
- configuration [ "key_file" ] ,
536
+ configuration . get ( "tenancy" ) ,
537
+ configuration . get ( "user" ) ,
538
+ configuration . get ( "fingerprint" ) ,
539
+ configuration . get ( "key_file" ) ,
517
540
configuration .get ("pass_phrase" ),
541
+ configuration .get ("key_content" )
518
542
),
519
543
"client_kwargs" : self .client_kwargs ,
520
544
}
0 commit comments