Skip to content

Commit

Permalink
Merge pull request #6 from IBM/patch-1
Browse files Browse the repository at this point in the history
Updated code to incorporate ssl authentication with Db2
  • Loading branch information
manojjahgirdar committed Jan 18, 2022
2 parents 5440a37 + a4c0631 commit 5f8bedb
Showing 1 changed file with 34 additions and 20 deletions.
54 changes: 34 additions & 20 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import requests
from ibm_watson import AssistantV2
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
import base64
try:
import ibm_db
except:
Expand All @@ -22,25 +23,36 @@
with open('ibm-db2-credentials.json', 'r') as credentialsFile:
credentials1 = json.loads(credentialsFile.read())

dsn_driver = "IBM DB2 ODBC DRIVER"
dsn_database = credentials1['db']
dsn_hostname = credentials1['host']
dsn_port = "50000"
dsn_uid = credentials1['username']
dsn_pwd = credentials1['password']

dsn = (
"DRIVER={{IBM DB2 ODBC DRIVER}};"
"DATABASE={0};"
"HOSTNAME={1};"
"PORT={2};"
"PROTOCOL=TCPIP;"
"UID={3};"
"PWD={4};").format(dsn_database, dsn_hostname, dsn_port, dsn_uid, dsn_pwd)
try:
conn = ibm_db.connect(dsn, "", "")
dsn_database = credentials1['connection']['db2']['database']
dsn_hostname = credentials1['connection']['db2']['hosts'][0]['hostname']
dsn_port = credentials1['connection']['db2']['hosts'][0]['port']
dsn_uid = credentials1['connection']['db2']['authentication']['username']
dsn_pwd = credentials1['connection']['db2']['authentication']['password']

certificate = credentials1['connection']['db2']['certificate']['certificate_base64']
ssl_certificate_bytes = base64.b64decode(certificate)
ssl_certificate = ssl_certificate_bytes.decode('ascii')
except:
pass
print('No DB2 credentials found')

db2_conn_cert = os.path.join(os.path.expanduser('~'),'ibm-db2-ssl.cert')
with open(db2_conn_cert, "w") as f:
f.write(ssl_certificate)

dsn = 'DATABASE={db};HOSTNAME={host};PORT={port};PROTOCOL=TCPIP;UID={uid};PWD={pwd};SECURITY=SSL;SSLServerCertificate={cert}'.format(
db=dsn_database,
host=dsn_hostname,
port=dsn_port,
uid=dsn_uid,
pwd=dsn_pwd,
cert=db2_conn_cert
)

try:
conn = ibm_db.connect(dsn, "", "")
except Exception as e:
print(e)

#########################
# Create Orders Table
Expand All @@ -52,8 +64,8 @@
ADDRESS varchar(255) ); '
try:
ibm_db.exec_immediate(conn, table)
except:
pass
except Exception as e:
print("Create Table error-> ",e)


with open('watson-assistant-credentials.json', 'r') as credentialsFile:
Expand Down Expand Up @@ -93,7 +105,7 @@ def destroySession():
assistant_id=assistantid, session_id=sessionid).get_result()
print(response)
except Exception as e:
pass
print(e)

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

Expand Down Expand Up @@ -234,3 +246,5 @@ def index():
if __name__ == "__main__":
app.secret_key = os.urandom(12)
app.run(debug=True, host='0.0.0.0', port=port)


0 comments on commit 5f8bedb

Please sign in to comment.