Skip to content

Commit 5f8bedb

Browse files
Merge pull request #6 from IBM/patch-1
Updated code to incorporate ssl authentication with Db2
2 parents 5440a37 + a4c0631 commit 5f8bedb

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

app.py

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import requests
55
from ibm_watson import AssistantV2
66
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
7+
import base64
78
try:
89
import ibm_db
910
except:
@@ -22,25 +23,36 @@
2223
with open('ibm-db2-credentials.json', 'r') as credentialsFile:
2324
credentials1 = json.loads(credentialsFile.read())
2425

25-
dsn_driver = "IBM DB2 ODBC DRIVER"
26-
dsn_database = credentials1['db']
27-
dsn_hostname = credentials1['host']
28-
dsn_port = "50000"
29-
dsn_uid = credentials1['username']
30-
dsn_pwd = credentials1['password']
31-
32-
dsn = (
33-
"DRIVER={{IBM DB2 ODBC DRIVER}};"
34-
"DATABASE={0};"
35-
"HOSTNAME={1};"
36-
"PORT={2};"
37-
"PROTOCOL=TCPIP;"
38-
"UID={3};"
39-
"PWD={4};").format(dsn_database, dsn_hostname, dsn_port, dsn_uid, dsn_pwd)
4026
try:
41-
conn = ibm_db.connect(dsn, "", "")
27+
dsn_database = credentials1['connection']['db2']['database']
28+
dsn_hostname = credentials1['connection']['db2']['hosts'][0]['hostname']
29+
dsn_port = credentials1['connection']['db2']['hosts'][0]['port']
30+
dsn_uid = credentials1['connection']['db2']['authentication']['username']
31+
dsn_pwd = credentials1['connection']['db2']['authentication']['password']
32+
33+
certificate = credentials1['connection']['db2']['certificate']['certificate_base64']
34+
ssl_certificate_bytes = base64.b64decode(certificate)
35+
ssl_certificate = ssl_certificate_bytes.decode('ascii')
4236
except:
43-
pass
37+
print('No DB2 credentials found')
38+
39+
db2_conn_cert = os.path.join(os.path.expanduser('~'),'ibm-db2-ssl.cert')
40+
with open(db2_conn_cert, "w") as f:
41+
f.write(ssl_certificate)
42+
43+
dsn = 'DATABASE={db};HOSTNAME={host};PORT={port};PROTOCOL=TCPIP;UID={uid};PWD={pwd};SECURITY=SSL;SSLServerCertificate={cert}'.format(
44+
db=dsn_database,
45+
host=dsn_hostname,
46+
port=dsn_port,
47+
uid=dsn_uid,
48+
pwd=dsn_pwd,
49+
cert=db2_conn_cert
50+
)
51+
52+
try:
53+
conn = ibm_db.connect(dsn, "", "")
54+
except Exception as e:
55+
print(e)
4456

4557
#########################
4658
# Create Orders Table
@@ -52,8 +64,8 @@
5264
ADDRESS varchar(255) ); '
5365
try:
5466
ibm_db.exec_immediate(conn, table)
55-
except:
56-
pass
67+
except Exception as e:
68+
print("Create Table error-> ",e)
5769

5870

5971
with open('watson-assistant-credentials.json', 'r') as credentialsFile:
@@ -93,7 +105,7 @@ def destroySession():
93105
assistant_id=assistantid, session_id=sessionid).get_result()
94106
print(response)
95107
except Exception as e:
96-
pass
108+
print(e)
97109

98110
# assistant.delete_session(skillid, "<YOUR SESSION ID>").get_result()
99111

@@ -234,3 +246,5 @@ def index():
234246
if __name__ == "__main__":
235247
app.secret_key = os.urandom(12)
236248
app.run(debug=True, host='0.0.0.0', port=port)
249+
250+

0 commit comments

Comments
 (0)