<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Liam Baker on Medium]]></title>
        <description><![CDATA[Stories by Liam Baker on Medium]]></description>
        <link>https://medium.com/@inanimateobject?source=rss-17e4ef945aa5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dmbNkD5D-u45r44go_cf0g.png</url>
            <title>Stories by Liam Baker on Medium</title>
            <link>https://medium.com/@inanimateobject?source=rss-17e4ef945aa5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 20:04:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@inanimateobject/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Password-less database authentication with Apache Superset and AWS Aurora Serverless V2…]]></title>
            <link>https://medium.com/sage-ai/password-less-database-authentication-with-apache-superset-and-aws-aurora-serverless-v2-9e228eb7424c?source=rss-17e4ef945aa5------2</link>
            <guid isPermaLink="false">https://medium.com/p/9e228eb7424c</guid>
            <category><![CDATA[aurora]]></category>
            <category><![CDATA[identity]]></category>
            <category><![CDATA[mlops]]></category>
            <category><![CDATA[apache-superset]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Liam Baker]]></dc:creator>
            <pubDate>Wed, 29 Nov 2023 14:35:50 GMT</pubDate>
            <atom:updated>2023-11-29T15:04:01.894Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sYmOwKkVEil7dtGoQiPaBQ.png" /></figure><h3>Password-less database authentication with Apache Superset and AWS Aurora Serverless V2 (PostgreSQL)</h3><p>Fewer static passwords, less risk. Migrating applications to Aurora Serverless V2.</p><p><strong>Introduction</strong></p><p>AWS has long supported <a href="https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/UsingWithRDS.IAMDBAuth.html">IAM Database Authentication</a>. This was a great opportunity as part of a larger migration to Aurora Serverless V2 and PostgreSQL 15 being pursued for scaling and feature improvements (namely the <em>pgvector </em>extension), to rethink how we provided storage for use by modern workloads.</p><p><strong>Planning for the migration</strong></p><p>Changing a database authentication pattern is challenging for any application. At Sage AI, all our applications are deployed to Kubernetes via Helm Charts. To ensure backwards compatibility for tenants with services that could not yet migrate to this authentication strategy, an optional feature flag was used in our core application configuration (a custom Helm Chart). Apache Superset being the flexible OSS project that it is, supports the use of password retrieval using a custom function. If the feature flag is enabled the custom function is patched into the Apache Superset (henceforth Superset) configuration file. This disables the legacy method, which required the ExternalSecrets Operator to fetch the password stored in AWS SSM Parameter Store.</p><p>Our cloud infrastructure is managed using Terraform, so when provisioning the database using the PostgreSQL provider, it was necessary to grant an additional, special role named <a href="https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/UsingWithRDS.IAMDBAuth.DBAccounts.html#UsingWithRDS.IAMDBAuth.DBAccounts.PostgreSQL"><em>rds_iam</em></a><strong><em> </em></strong>to the database users within our module. This ensures password authentication is disabled for users granted the role. A benefit of doing this is that it would be quick to revoke the grant in the event the authentication strategy was broken, in a “break glass” scenario.</p><p>To add to the complexity, the major version of the database engine had recently been updated courtesy of the migration to Aurora Serverless V2, so it was critical to ensure recent changes were accounted for in the target version (v15.x) from our current version (v13.9).</p><p><strong>Implementation</strong></p><p><strong>IAM Policies</strong></p><p>AWS provides a special reference for this action as described <a href="https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/UsingWithRDS.IAMDBAuth.IAMPolicy.html">here</a>, <em>rds-db:connect</em>, which allows the specified principal to retrieve temporary tokens in order make connections to the database cluster with the specified database user. Since Terraform is used to configure IAM policies and provision the database clusters, it was necessary to add an additional output to the <em>aurora</em> module maintained by Sage AI which the child module didn’t create for us, shown below:</p><pre>output &quot;cluster_resource_arn&quot; {<br>  description = &quot;The computed ARN of the created cluster, using cluster ID&quot;<br>  value = local.cluster_resource_arn<br>}</pre><pre>locals {<br>  ...<br>  cluster_resource_arn = &quot;arn:aws:rds-db:${var.tf_config.aws_region}:${var.tf_config.aws_account}:dbuser:${module.aurora.cluster_resource_id}&quot;<br>  ...<br>}</pre><p>Included below is an excerpt from the Terraform module that computes the policy statement that the IAM Role for Superset requires.</p><pre>output &quot;statements&quot; {<br>  description = &quot;Computed Aurora RDS Connect policy statements&quot;<br>  value = length(var.accessible_clusters) == 0 ? {} : {<br>    RDSConnectAccess = {<br>      actions = [&quot;rds-db:connect&quot;]<br>      resources = flatten([<br>        for value in var.accessible_clusters : [<br>          format(&quot;%s/%s&quot;,<br>            value.cluster_resource_arn,<br>            value.username,<br>          )<br>        ]<br>      ])<br>    }<br>  }<br>}</pre><p>This produces the following IAM policy statement:</p><pre>{<br>  &quot;Version&quot;: &quot;2012–10–17&quot;,<br>  &quot;Statement&quot;: [<br>    {<br>      &quot;Action&quot;: &quot;rds-db:connect&quot;,<br>      &quot;Effect&quot;: &quot;Allow&quot;,<br>      &quot;Resource&quot;: &quot;arn:aws:rds-db:eu-west-1:000000000000:dbuser:cluster-XXXXXXXXXX/bob&quot;,<br>      &quot;Sid&quot;: &quot;RDSConnectAccess&quot;<br>    }<br>  ]<br>}</pre><p><strong>Custom Password Store Function</strong></p><p>With IAM policies appropriately setup, there needs to be a mechanism to signal to Superset that it needs to perform additional actions when substituting password values into the connection string.</p><p>Superset can be <a href="https://apache-superset.readthedocs.io/en/latest/installation.html#configuration">configured</a> with a Python file, which provides a variable <a href="https://apache-superset.readthedocs.io/en/latest/installation.html#external-password-store-for-sqlalchemy-connections"><em>SQLALCHEMY_CUSTOM_PASSWORD_STORE</em></a> that can be assigned as a regular Python function.</p><p>Using Helm, a feature flag was added to the custom Chart maintained by the Infrastructure team, when specified a small template helper patches the function. This ensured backwards compatibility.</p><p>The body of the function can be seen below (with enclosing templating logic):</p><pre>{{/*<br>RDS IAM Authentication code for custom password store extension<br>*/}}<br>{{- define &quot;superset.rdsIamAuthCustomFunc&quot; -}}<br>import boto3<br>import time<br>from urllib import parse</pre><pre>DATABASE_AUTH_ROLE_SESSION_NAME = &quot;superset-rds-iam-auth-&quot;<br>SAGEAI_ACCOUNTS = {<br>    &quot;development&quot;: &quot;&lt;devAccountID&gt;&quot;,<br>    &quot;pre-production&quot;: &quot;&lt;preProdAccountId&gt;&quot;,<br>    &quot;production&quot;: &quot;&lt;ProdAccountId&gt;&quot;,<br>}</pre><pre>def env_var_password_formatter(url):<br>    if not url.password:<br>        return None</pre><pre>    account = &quot;&quot;<br>    if &quot;DEVELOPMENT&quot; in url.password:<br>        account = SAGEAI_ACCOUNTS.get(&quot;development&quot;)<br>    elif &quot;PRE_PRODUCTION&quot; in url.password:<br>        account = SAGEAI_ACCOUNTS.get(&quot;pre-production&quot;)<br>    else:<br>        account = SAGEAI_ACCOUNTS.get(&quot;production&quot;)</pre><pre>    formatted = url.password.format(**os.environ)</pre><pre>    if &quot;generate&quot; in formatted:<br>        role_name = os.getenv(&quot;DATABASE_AUTH_ROLE_NAME&quot;)<br>        region = os.getenv(&quot;AWS_REGION&quot;)<br>        role_arn = f&quot;arn:aws:iam::{account}:role/{role_name}@{region}&quot;</pre><pre>        SINCE_EPOCH = int(time.time())<br>        role_session_name = f&quot;{DATABASE_AUTH_ROLE_SESSION_NAME}-{SINCE_EPOCH}&quot;<br>        credentials = assume_role(role_arn, role_session_name)</pre><pre>        rds_token = get_db_auth_token(credentials, url, region)<br>        return rds_token</pre><pre>    return formatted<br></pre><pre>def assume_role(role_arn, role_session_name):<br>    session = boto3.Session()<br>    sts_client = boto3.client(&quot;sts&quot;)<br>    response = sts_client.assume_role(<br>        RoleArn=role_arn, RoleSessionName=role_session_name<br>    )</pre><pre>    return response[&quot;Credentials&quot;]<br></pre><pre>def get_db_auth_token(credentials, url, region) -&gt; str:<br>    rds_client = boto3.client(<br>        &quot;rds&quot;,<br>        aws_access_key_id=credentials[&quot;AccessKeyId&quot;],<br>        aws_secret_access_key=credentials[&quot;SecretAccessKey&quot;],<br>        aws_session_token=credentials[&quot;SessionToken&quot;],<br>        region_name=region,<br>    )</pre><pre>    port = os.getenv(&quot;POSTGRES_PORT&quot;, &quot;5432&quot;)</pre><pre>    rds_token = rds_client.generate_db_auth_token(<br>        DBHostname=url.host,<br>        Port=port,<br>        DBUsername=url.username,<br>        Region=region,<br>    )</pre><pre>    return parse.quote_plus(rds_token)<br>{{- end -}}</pre><p><strong>Helm Release Configuration</strong></p><p>Within the <em>HelmRelease</em> object created for the tenant-specific deployment of Superset, there is a special placeholder of “<em>generate”</em> in the connection string where the password would usually be declared. This signals to the patched function above that it should retrieve the token from AWS, instead of the environment. It operates in the “legacy” or normal mode if this keyword is not encountered in the connection string.</p><p><strong>PostgreSQL Role Grant</strong></p><p>When a database cluster is provisioned through Terraform in our cloud environment, PostgreSQL database users are also created along with it. As the documentation <a href="https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/UsingWithRDS.IAMDBAuth.DBAccounts.html#UsingWithRDS.IAMDBAuth.DBAccounts.PostgreSQL">explains,</a> an Aurora PostgreSQL role named <em>rds_iam</em> must be granted to the user wishing to use IAM database authentication, this was achieved by appending this role to the list of roles provided to the <em>postgresql_role </em>resource within the Terraform configuration.</p><p><strong>Gotchas</strong></p><p>When trying to allow a cross-account IAM role to connect to the target database cluster, to reduce the number of IAM roles involved, database connections would fail with a PAM authentication error. It was later clear that an IAM role in the source account would need to assume an IAM role within the same account that the database cluster was located in order for authentication to succeed.</p><p>Assuming a Python setup, the token returned must be sanitised by the function <em>parse.unquote_plus</em> (or a similar function in other languages) otherwise SQL client libraries will incorrectly parse the password portion of the connection URL, again resulting in PAM authentication failures.</p><p><strong>Conclusion</strong></p><p>At a high level we’ve covered how we planned and introduced a new feature to enable a seamless database authentication experience. In detail, we’ve shown that with a combination of a custom python function and some basic helm templating, we can customise Superset to support additional authentication mechanisms. This has effectively reduced the risk associated with static password exposure. After all, data protection remains a critical and top priority.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9e228eb7424c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/sage-ai/password-less-database-authentication-with-apache-superset-and-aws-aurora-serverless-v2-9e228eb7424c">Password-less database authentication with Apache Superset and AWS Aurora Serverless V2…</a> was originally published in <a href="https://medium.com/sage-ai">Sage Ai</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>