Skip to content

Commit 6f48b3c

Browse files
committed
Add dsn attribute to ADWSecretKeeper
1 parent 2799630 commit 6f48b3c

File tree

2 files changed

+67
-9
lines changed

2 files changed

+67
-9
lines changed

ads/secrets/adb.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
#!/usr/bin/env python
2-
# -*- coding: utf-8 -*--
32

4-
# Copyright (c) 2021, 2022 Oracle and/or its affiliates.
3+
# Copyright (c) 2021, 2024 Oracle and/or its affiliates.
54
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
65

7-
import ads
8-
from ads.secrets import SecretKeeper, Secret
96
import json
107
import os
118
import tempfile
129
import zipfile
10+
1311
from tqdm.auto import tqdm
1412

13+
import ads
14+
from ads.secrets import Secret, SecretKeeper
15+
1516
logger = ads.getLogger("ads.secrets")
1617

1718
from dataclasses import dataclass, field
@@ -40,6 +41,7 @@ class ADBSecret(Secret):
4041
wallet_secret_ids: list = field(
4142
repr=False, default_factory=list
4243
) # Not exposed through environment or `to_dict` function
44+
dsn: str = field(default=None)
4345

4446
def __post_init__(self):
4547
self.wallet_file_name = (
@@ -76,6 +78,22 @@ class ADBSecretKeeper(SecretKeeper):
7678
>>> print(adw_keeper.secret_id) # Prints the secret_id of the stored credentials
7779
>>> adw_keeper.export_vault_details("adw_employee_att.json", format="json") # Save the secret id and vault info to a json file
7880
81+
82+
>>> # Saving credentials for TLS connection
83+
>>> from ads.secrets.adw import ADBSecretKeeper
84+
>>> vault_id = "ocid1.vault.oc1..<unique_ID>"
85+
>>> kid = "ocid1.ke..<unique_ID>"
86+
87+
>>> import ads
88+
>>> ads.set_auth("resource_principal") # If using resource principal for authentication
89+
>>> connection_parameters={
90+
... "user_name":"admin",
91+
... "password":"<your password>",
92+
... "dsn":"<dsn string>"
93+
... }
94+
>>> adw_keeper = ADBSecretKeeper(vault_id=vault_id, key_id=kid, **connection_parameters)
95+
>>> adw_keeper.save("adw_employee", "My DB credentials", freeform_tags={"schema":"emp"})
96+
7997
>>> # Loading credentails
8098
>>> import ads
8199
>>> ads.set_auth("resource_principal") # If using resource principal for authentication
@@ -133,6 +151,7 @@ def __init__(
133151
wallet_dir: str = None,
134152
repository_path: str = None,
135153
repository_key: str = None,
154+
dsn: str = None,
136155
**kwargs,
137156
):
138157
"""
@@ -152,6 +171,8 @@ def __init__(
152171
Path to credentials repository. For more details refer `ads.database.connection`
153172
repository_key: (str, optional). Default None.
154173
Configuration key for loading the right configuration from repository. For more details refer `ads.database.connection`
174+
dsn: (str, optional). Default None.
175+
dsn string copied from the OCI console for TLS connection
155176
kwargs:
156177
vault_id: str. OCID of the vault where the secret is stored. Required for saving secret.
157178
key_id: str. OCID of the key used for encrypting the secret. Required for saving secret.
@@ -180,6 +201,7 @@ def __init__(
180201
password=password,
181202
service_name=service_name,
182203
wallet_location=wallet_location,
204+
dsn=dsn,
183205
)
184206
self.wallet_dir = wallet_dir
185207

@@ -252,7 +274,7 @@ def decode(self) -> "ads.secrets.adb.ADBSecretKeeper":
252274
logger.debug(f"Setting wallet file to {self.data.wallet_location}")
253275
data.wallet_location = self.data.wallet_location
254276
elif data.wallet_secret_ids and len(data.wallet_secret_ids) > 0:
255-
logger.debug(f"Secret ids corresponding to the wallet files found.")
277+
logger.debug("Secret ids corresponding to the wallet files found.")
256278
# If the secret ids for wallet files are available in secret, then we
257279
# can generate the wallet file.
258280

tests/unitary/default_setup/secret/test_secretkeeper_adw.py

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
22

3-
# Copyright (c) 2021, 2023 Oracle and/or its affiliates.
3+
# Copyright (c) 2021, 2024 Oracle and/or its affiliates.
44
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
55

66
from ads.secrets import ADBSecretKeeper
@@ -36,6 +36,24 @@ def key_encoding():
3636
)
3737

3838

39+
@pytest.fixture
40+
def key_encoding_dsn():
41+
user_name = "myuser"
42+
password = "this-is-not-the-secret"
43+
dsn = "my long dsn string....................."
44+
secret_dict = {
45+
"user_name": user_name,
46+
"password": password,
47+
"dsn": dsn,
48+
}
49+
encoded = b64encode(json.dumps(secret_dict).encode("utf-8")).decode("utf-8")
50+
return (
51+
(user_name, password, dsn),
52+
secret_dict,
53+
encoded,
54+
)
55+
56+
3957
def generate_wallet_data(wallet_zip_path, wallet_dir_path):
4058
files = 4
4159
file_content = {}
@@ -133,9 +151,9 @@ def test_encode(mock_client, mock_signer, key_encoding):
133151

134152
@patch("ads.common.auth.default_signer")
135153
@patch("ads.common.oci_client.OCIClientFactory")
136-
def test_adw_save(mock_client, mock_signer, key_encoding, tmpdir):
154+
def test_adw_tls_save(mock_client, mock_signer, key_encoding_dsn, tmpdir):
137155
adwsecretkeeper = ADBSecretKeeper(
138-
*key_encoding[0],
156+
**key_encoding_dsn[1],
139157
vault_id="ocid.vault",
140158
key_id="ocid.key",
141159
compartment_id="dummy",
@@ -211,6 +229,7 @@ def test_adw_context(mock_client, mock_signer, key_encoding):
211229
assert adwsecretkeeper == {
212230
**key_encoding[1],
213231
"wallet_location": "/this/is/mywallet.zip",
232+
"dsn": None,
214233
}
215234
assert os.environ.get("user_name") == key_encoding[0][0]
216235
assert os.environ.get("password") == key_encoding[0][1]
@@ -221,6 +240,7 @@ def test_adw_context(mock_client, mock_signer, key_encoding):
221240
"password": None,
222241
"service_name": None,
223242
"wallet_location": None,
243+
"dsn": None,
224244
}
225245
assert os.environ.get("user_name") is None
226246
assert os.environ.get("password") is None
@@ -240,13 +260,14 @@ def test_adw_keeper_no_wallet(mock_client, mock_signer, key_encoding):
240260
assert adwsecretkeeper == {
241261
**key_encoding[1],
242262
"wallet_location": None,
263+
"dsn": None,
243264
}
244265

245266

246267
@patch("ads.common.auth.default_signer")
247268
@patch("ads.common.oci_client.OCIClientFactory")
248269
def test_adw_keeper_with_repository(mock_client, mock_signer, key_encoding, tmpdir):
249-
expected = {**key_encoding[1], "wallet_location": key_encoding[3]}
270+
expected = {**key_encoding[1], "wallet_location": key_encoding[3], "dsn": None}
250271
os.makedirs(os.path.join(tmpdir, "testdb"))
251272
with open(os.path.join(tmpdir, "testdb", "config.json"), "w") as conffile:
252273
json.dump(expected, conffile)
@@ -270,6 +291,7 @@ def test_adw_context_namespace(mock_client, mock_signer, key_encoding):
270291
assert adwsecretkeeper == {
271292
**key_encoding[1],
272293
"wallet_location": "/this/is/mywallet.zip",
294+
"dsn": None,
273295
}
274296
assert os.environ.get("myapp.user_name") == key_encoding[0][0]
275297
assert os.environ.get("myapp.password") == key_encoding[0][1]
@@ -280,6 +302,7 @@ def test_adw_context_namespace(mock_client, mock_signer, key_encoding):
280302
"password": None,
281303
"service_name": None,
282304
"wallet_location": None,
305+
"dsn": None,
283306
}
284307
assert os.environ.get("myapp.user_name") is None
285308
assert os.environ.get("myapp.password") is None
@@ -300,6 +323,7 @@ def test_adw_context_noexport(mock_client, mock_signer, key_encoding):
300323
assert adwsecretkeeper == {
301324
**key_encoding[1],
302325
"wallet_location": "/this/is/mywallet.zip",
326+
"dsn": None,
303327
}
304328

305329
assert os.environ.get("user_name") is None
@@ -312,6 +336,7 @@ def test_adw_context_noexport(mock_client, mock_signer, key_encoding):
312336
"password": None,
313337
"service_name": None,
314338
"wallet_location": None,
339+
"dsn": None,
315340
}
316341

317342

@@ -413,6 +438,7 @@ def mock_get_secret_id(
413438
"password": key_encoding_with_wallet.credentials.password,
414439
"service_name": key_encoding_with_wallet.credentials.service_name,
415440
"wallet_location": f"{os.path.join(wallet_dir,'wallet.zip')}",
441+
"dsn": None,
416442
}
417443

418444
# with open(key_encoding_with_wallet[3], "rb") as orgfile:
@@ -449,6 +475,7 @@ def mock_get_secret_id(
449475
"password": key_encoding_with_wallet.credentials.password,
450476
"service_name": key_encoding_with_wallet.credentials.service_name,
451477
"wallet_location": f"{os.path.join(wallet_dir,'wallet.zip')}",
478+
"dsn": None,
452479
}
453480
assert (
454481
os.environ.get("user_name")
@@ -472,6 +499,7 @@ def mock_get_secret_id(
472499
"password": None,
473500
"service_name": None,
474501
"wallet_location": None,
502+
"dsn": None,
475503
}
476504
assert os.environ.get("user_name") is None
477505
assert os.environ.get("password") is None
@@ -508,6 +536,7 @@ def mock_get_secret_id(
508536
"password": key_encoding_with_wallet.credentials.password,
509537
"service_name": key_encoding_with_wallet.credentials.service_name,
510538
"wallet_location": f"{os.path.join(wallet_dir,'wallet.zip')}",
539+
"dsn": None,
511540
}
512541
assert (
513542
os.environ.get("myapp.user_name")
@@ -531,6 +560,7 @@ def mock_get_secret_id(
531560
"password": None,
532561
"service_name": None,
533562
"wallet_location": None,
563+
"dsn": None,
534564
}
535565
assert os.environ.get("myapp.user_name") is None
536566
assert os.environ.get("myapp.password") is None
@@ -565,6 +595,7 @@ def mock_get_secret_id(
565595
"password": key_encoding_with_wallet.credentials.password,
566596
"service_name": key_encoding_with_wallet.credentials.service_name,
567597
"wallet_location": f"{os.path.join(wallet_dir,'wallet.zip')}",
598+
"dsn": None,
568599
}
569600
assert os.environ.get("user_name") is None
570601
assert os.environ.get("password") is None
@@ -576,6 +607,7 @@ def mock_get_secret_id(
576607
"password": None,
577608
"service_name": None,
578609
"wallet_location": None,
610+
"dsn": None,
579611
}
580612

581613

@@ -730,6 +762,7 @@ def mock_get_secret_id(
730762
"password": key_encoding_with_wallet.credentials.password,
731763
"service_name": key_encoding_with_wallet.credentials.service_name,
732764
"wallet_location": f"{os.path.join(wallet_dir,'wallet.zip')}",
765+
"dsn": None,
733766
}
734767
assert (
735768
os.environ.get("user_name")
@@ -753,6 +786,7 @@ def mock_get_secret_id(
753786
"password": None,
754787
"service_name": None,
755788
"wallet_location": None,
789+
"dsn": None,
756790
}
757791
assert os.environ.get("user_name") is None
758792
assert os.environ.get("password") is None
@@ -786,6 +820,7 @@ def mock_get_secret_id(
786820
"password": key_encoding_with_wallet.credentials.password,
787821
"service_name": key_encoding_with_wallet.credentials.service_name,
788822
"wallet_location": f"{os.path.join(wallet_dir,'wallet.zip')}",
823+
"dsn": None,
789824
}
790825
assert (
791826
os.environ.get("user_name")
@@ -809,6 +844,7 @@ def mock_get_secret_id(
809844
"password": None,
810845
"service_name": None,
811846
"wallet_location": None,
847+
"dsn": None,
812848
}
813849
assert os.environ.get("user_name") is None
814850
assert os.environ.get("password") is None

0 commit comments

Comments
 (0)