-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create SQL Alchemy connection string using Okta IdP #67
Comments
Hey @nam-tonthat-afterpay , Below is an example showing how to establish a connection to Redshift using Okta Browser authentication. Please note that the import sqlalchemy as sa
from sqlalchemy.engine.url import URL
url = URL.create(
drivername='redshift+redshift_connector', # indicate redshift_connector driver and dialect will be used
)
conn_params = {
"ssl": True,
"iam": True, # must be enabled when authenticating via IAM
"credentials_provider": "BrowserSamlCredentialsProvider",
"database": '<db_name>', # Amazon Redshift database
"region": "ap-southeast-2",
"cluster_identifier": "<cluster_identifer>",
"login_url": "<saml_url>"
}
engine = sa.create_engine(url, connect_args=conn_params)
with engine.connect() as connection:
result = connection.execute("select 1")
for row in result:
print(row)
engine.dispose() Please let me know if you see any issue or have any questions. |
Hey @Brooke-white, I appreciate that! That works running it by itself but once integrated into Amundsen where the
Uses the More context, I've used the following versions:
Any idea on how I should start debugging this issue? |
Hi @nam-tonthat-afterpay , Thanks for providing this additional information. I'd recommend to try the following:
The error provided is being thrown after the authentication process w/ Okta has completed (i.e. we have temporary IAM credentials), when redshift_connector tries to retrieve the In my experience, this exception occurs when authentication has been successful but there is some mis-match between If you still see issue after trying the above, please let me know and I will work to reproduce on my end. |
Hey @Brooke-white, do you have any tutorials on how to enable debug logging for I've tried using the Python tutorial but haven't had much luck. |
Hey @nam-tonthat-afterpay , Apologies for the late response. Here's an example of configuring debug logging to output to This would likely be easier that outputting the logs to a file given your Amundsen instance is probably running in a docker container, and a log file would show up there rather than your local file system :) import redshift_connector
import logging
import sys
root = logging.getLogger()
root.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG) # will write all logs with a level >= DEBUG
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
root.addHandler(handler)
with redshift_connector.connect(
...
) as conn:
with conn.cursor() as cursor:
cursor.execute("select 1")
print(cursor.fetchall()) For me, this outputs: 2021-11-19 09:31:02,869 - redshift_connector - DEBUG - ===================================
2021-11-19 09:31:02,869 - redshift_connector - DEBUG - User provided connection arguments
2021-11-19 09:31:02,869 - redshift_connector - DEBUG - ===================================
2021-11-19 09:31:02,869 - redshift_connector - DEBUG - {{'access_key_id': None, 'allow_db_user_override': False, 'app_id': None, 'app_name': 'amazon_aws_redshift',....} |
Hey @Brooke-white, thanks for the heads up; I've done some more testing with this and the
Any ideas for this error? |
Hey @nam-tonthat-afterpay , Could you verify that IAM SAML federation role was created inline with the instructions here:
If the role does not provide permissions to |
Hi @Brooke-white , On a simple query like this, it returns the import redshift_connector
import logging
import sys
root = logging.getLogger()
root.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG) # will write all logs with a level >= DEBUG
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
root.addHandler(handler)
import sqlalchemy as sa
from sqlalchemy.engine.url import URL
url = URL.create(
drivername='redshift+redshift_connector', # indicate redshift_connector driver and dialect will be used
)
conn_params = {
"ssl": True,
"iam": True, # must be enabled when authenticating via IAM
"credentials_provider": "BrowserSamlCredentialsProvider",
"database": 'vega', # Amazon Redshift database
"region": "ap-southeast-2",
"cluster_identifier": "dwh",
"login_url": "<okta_login>"
}
engine = sa.create_engine(url, connect_args=conn_params)
with engine.connect() as connection:
result = connection.execute("select 1")
for row in result:
print(row)
engine.dispose() However, repeating it for the amundsen query (adjusting the Any ideas on what could be the issue? |
Hi @nam-tonthat-afterpay, Can you confirm if the Python script which only uses redshift_connector is now consistently connecting?
When this behavior occurs, what environment(s) are used to run the Python script (e.g. Jupyter)?
Could you share the modified |
Hi @nam-tonthat-afterpay , Following up to see if you're still experiencing issues here. |
Hi @Brooke-white , yes I am - I'm just a little tied up with some tickets that I have to close off - will update this thread by tomorrow! Apologies for the wait :'(. |
Hi @Brooke-white, did a fresh install this morning and tried. Looks like everything is working perfectly. Closing the thread. The debug logging helped me iron out any issues. Thank you again for your help and patience on this! |
yay! I'm happy to hear this, @nam-tonthat-afterpay. Best wishes on your project :) |
Driver version
redshift_connector 2.0.889
Redshift version
PostgreSQL 8.0.2 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 3.4.2 20041017 (Red Hat 3.4.2-6.fc3), Redshift 1.0.31651
Client Operating System
mac OS Monterery
Python version
3.9.7
Problem description
Currently, I can connect to Redshift using the above credentials but when trying to utilise the SQL Alchemy connection string - it causes an identification error (using
sqlalchemy-redshift
). Is there a way to generate an appropriate URL forsqlalchemy
to parse for IdP authentication?I've noticed that Redshift was recently updated to work with
sqlalchemy-redshift
but have had no luck generating an appropriate URL login.The text was updated successfully, but these errors were encountered: