7
7
import copy
8
8
import os
9
9
from dataclasses import dataclass
10
- from typing import Any , Callable , Dict , Optional
10
+ from typing import Any , Callable , Dict , Optional , Union
11
11
12
12
import ads .telemetry
13
13
import oci
@@ -45,7 +45,7 @@ class AuthState(metaclass=SingletonMeta):
45
45
oci_cli_auth : str = None
46
46
oci_config_path : str = None
47
47
oci_key_profile : str = None
48
- oci_config : str = None
48
+ oci_config : Dict = None
49
49
oci_signer : Any = None
50
50
oci_signer_callable : Callable = None
51
51
oci_signer_kwargs : Dict = None
@@ -66,7 +66,6 @@ def __post_init__(self):
66
66
)
67
67
self .oci_config = self .oci_config or {}
68
68
self .oci_signer = self .oci_signer
69
- self .oci_signer_callable = self .oci_signer_callable
70
69
self .oci_signer_kwargs = self .oci_signer_kwargs or {}
71
70
self .oci_client_kwargs = self .oci_client_kwargs or {}
72
71
@@ -82,15 +81,14 @@ def set_auth(
82
81
client_kwargs : Optional [Dict ] = {},
83
82
) -> None :
84
83
"""
85
- Save type of authentication, profile, config location, config (keypair identity) or signer, which will be used
86
- when actual creation of config or signer happens.
84
+ Sets the default authentication type.
87
85
88
86
Parameters
89
87
----------
90
88
auth: Optional[str], default 'api_key'
91
89
'api_key', 'resource_principal' or 'instance_principal'. Enable/disable resource principal identity,
92
90
instance principal or keypair identity in a notebook session
93
- oci_config_location: Optional[str], default oci.config.DEFAULT_LOCATION, which is '~/.oci/config'
91
+ oci_config_location: Optional[str], default '~/.oci/config'
94
92
config file location
95
93
profile: Optional[str], default is DEFAULT_PROFILE, which is 'DEFAULT'
96
94
profile name for api keys config file
@@ -117,14 +115,50 @@ def set_auth(
117
115
>>> ads.set_auth("api_key", oci_config_location = "other_config_location") # use non-default oci_config_location
118
116
119
117
>>> ads.set_auth("api_key", client_kwargs={"timeout": 60}) # default signer with connection and read timeouts set to 60 seconds for the client.
120
- >>> ads.set_auth("api_key", signer_kwargs={"key_content": "private_key_content"}) # Create config using key content
118
+
121
119
>>> other_config = oci.config.from_file("other_config_location", "OTHER_PROFILE") # Create non-default config
122
120
>>> ads.set_auth(config=other_config) # Set api keys type of authentication based on provided config
123
121
122
+ >>> config={
123
+ ... user=ocid1.user.oc1..<unique_ID>,
124
+ ... fingerprint=<fingerprint>,
125
+ ... tenancy=ocid1.tenancy.oc1..<unique_ID>,
126
+ ... region=us-ashburn-1,
127
+ ... key_content=<private key content>,
128
+ ... }
129
+ >>> ads.set_auth(config=config) # Set api key authentication using private key content based on provided config
130
+
131
+ >>> config={
132
+ ... user=ocid1.user.oc1..<unique_ID>,
133
+ ... fingerprint=<fingerprint>,
134
+ ... tenancy=ocid1.tenancy.oc1..<unique_ID>,
135
+ ... region=us-ashburn-1,
136
+ ... key_file=~/.oci/oci_api_key.pem,
137
+ ... }
138
+ >>> ads.set_auth(config=config) # Set api key authentication using private key file location based on provided config
139
+
124
140
>>> ads.set_auth("resource_principal") # Set resource principal authentication
125
141
126
142
>>> ads.set_auth("instance_principal") # Set instance principal authentication
127
143
144
+ >>> singer = oci.signer.Signer(
145
+ ... user=ocid1.user.oc1..<unique_ID>,
146
+ ... fingerprint=<fingerprint>,
147
+ ... tenancy=ocid1.tenancy.oc1..<unique_ID>,
148
+ ... region=us-ashburn-1,
149
+ ... private_key_content=<private key content>,
150
+ ... )
151
+ >>> ads.set_auth(singer=singer) # Set api keys authentication with private key content based on provided signer
152
+
153
+ >>> singer = oci.signer.Signer(
154
+ ... user=ocid1.user.oc1..<unique_ID>,
155
+ ... fingerprint=<fingerprint>,
156
+ ... tenancy=ocid1.tenancy.oc1..<unique_ID>,
157
+ ... region=us-ashburn-1,
158
+ ... private_key_file_location=<private key content>,
159
+ ... )
160
+ >>> ads.set_auth(singer=singer) # Set api keys authentication with private key file location based on provided signer
161
+
128
162
>>> singer = oci.auth.signers.get_resource_principals_signer()
129
163
>>> ads.auth.create_signer(config={}, singer=signer) # resource principals authentication dictionary created
130
164
@@ -157,47 +191,30 @@ def set_auth(
157
191
158
192
auth_state .oci_config = config
159
193
auth_state .oci_key_profile = profile
160
- if auth == AuthType .API_KEY and not signer and not signer_callable and not signer_kwargs :
161
- if os .path .exists (os .path .expanduser (oci_config_location )):
162
- auth_state .oci_config_path = oci_config_location
163
- else :
164
- raise ValueError (
165
- f"{ oci_config_location } path does not exist, please provide existing path to config file."
166
- )
167
-
194
+ auth_state .oci_config_path = oci_config_location
168
195
auth_state .oci_signer = signer
169
196
auth_state .oci_signer_callable = signer_callable
170
197
auth_state .oci_signer_kwargs = signer_kwargs
171
198
auth_state .oci_client_kwargs = client_kwargs
172
199
173
200
174
201
def api_keys (
175
- oci_config : str = os .path .join ( os . path . expanduser ("~" ), ".oci" , "config" ),
202
+ oci_config : Union [ str , Dict ] = os .path .expanduser (DEFAULT_LOCATION ),
176
203
profile : str = DEFAULT_PROFILE ,
177
204
client_kwargs : Dict = None ,
178
- kwargs : Dict = None
179
205
) -> Dict :
180
206
"""
181
207
Prepares authentication and extra arguments necessary for creating clients for different OCI services using API
182
208
Keys.
183
209
184
210
Parameters
185
211
----------
186
- oci_config: Optional[str] , default is $HOME /.oci/config
187
- OCI authentication config file location.
212
+ oci_config: Optional[Union[ str, Dict]] , default is ~ /.oci/config
213
+ OCI authentication config file location or a dictionary with config attributes .
188
214
profile: Optional[str], is DEFAULT_PROFILE, which is 'DEFAULT'
189
215
Profile name to select from the config file.
190
216
client_kwargs: Optional[Dict], default None
191
217
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.
201
218
202
219
Returns
203
220
-------
@@ -215,10 +232,12 @@ def api_keys(
215
232
>>> oc.OCIClientFactory(**auth).object_storage # Creates Object storage client with timeout set to 6000 using API Key authentication
216
233
"""
217
234
signer_args = dict (
218
- oci_config_location = oci_config ,
235
+ oci_config = oci_config if isinstance (oci_config , Dict ) else {},
236
+ oci_config_location = oci_config
237
+ if isinstance (oci_config , str )
238
+ else os .path .expanduser (DEFAULT_LOCATION ),
219
239
oci_key_profile = profile ,
220
240
client_kwargs = client_kwargs ,
221
- signer_kwargs = kwargs ,
222
241
)
223
242
signer_generator = AuthFactory ().signerGenerator (AuthType .API_KEY )
224
243
return signer_generator (signer_args ).create_signer ()
@@ -302,6 +321,24 @@ def create_signer(
302
321
>>> config = oci.config.from_file("other_config_location", "OTHER_PROFILE")
303
322
>>> auth = ads.auth.create_signer(config=config) # api_key type of authentication dictionary created based on provided config
304
323
324
+ >>> config={
325
+ ... user=ocid1.user.oc1..<unique_ID>,
326
+ ... fingerprint=<fingerprint>,
327
+ ... tenancy=ocid1.tenancy.oc1..<unique_ID>,
328
+ ... region=us-ashburn-1,
329
+ ... key_content=<private key content>,
330
+ ... }
331
+ >>> auth = ads.auth.create_signer(config=config) # api_key type of authentication dictionary with private key content created based on provided config
332
+
333
+ >>> config={
334
+ ... user=ocid1.user.oc1..<unique_ID>,
335
+ ... fingerprint=<fingerprint>,
336
+ ... tenancy=ocid1.tenancy.oc1..<unique_ID>,
337
+ ... region=us-ashburn-1,
338
+ ... key_file=~/.oci/oci_api_key.pem,
339
+ ... }
340
+ >>> auth = ads.auth.create_signer(config=config) # api_key type of authentication dictionary with private key file location created based on provided config
341
+
305
342
>>> singer = oci.auth.signers.get_resource_principals_signer()
306
343
>>> auth = ads.auth.create_signer(config={}, signer=signer) # resource principals authentication dictionary created
307
344
@@ -327,7 +364,6 @@ def create_signer(
327
364
oci_config_location = oci_config_location ,
328
365
oci_key_profile = profile ,
329
366
oci_config = config ,
330
- signer_kwargs = signer_kwargs ,
331
367
client_kwargs = client_kwargs ,
332
368
)
333
369
if config :
@@ -398,7 +434,6 @@ def default_signer(client_kwargs: Optional[Dict] = None) -> Dict:
398
434
oci_config_location = auth_state .oci_config_path ,
399
435
oci_key_profile = auth_state .oci_key_profile ,
400
436
oci_config = auth_state .oci_config ,
401
- signer_kwargs = auth_state .oci_signer_kwargs or {},
402
437
client_kwargs = {
403
438
** (auth_state .oci_client_kwargs or {}),
404
439
** (client_kwargs or {}),
@@ -483,22 +518,20 @@ def __init__(self, args: Optional[Dict] = None):
483
518
- oci_config_location - path to config file
484
519
- oci_key_profile - the profile to load from config file
485
520
- client_kwargs - optional parameters for OCI client creation in next steps
486
- - signer_kwargs - optional parameters for signer
487
521
"""
488
522
self .oci_config = args .get ("oci_config" )
489
523
self .oci_config_location = args .get ("oci_config_location" )
490
524
self .oci_key_profile = args .get ("oci_key_profile" )
491
525
self .client_kwargs = args .get ("client_kwargs" )
492
- self .signer_kwargs = args .get ("signer_kwargs" )
493
526
494
527
def create_signer (self ) -> Dict :
495
528
"""
496
529
Creates api keys configuration and signer with extra arguments necessary for creating clients.
497
530
Signer constructed from the `oci_config` provided. If not 'oci_config', configuration will be
498
531
constructed from 'oci_config_location' and 'oci_key_profile' in place.
499
532
500
- Resturns
501
- --------
533
+ Returns
534
+ -------
502
535
dict
503
536
Contains keys - config, signer and client_kwargs.
504
537
@@ -517,23 +550,22 @@ def create_signer(self) -> Dict:
517
550
"""
518
551
if self .oci_config :
519
552
configuration = ads .telemetry .update_oci_client_config (self .oci_config )
520
- elif self .signer_kwargs :
521
- configuration = ads .telemetry .update_oci_client_config (self .signer_kwargs )
522
553
else :
523
554
configuration = ads .telemetry .update_oci_client_config (
524
555
oci .config .from_file (self .oci_config_location , self .oci_key_profile )
525
556
)
526
-
557
+
558
+ oci .config .validate_config (configuration )
527
559
logger .info (f"Using 'api_key' authentication." )
528
560
return {
529
561
"config" : configuration ,
530
562
"signer" : oci .signer .Signer (
531
- configuration . get ( "tenancy" ) ,
532
- configuration . get ( "user" ) ,
533
- configuration . get ( "fingerprint" ) ,
534
- configuration .get ("key_file" ),
535
- configuration .get ("pass_phrase" ),
536
- configuration .get ("key_content" )
563
+ tenancy = configuration [ "tenancy" ] ,
564
+ user = configuration [ "user" ] ,
565
+ fingerprint = configuration [ "fingerprint" ] ,
566
+ private_key_file_location = configuration .get ("key_file" ),
567
+ pass_phrase = configuration .get ("pass_phrase" ),
568
+ private_key_content = configuration .get ("key_content" )
537
569
),
538
570
"client_kwargs" : self .client_kwargs ,
539
571
}
@@ -563,8 +595,8 @@ def create_signer(self) -> Dict:
563
595
"""
564
596
Creates Resource Principal signer with extra arguments necessary for creating clients.
565
597
566
- Resturns
567
- --------
598
+ Returns
599
+ -------
568
600
dict
569
601
Contains keys - config, signer and client_kwargs.
570
602
@@ -619,8 +651,8 @@ def create_signer(self) -> Dict:
619
651
Signer instantiated from the `signer_callable` or if the `signer` provided is will be return by this method.
620
652
If `signer_callable` or `signer` not provided new signer will be created in place.
621
653
622
- Resturns
623
- --------
654
+ Returns
655
+ -------
624
656
dict
625
657
Contains keys - config, signer and client_kwargs.
626
658
0 commit comments