Skip to content

Role-Based Access

Overview

At checkout, the broker creates a temporary MySQL user derived from the requesting user’s email and grants a specific privilege (e.g. SELECT) on a target table. At checkin, the privilege is revoked and the user is dropped. Admin credentials are retrieved from AWS Secrets Manager at runtime.

Use this pattern for scoped, least-privilege access — for example, a data analyst who needs read access to a specific table for a limited time.

Before You Begin

  • The broker is running and connected (see Getting Started)
  • mysql client, aws CLI, and jq are installed on the broker host
  • The broker host has an IAM role with secretsmanager:GetSecretValue permission
  • The broker service account (britive_svc) has been created in Aurora MySQL with the required permissions

Checkout Routine

Full script: Aurora MySQL/permissions/role-member/

Environment variables:

VariableDefaultNotes
userInjected — requesting user’s email; username is derived by stripping domain and non-alphanumeric chars
hostAurora cluster endpoint
dburlSame as host — the cluster endpoint
secretAWS Secrets Manager secret name or ARN containing admin credentials
tableTarget table in database.table format (e.g. myapp.orders)
roleSELECTMySQL privilege to grant (e.g. SELECT, INSERT, SELECT,INSERT)

Retrieve credentials and create user:

# Derive MySQL username from email
DB_USER=$(echo "$user" | cut -d'@' -f1 | tr -cd '[:alnum:]')

# Retrieve admin credentials from Secrets Manager
SECRET_JSON=$(aws secretsmanager get-secret-value --secret-id "$secret" --query SecretString --output text)
ADMIN_USER=$(echo "$SECRET_JSON" | jq -r '.username')
ADMIN_PASS=$(echo "$SECRET_JSON" | jq -r '.password')

# Generate a random password for the temp user
DB_PASSWORD=$(openssl rand -base64 16 | tr -d '=/+')

# Create temp user and grant privilege on the target table
mysql -h "$dburl" -u "$ADMIN_USER" -p"$ADMIN_PASS" <<EOF
CREATE USER IF NOT EXISTS '${DB_USER}'@'%' IDENTIFIED BY '${DB_PASSWORD}';
GRANT ${role} ON ${table} TO '${DB_USER}'@'%';
FLUSH PRIVILEGES;
EOF

echo "username=${DB_USER}"
echo "password=${DB_PASSWORD}"
echo "host=${host}"

Checkin Routine

Full script: Aurora MySQL/permissions/role-member/

Revoke and drop the user:

DB_USER=$(echo "$user" | cut -d'@' -f1 | tr -cd '[:alnum:]')

SECRET_JSON=$(aws secretsmanager get-secret-value --secret-id "$secret" --query SecretString --output text)
ADMIN_USER=$(echo "$SECRET_JSON" | jq -r '.username')
ADMIN_PASS=$(echo "$SECRET_JSON" | jq -r '.password')

mysql -h "$dburl" -u "$ADMIN_USER" -p"$ADMIN_PASS" <<EOF
REVOKE ALL PRIVILEGES ON ${table} FROM '${DB_USER}'@'%';
DROP USER IF EXISTS '${DB_USER}'@'%';
FLUSH PRIVILEGES;
EOF

echo "User ${DB_USER} revoked and dropped."

DROP USER IF EXISTS makes checkin idempotent — if the user was already removed (e.g. manual cleanup), checkin completes without error.


Configure in Britive

Create a response template

Go to Resource Manager → Response Templates → New Template. Add fields for username, password, and host from the checkout output.

Create a permission

Go to Resource Manager → Resource Type Permissions → New Permission. Set Language to Shell.

Paste the checkout and checkin routines into the respective fields.

Declare variables:

VariableSystem definedNotes
userYesInjected automatically
hostNoSet per resource
dburlNoSet per resource
secretNoSet per resource
tableNoSet on permission or profile
roleNoe.g. SELECT

Under Response Templates, attach the template you created.

Create a profile

Go to Resource Manager → Profiles → New Profile. Set an expiration (e.g. 4h). Under Associations, select the resource label(s) for the Aurora clusters. Under Permissions, add the permission above.

Add a policy

Under Policies, assign members and configure approval conditions.


Verify

Check out the profile

Navigate to My Access → find the profile → Check Out. The MySQL host, username, and password appear in the checkout response.

Connect and confirm access

mysql -h <returned-host> -u <returned-username> -p<returned-password>
-- Confirm the user and privilege
SELECT current_user();
SHOW GRANTS;
-- Expected: GRANT SELECT ON myapp.orders TO 'username'@'%'

Check in

Return to My AccessCheck In.

Confirm access is revoked

mysql -h <host> -u <returned-username> -p<returned-password>
# Expected: ERROR 1045 (28000): Access denied

Troubleshoot

SymptomCauseFix
ERROR 1045: Access denied during checkoutService account lacks GRANT OPTIONGrant WITH GRANT OPTION to britive_svc on the managed table
ERROR 1142: command denied on GRANTWrong privilege nameUse uppercase MySQL privilege names: SELECT, INSERT, UPDATE
Unable to locate credentialsBroker IAM role missing Secrets Manager permissionAttach secretsmanager:GetSecretValue to the broker instance profile
User still exists after checkinCheckin routine failedCheck broker logs; run DROP USER IF EXISTS 'username'@'%'; manually
table variable not resolvedVariable not declared on the permissionAdd table to the permission variable list
Last updated on