Temporary DBA Access
Overview
At checkout, the broker generates a random password, creates a SQL Server login in master, creates a corresponding database user in the target database, and assigns the db_owner role. At checkin, all active sessions for that login are terminated and the login is dropped from master.
Scripts are available in both Bash (Linux broker with sqlcmd) and PowerShell (Windows broker).
Before You Begin
- The broker is running and connected (see Getting Started)
sqlcmdis installed on the broker host- The broker service account (
britive_svc) hasALTER ANY LOGIN,VIEW SERVER STATE, andALTER ANY CONNECTIONon the SQL Server instance anddb_ownerin the target database
Checkout Routine
Full scripts: Microsoft SQL Server/permissions/Temp DBA/
Environment variables:
| Variable | Notes |
|---|---|
EMAIL | Injected — requesting user’s email; login name is derived from the local part |
SERVER_NAME | SQL Server hostname or IP — set as a resource parameter |
DATABASE_NAME | Target database — set as a resource parameter |
ADMIN_USER | Broker service account login name |
ADMIN_PASSWORD | Broker service account password — reference from Britive Secrets Store |
Generate password and create login:
# Derive login name from email (strip domain, sanitize)
LOGIN_NAME=$(echo "$EMAIL" | cut -d'@' -f1 | tr -cd '[:alnum:]_')
# Generate a random password meeting SQL Server complexity requirements
PASSWORD=$(openssl rand -base64 18 | tr -d '=/+')
# Create login in master and user in target database with db_owner role
/opt/mssql-tools18/bin/sqlcmd -S "$SERVER_NAME" \
-U "$ADMIN_USER" -P "$ADMIN_PASSWORD" \
-C -Q "
CREATE LOGIN [${LOGIN_NAME}] WITH PASSWORD = '${PASSWORD}', CHECK_POLICY = OFF;
USE [${DATABASE_NAME}];
CREATE USER [${LOGIN_NAME}] FOR LOGIN [${LOGIN_NAME}];
ALTER ROLE db_owner ADD MEMBER [${LOGIN_NAME}];
"
echo "LOGIN_NAME=${LOGIN_NAME}"
echo "PASSWORD=${PASSWORD}"
echo "SERVER=${SERVER_NAME}"
echo "DATABASE=${DATABASE_NAME}"Checkin Routine
Full scripts: Microsoft SQL Server/permissions/Temp DBA/
Kill sessions and drop the login:
LOGIN_NAME=$(echo "$EMAIL" | cut -d'@' -f1 | tr -cd '[:alnum:]_')
/opt/mssql-tools18/bin/sqlcmd -S "$SERVER_NAME" \
-U "$ADMIN_USER" -P "$ADMIN_PASSWORD" \
-C -Q "
-- Kill all active sessions for this login
DECLARE @spid INT;
DECLARE session_cursor CURSOR FOR
SELECT session_id FROM sys.dm_exec_sessions
WHERE login_name = '${LOGIN_NAME}';
OPEN session_cursor;
FETCH NEXT FROM session_cursor INTO @spid;
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC ('KILL ' + @spid);
FETCH NEXT FROM session_cursor INTO @spid;
END;
CLOSE session_cursor;
DEALLOCATE session_cursor;
-- Drop the database user and server login
USE [${DATABASE_NAME}];
IF EXISTS (SELECT 1 FROM sys.database_principals WHERE name = '${LOGIN_NAME}')
DROP USER [${LOGIN_NAME}];
USE [master];
IF EXISTS (SELECT 1 FROM sys.server_principals WHERE name = '${LOGIN_NAME}')
DROP LOGIN [${LOGIN_NAME}];
"
echo "Login ${LOGIN_NAME} and all active sessions removed."The session kill loop uses sys.dm_exec_sessions — the broker service account needs VIEW SERVER STATE to query this view. Killing all sessions ensures no lingering connections survive after checkin.
PowerShell Variant
For Windows broker hosts, a PowerShell variant is available in the same directory. It uses Invoke-Sqlcmd from the SqlServer module instead of sqlcmd.
Full script: Microsoft SQL Server/permissions/Temp DBA/temp_dba_checkout.ps1
$LoginName = ($env:EMAIL -split "@")[0] -replace '[^a-zA-Z0-9_]', ''
$Password = [System.Web.Security.Membership]::GeneratePassword(16, 2)
Invoke-Sqlcmd -ServerInstance $env:SERVER_NAME `
-Username $env:ADMIN_USER -Password $env:ADMIN_PASSWORD `
-TrustServerCertificate -Query "
CREATE LOGIN [$LoginName] WITH PASSWORD = '$Password', CHECK_POLICY = OFF;
USE [$($env:DATABASE_NAME)];
CREATE USER [$LoginName] FOR LOGIN [$LoginName];
ALTER ROLE db_owner ADD MEMBER [$LoginName];"
Write-Output "LOGIN_NAME=$LoginName"
Write-Output "PASSWORD=$Password"Configure in Britive
Create a response template
Go to Resource Manager → Response Templates → New Template. Add fields for LOGIN_NAME, PASSWORD, SERVER, and DATABASE from the checkout output.
Create a permission
Go to Resource Manager → Resource Type Permissions → New Permission. Set Language to Shell (Bash variant) or PowerShell (Windows variant).
Paste the checkout and checkin routines. Declare variables:
| Variable | System defined | Notes |
|---|---|---|
EMAIL | Yes | Injected automatically |
SERVER_NAME | No | Set per resource |
DATABASE_NAME | No | Set per resource |
ADMIN_USER | No | Broker service account name |
ADMIN_PASSWORD | No | Reference from Britive Secrets Store |
Under Response Templates, attach the template you created.
Create a profile
Go to Resource Manager → Profiles → New Profile. Set a short expiration (e.g. 2h). Under Associations, select the resource label(s). Under Permissions, add the permission above.
Add a policy with approval
Under Policies, assign members. Add an approval condition — db_owner is a highly privileged role and should require DBA team approval.
Verify
Check out the profile
Navigate to My Access → find the profile → Check Out. Server, database, login name, and password appear in the response.
Connect to SQL Server
# Linux
sqlcmd -S <server> -U <login-name> -P <password> -d <database> -C -Q "SELECT current_user, db_name()"# Windows
sqlcmd -S <server> -U <login-name> -P <password> -d <database> -Q "SELECT current_user, db_name()"Confirm db_owner role
SELECT dp.name AS principal, r.name AS role
FROM sys.database_role_members drm
JOIN sys.database_principals dp ON dp.principal_id = drm.member_principal_id
JOIN sys.database_principals r ON r.principal_id = drm.role_principal_id
WHERE dp.name = '<login-name>';
-- Expected row: <login-name> | db_ownerCheck in
Return to My Access → Check In.
Confirm access is revoked
sqlcmd -S <server> -U <login-name> -P <password> -C -Q "SELECT 1"
# Expected: Login failed for user '<login-name>'Troubleshoot
| Symptom | Cause | Fix |
|---|---|---|
Login failed for user 'britive_svc' | Service account credentials wrong | Verify ADMIN_USER and ADMIN_PASSWORD variables; test with sqlcmd -S <server> -U britive_svc -P <pass> |
Cannot drop the login, because it does not exist | Login was already cleaned up | Confirm IF EXISTS guard is in the checkin routine; ignore this error — it is safe |
Cannot kill process executing within the KILL statement | KILL targeting its own session | Expected — sqlcmd skips its own session ID automatically |
| Session still active after checkin | Session not visible in sys.dm_exec_sessions | Verify the broker has VIEW SERVER STATE; check for MARS/pooled connections that may reuse sessions |
CHECK_POLICY = OFF causes error | SQL Server policy requires password history check | Remove CHECK_POLICY = OFF or adjust the generated password to meet policy requirements |