Skip to content

Commit 8105c82

Browse files
Fix auth handling in OCI config provider.
1 parent 08e669a commit 8105c82

File tree

1 file changed

+65
-30
lines changed

1 file changed

+65
-30
lines changed

src/oracledb/plugins/oci_config_provider.py

Lines changed: 65 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,20 @@
5353
def _get_config(parameters, connect_params):
5454
config = {}
5555

56-
credential = _get_credential(parameters)
57-
client_oci = oci_object_storage_client(credential)
56+
credential, signer = _get_credential(parameters)
57+
auth_method = parameters.get("auth")
58+
if auth_method is not None:
59+
auth_method = auth_method.upper()
60+
61+
if auth_method is None or auth_method == "OCI_DEFAULT":
62+
client_oci = oci_object_storage_client(credential)
63+
elif (
64+
auth_method == "OCI_INSTANCE_PRINCIPAL"
65+
or auth_method == "OCI_RESOURCE_PRINCIPAL"
66+
):
67+
client_oci = oci_object_storage_client(
68+
config=credential, signer=signer
69+
)
5870
get_object_request = {
5971
"object_name": _get_required_parameter(parameters, "filename"),
6072
"bucket_name": _get_required_parameter(parameters, "bucketname"),
@@ -79,6 +91,7 @@ def _get_config(parameters, connect_params):
7991
pwd = settings["password"]
8092
if settings["password"]["type"] == "oci-vault":
8193
pwd["credential"] = credential
94+
pwd["auth"] = auth_method
8295

8396
# password should be stored in JSON and not plain text.
8497
config["password"] = pwd
@@ -99,33 +112,39 @@ def _get_credential(parameters):
99112
if auth is not None:
100113
auth = auth.upper()
101114

102-
if auth is None or auth == "OCI_DEFAULT":
103-
# Default Authentication
104-
# default path ~/.oci/config
105-
return oci_from_file()
106-
if "tenancy_user" in parameters and "oci_user" in parameters:
107-
with open(parameters["oci_key_file"], "r") as file_content:
108-
public_key = file_content.read()
109-
_retrieve_region(parameters.get("objservername"))
110-
provider = oci.signer.Signer(
111-
tenancy=parameters["oci_tenancy"],
112-
user=parameters["oci_user"],
113-
fingerprint=parameters["oci_fingerprint"],
114-
private_key_file_location=parameters["oci_key_file"],
115-
private_key_content=public_key,
116-
pass_phrase=None,
117-
)
118-
else:
115+
try:
116+
if auth is None or auth == "OCI_DEFAULT":
117+
# Default Authentication
118+
# default path ~/.oci/config
119+
return oci_from_file(), None
120+
except oci.exceptions.ClientError:
121+
# try to create config with connection string parameters.
122+
if "oci_tenancy" in parameters and "oci_user" in parameters:
123+
with open(parameters["oci_key_file"], "r") as file_content:
124+
public_key = file_content.read()
125+
provider = dict(
126+
tenancy=parameters["oci_tenancy"],
127+
user=parameters["oci_user"],
128+
fingerprint=parameters["oci_fingerprint"],
129+
key_file=parameters["oci_key_file"],
130+
private_key_content=public_key,
131+
region=_retrieve_region(parameters.get("objservername")),
132+
)
133+
return provider, None
134+
135+
if auth == "OCI_INSTANCE_PRINCIPAL":
119136
signer = oci.auth.signers.InstancePrincipalsSecurityTokenSigner()
137+
return (
138+
dict(region=_retrieve_region(parameters.get("objservername"))),
139+
signer,
140+
)
141+
142+
elif auth == "OCI_RESOURCE_PRINCIPAL":
120143
rps = oci.auth.signers.get_resource_principals_signer()
121-
if parameters[auth].upper() == "OCI_INSTANCE_PRINCIPAL":
122-
provider = signer().build()
123-
elif parameters[auth].upper() == "OCI_RESOURCE_PRINCIPAL":
124-
provider = rps.builder().build()
125-
else:
126-
msg = "Authentication options not available in Connection String"
127-
raise Exception(msg)
128-
return provider
144+
return {}, rps
145+
else:
146+
msg = "Authentication options not available in Connection String"
147+
raise Exception(msg)
129148

130149

131150
def _get_required_parameter(parameters, name):
@@ -170,9 +189,25 @@ def password_type_oci_vault_hook(args):
170189
raise Exception(
171190
"OCI Key Vault authentication details are not provided."
172191
)
173-
credential = _get_credential(auth)
192+
credential, signer = _get_credential(auth)
193+
auth_method = args.get("auth")
194+
195+
if auth_method is not None:
196+
auth_method = auth_method.upper()
197+
198+
if auth_method is None or auth_method == "OCI_DEFAULT":
199+
secret_client_oci = oci_secrets_client(credential)
200+
elif auth_method == "OCI_INSTANCE_PRINCIPAL":
201+
signer = oci.auth.signers.InstancePrincipalsSecurityTokenSigner()
202+
secret_client_oci = oci_secrets_client(
203+
config=credential, signer=signer
204+
)
205+
elif auth_method == "OCI_RESOURCE_PRINCIPAL":
206+
signer = oci.auth.signers.get_resource_principals_signer()
207+
secret_client_oci = oci_secrets_client(
208+
config=credential, signer=signer
209+
)
174210

175-
secret_client_oci = oci_secrets_client(credential)
176211
get_secret_bundle_request = {"secret_id": secret_id}
177212
get_secret_bundle_response = secret_client_oci.get_secret_bundle(
178213
**get_secret_bundle_request
@@ -182,7 +217,7 @@ def password_type_oci_vault_hook(args):
182217

183218
def _retrieve_region(objservername):
184219
arr = objservername.split(".")
185-
return arr[1].upper().replace("-", "_")
220+
return arr[1].lower().replace("_", "-")
186221

187222

188223
def _stream_to_string(stream):

0 commit comments

Comments
 (0)