Skip to content

Commit 8bc81a8

Browse files
committed
added vault support
1 parent 69222e8 commit 8bc81a8

File tree

4 files changed

+48
-10
lines changed

4 files changed

+48
-10
lines changed

ads/opctl/operator/common/operator_config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class InputData(DataClassSerializable):
2828
limit: int = None
2929
sql: str = None
3030
table_name: str = None
31+
vault_secret_id: str = None
3132

3233

3334
@dataclass(repr=True)

ads/opctl/operator/lowcode/anomaly/schema.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ spec:
7878
limit:
7979
required: false
8080
type: integer
81+
vault_secret_id:
82+
required: false
83+
type: string
8184

8285
validation_data:
8386
required: false
@@ -130,6 +133,9 @@ spec:
130133
limit:
131134
required: false
132135
type: integer
136+
vault_secret_id:
137+
required: false
138+
type: string
133139

134140
datetime_column:
135141
type: dict

ads/opctl/operator/lowcode/common/utils.py

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import argparse
88
import logging
99
import os
10+
import shutil
1011
import sys
12+
import tempfile
1113
import time
1214
from string import Template
1315
from typing import Any, Dict, List, Tuple
@@ -28,6 +30,7 @@
2830
)
2931
from ads.opctl.operator.common.operator_config import OutputDirectory
3032
from ads.common.object_storage_details import ObjectStorageDetails
33+
from ads.secrets import ADBSecretKeeper
3134

3235

3336
def call_pandas_fsspec(pd_fn, filename, storage_options, **kwargs):
@@ -53,10 +56,12 @@ def load_data(data_spec, storage_options=None, **kwargs):
5356
sql = data_spec.sql
5457
table_name = data_spec.table_name
5558
limit = data_spec.limit
56-
59+
vault_secret_id = data_spec.vault_secret_id
5760
storage_options = storage_options or (
5861
default_signer() if ObjectStorageDetails.is_oci_path(filename) else {}
5962
)
63+
if vault_secret_id is not None and connect_args is None:
64+
connect_args = dict()
6065

6166
if filename is not None:
6267
if not format:
@@ -76,15 +81,32 @@ def load_data(data_spec, storage_options=None, **kwargs):
7681
f"The format {format} is not currently supported for reading data. Please reformat the data source: {filename} ."
7782
)
7883
elif connect_args is not None:
79-
con = oracledb.connect(**connect_args)
80-
if table_name is not None:
81-
data = pd.read_sql_table(table_name, con)
82-
elif sql is not None:
83-
data = pd.read_sql(sql, con)
84-
else:
85-
raise InvalidParameterError(
86-
f"Database `connect_args` provided without sql query or table name. Please specify either `sql` or `table_name`."
87-
)
84+
with tempfile.TemporaryDirectory() as temp_dir:
85+
if vault_secret_id is not None:
86+
try:
87+
with ADBSecretKeeper.load_secret(vault_secret_id, wallet_dir=temp_dir) as adwsecret:
88+
if 'wallet_location' in adwsecret and 'wallet_location' not in connect_args:
89+
shutil.unpack_archive(adwsecret["wallet_location"], temp_dir)
90+
connect_args['wallet_location'] = temp_dir
91+
if 'user_name' in adwsecret and 'user' not in connect_args:
92+
connect_args['user'] = adwsecret['user_name']
93+
if 'password' in adwsecret and 'password' not in connect_args:
94+
connect_args['password'] = adwsecret['password']
95+
if 'service_name' in adwsecret and 'service_name' not in connect_args:
96+
connect_args['service_name'] = adwsecret['service_name']
97+
98+
except Exception as e:
99+
logger.debug(f"Could not retrieve database credentials from vault : {e}")
100+
101+
con = oracledb.connect(**connect_args)
102+
if table_name is not None:
103+
data = pd.read_sql(f"SELECT * FROM {table_name}", con)
104+
elif sql is not None:
105+
data = pd.read_sql(sql, con)
106+
else:
107+
raise InvalidParameterError(
108+
f"Database `connect_args` provided without sql query or table name. Please specify either `sql` or `table_name`."
109+
)
88110
else:
89111
raise InvalidParameterError(
90112
f"No filename/url provided, and no connect_args provided. Please specify one of these if you want to read data from a file or a database respectively."

ads/opctl/operator/lowcode/forecast/schema.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ spec:
7878
limit:
7979
required: false
8080
type: integer
81+
vault_secret_id:
82+
required: false
83+
type: string
8184

8285
additional_data:
8386
required: false
@@ -130,6 +133,9 @@ spec:
130133
limit:
131134
required: false
132135
type: integer
136+
vault_secret_id:
137+
required: false
138+
type: string
133139

134140
test_data:
135141
required: false
@@ -181,6 +187,9 @@ spec:
181187
limit:
182188
required: false
183189
type: integer
190+
vault_secret_id:
191+
required: false
192+
type: string
184193
type: dict
185194

186195
output_directory:

0 commit comments

Comments
 (0)