1
1
#!/usr/bin/env python
2
2
3
- # Copyright (c) 2021, 2023 Oracle and/or its affiliates.
3
+ # Copyright (c) 2021, 2024 Oracle and/or its affiliates.
4
4
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
5
5
6
6
from ads .secrets import ADBSecretKeeper
@@ -36,6 +36,24 @@ def key_encoding():
36
36
)
37
37
38
38
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
+
39
57
def generate_wallet_data (wallet_zip_path , wallet_dir_path ):
40
58
files = 4
41
59
file_content = {}
@@ -133,9 +151,9 @@ def test_encode(mock_client, mock_signer, key_encoding):
133
151
134
152
@patch ("ads.common.auth.default_signer" )
135
153
@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 ):
137
155
adwsecretkeeper = ADBSecretKeeper (
138
- * key_encoding [ 0 ],
156
+ ** key_encoding_dsn [ 1 ],
139
157
vault_id = "ocid.vault" ,
140
158
key_id = "ocid.key" ,
141
159
compartment_id = "dummy" ,
@@ -211,6 +229,7 @@ def test_adw_context(mock_client, mock_signer, key_encoding):
211
229
assert adwsecretkeeper == {
212
230
** key_encoding [1 ],
213
231
"wallet_location" : "/this/is/mywallet.zip" ,
232
+ "dsn" : None ,
214
233
}
215
234
assert os .environ .get ("user_name" ) == key_encoding [0 ][0 ]
216
235
assert os .environ .get ("password" ) == key_encoding [0 ][1 ]
@@ -221,13 +240,44 @@ def test_adw_context(mock_client, mock_signer, key_encoding):
221
240
"password" : None ,
222
241
"service_name" : None ,
223
242
"wallet_location" : None ,
243
+ "dsn" : None ,
224
244
}
225
245
assert os .environ .get ("user_name" ) is None
226
246
assert os .environ .get ("password" ) is None
227
247
assert os .environ .get ("service_name" ) is None
228
248
assert os .environ .get ("wallet_location" ) is None
229
249
230
250
251
+ @patch ("ads.common.auth.default_signer" )
252
+ @patch ("ads.common.oci_client.OCIClientFactory" )
253
+ def test_adw_context_tls (mock_client , mock_signer , key_encoding_dsn ):
254
+ with mock .patch (
255
+ "ads.vault.Vault.get_secret" , return_value = key_encoding_dsn [2 ]
256
+ ) as mocked_getsecret :
257
+ with ADBSecretKeeper .load_secret (
258
+ source = "ocid.secret.id" ,
259
+ export_env = True ,
260
+ ) as adwsecretkeeper :
261
+ assert adwsecretkeeper == {
262
+ ** key_encoding_dsn [1 ],
263
+ "service_name" : None ,
264
+ "wallet_location" : None ,
265
+ }
266
+ assert os .environ .get ("user_name" ) == key_encoding_dsn [0 ][0 ]
267
+ assert os .environ .get ("password" ) == key_encoding_dsn [0 ][1 ]
268
+ assert os .environ .get ("dsn" ) == key_encoding_dsn [0 ][2 ]
269
+ assert adwsecretkeeper == {
270
+ "user_name" : None ,
271
+ "password" : None ,
272
+ "service_name" : None ,
273
+ "wallet_location" : None ,
274
+ "dsn" : None ,
275
+ }
276
+ assert os .environ .get ("user_name" ) is None
277
+ assert os .environ .get ("password" ) is None
278
+ assert os .environ .get ("dsn" ) is None
279
+
280
+
231
281
@patch ("ads.common.auth.default_signer" )
232
282
@patch ("ads.common.oci_client.OCIClientFactory" )
233
283
def test_adw_keeper_no_wallet (mock_client , mock_signer , key_encoding ):
@@ -240,13 +290,14 @@ def test_adw_keeper_no_wallet(mock_client, mock_signer, key_encoding):
240
290
assert adwsecretkeeper == {
241
291
** key_encoding [1 ],
242
292
"wallet_location" : None ,
293
+ "dsn" : None ,
243
294
}
244
295
245
296
246
297
@patch ("ads.common.auth.default_signer" )
247
298
@patch ("ads.common.oci_client.OCIClientFactory" )
248
299
def test_adw_keeper_with_repository (mock_client , mock_signer , key_encoding , tmpdir ):
249
- expected = {** key_encoding [1 ], "wallet_location" : key_encoding [3 ]}
300
+ expected = {** key_encoding [1 ], "wallet_location" : key_encoding [3 ], "dsn" : None }
250
301
os .makedirs (os .path .join (tmpdir , "testdb" ))
251
302
with open (os .path .join (tmpdir , "testdb" , "config.json" ), "w" ) as conffile :
252
303
json .dump (expected , conffile )
@@ -270,6 +321,7 @@ def test_adw_context_namespace(mock_client, mock_signer, key_encoding):
270
321
assert adwsecretkeeper == {
271
322
** key_encoding [1 ],
272
323
"wallet_location" : "/this/is/mywallet.zip" ,
324
+ "dsn" : None ,
273
325
}
274
326
assert os .environ .get ("myapp.user_name" ) == key_encoding [0 ][0 ]
275
327
assert os .environ .get ("myapp.password" ) == key_encoding [0 ][1 ]
@@ -280,6 +332,7 @@ def test_adw_context_namespace(mock_client, mock_signer, key_encoding):
280
332
"password" : None ,
281
333
"service_name" : None ,
282
334
"wallet_location" : None ,
335
+ "dsn" : None ,
283
336
}
284
337
assert os .environ .get ("myapp.user_name" ) is None
285
338
assert os .environ .get ("myapp.password" ) is None
@@ -300,6 +353,7 @@ def test_adw_context_noexport(mock_client, mock_signer, key_encoding):
300
353
assert adwsecretkeeper == {
301
354
** key_encoding [1 ],
302
355
"wallet_location" : "/this/is/mywallet.zip" ,
356
+ "dsn" : None ,
303
357
}
304
358
305
359
assert os .environ .get ("user_name" ) is None
@@ -312,6 +366,7 @@ def test_adw_context_noexport(mock_client, mock_signer, key_encoding):
312
366
"password" : None ,
313
367
"service_name" : None ,
314
368
"wallet_location" : None ,
369
+ "dsn" : None ,
315
370
}
316
371
317
372
@@ -413,6 +468,7 @@ def mock_get_secret_id(
413
468
"password" : key_encoding_with_wallet .credentials .password ,
414
469
"service_name" : key_encoding_with_wallet .credentials .service_name ,
415
470
"wallet_location" : f"{ os .path .join (wallet_dir ,'wallet.zip' )} " ,
471
+ "dsn" : None ,
416
472
}
417
473
418
474
# with open(key_encoding_with_wallet[3], "rb") as orgfile:
@@ -449,6 +505,7 @@ def mock_get_secret_id(
449
505
"password" : key_encoding_with_wallet .credentials .password ,
450
506
"service_name" : key_encoding_with_wallet .credentials .service_name ,
451
507
"wallet_location" : f"{ os .path .join (wallet_dir ,'wallet.zip' )} " ,
508
+ "dsn" : None ,
452
509
}
453
510
assert (
454
511
os .environ .get ("user_name" )
@@ -472,6 +529,7 @@ def mock_get_secret_id(
472
529
"password" : None ,
473
530
"service_name" : None ,
474
531
"wallet_location" : None ,
532
+ "dsn" : None ,
475
533
}
476
534
assert os .environ .get ("user_name" ) is None
477
535
assert os .environ .get ("password" ) is None
@@ -508,6 +566,7 @@ def mock_get_secret_id(
508
566
"password" : key_encoding_with_wallet .credentials .password ,
509
567
"service_name" : key_encoding_with_wallet .credentials .service_name ,
510
568
"wallet_location" : f"{ os .path .join (wallet_dir ,'wallet.zip' )} " ,
569
+ "dsn" : None ,
511
570
}
512
571
assert (
513
572
os .environ .get ("myapp.user_name" )
@@ -531,6 +590,7 @@ def mock_get_secret_id(
531
590
"password" : None ,
532
591
"service_name" : None ,
533
592
"wallet_location" : None ,
593
+ "dsn" : None ,
534
594
}
535
595
assert os .environ .get ("myapp.user_name" ) is None
536
596
assert os .environ .get ("myapp.password" ) is None
@@ -565,6 +625,7 @@ def mock_get_secret_id(
565
625
"password" : key_encoding_with_wallet .credentials .password ,
566
626
"service_name" : key_encoding_with_wallet .credentials .service_name ,
567
627
"wallet_location" : f"{ os .path .join (wallet_dir ,'wallet.zip' )} " ,
628
+ "dsn" : None ,
568
629
}
569
630
assert os .environ .get ("user_name" ) is None
570
631
assert os .environ .get ("password" ) is None
@@ -576,6 +637,7 @@ def mock_get_secret_id(
576
637
"password" : None ,
577
638
"service_name" : None ,
578
639
"wallet_location" : None ,
640
+ "dsn" : None ,
579
641
}
580
642
581
643
@@ -730,6 +792,7 @@ def mock_get_secret_id(
730
792
"password" : key_encoding_with_wallet .credentials .password ,
731
793
"service_name" : key_encoding_with_wallet .credentials .service_name ,
732
794
"wallet_location" : f"{ os .path .join (wallet_dir ,'wallet.zip' )} " ,
795
+ "dsn" : None ,
733
796
}
734
797
assert (
735
798
os .environ .get ("user_name" )
@@ -753,6 +816,7 @@ def mock_get_secret_id(
753
816
"password" : None ,
754
817
"service_name" : None ,
755
818
"wallet_location" : None ,
819
+ "dsn" : None ,
756
820
}
757
821
assert os .environ .get ("user_name" ) is None
758
822
assert os .environ .get ("password" ) is None
@@ -786,6 +850,7 @@ def mock_get_secret_id(
786
850
"password" : key_encoding_with_wallet .credentials .password ,
787
851
"service_name" : key_encoding_with_wallet .credentials .service_name ,
788
852
"wallet_location" : f"{ os .path .join (wallet_dir ,'wallet.zip' )} " ,
853
+ "dsn" : None ,
789
854
}
790
855
assert (
791
856
os .environ .get ("user_name" )
@@ -809,6 +874,7 @@ def mock_get_secret_id(
809
874
"password" : None ,
810
875
"service_name" : None ,
811
876
"wallet_location" : None ,
877
+ "dsn" : None ,
812
878
}
813
879
assert os .environ .get ("user_name" ) is None
814
880
assert os .environ .get ("password" ) is None
0 commit comments