Per-User Database Access on a Shared Fleet: A Just-in-Time Credential Broker for Amazon WorkSpaces Applications (formerly AppStream 2.0)
This article gives a pattern for per-user, just-in-time database access on a shared fleet. A small credential broker verifies a signed identity token and returns short-lived credentials: RDS IAM authentication tokens, or temporary users with an expiry for databases without IAM authentication. It also covers the limits to plan for, such as open connections outliving an expired token. It ends with a checklist for testing that one user can't reach another user's data.
Platform teams often get this request: a browser-based portal that opens a database GUI, such as pgAdmin or DBeaver, already connected and ready to use. No developer holds standing credentials, and each developer reaches only their own data. If Alice signs in, she reaches her database and nothing else, even if she looks for a way around it.
Amazon WorkSpaces Applications (formerly Amazon AppStream 2.0, and still appstream in the API and CLI) handles the streaming. Identity is the hard part. A WorkSpaces Applications fleet runs every concurrent session under the same AWS Identity and Access Management (IAM) role, which rules out the usual AWS-native ways to isolate users.
This article explains why the shared role breaks the intuitive designs, then walks through a pattern that works: a small credential broker that verifies a signed token and returns short-lived, per-user database credentials.
Why the obvious design fails
The first idea most teams reach for is reasonable:
- Store each developer's database credentials in AWS Secrets Manager.
- When a session starts, have the streaming instance read that developer's secret and pass it to the database tool.
The second step is where it fails. Every session in the fleet runs as the same IAM role, so a call to secretsmanager:GetSecretValue reaches AWS as the fleet role, not as a person. The usual isolation tools can't tell the sessions apart either:
- Per-user IAM roles don't apply, because the fleet has one role.
- Attribute-based access control (ABAC) has nothing to match on, because the fleet role carries no per-user principal tag.
- Secret resource policies see only the shared role, so they can't narrow access to one user.
Anything the fleet role can read, any session can read.
Session context doesn't close the gap. It travels in the streaming URL, which users can see and edit, and WorkSpaces Applications doesn't validate it (Session context). Treat it as metadata, not proof of identity.
The rule that follows: a session's claim to be a particular user counts only when it arrives as a signed token that the server verifies.
The pattern: verify the token, then mint a scoped credential
Developers sign in to the portal through single sign-on (SSO) with multi-factor authentication (MFA). In your identity provider (IdP), register a separate OpenID Connect (OIDC) app whose only audience is the credential broker.
When a session starts, a launcher inside the session requests an access token from that app using the authorization code flow with Proof Key for Code Exchange (PKCE), then sends the token straight to the broker over Transport Layer Security (TLS). The token never travels in the streaming URL or the session context, so a user can't substitute someone else's identity. Users may see a second sign-in prompt inside the session, because their IdP session lives in their local browser rather than on the streaming instance.
Exactly one component, the credential broker, can create or revoke database credentials.
Figure 1. The credential broker verifies the user's token and is the only component that mints database credentials. The fleet itself holds no standing access.
How a request flows:
- The developer signs in to the portal through the IdP with SSO and MFA.
- WorkSpaces Applications streams the session to the developer's browser.
- A launcher in the session gets an access token for the broker (authorization code flow with PKCE).
- The launcher calls
POST /credentialswith the token, through the interface VPC endpoint to the private API. - The Lambda authorizer verifies the JWT and passes the request to the broker.
- The broker looks up the user by
sub, checksgroups, and issues a scoped, short-lived credential: an RDS IAM authentication token, or a temporary user on the database without IAM authentication. - The database GUI connects as that user over TLS.
When the session ends, /revoke removes temporary users and ends open PostgreSQL connections. Expiry is the backstop.
Key design choices:
- Put the broker behind a private REST API in Amazon API Gateway. The fleet reaches it through an interface VPC endpoint. A Lambda authorizer rejects any request without a valid token by checking the JSON Web Token (JWT) signature against the IdP's JSON Web Key Set (JWKS), along with the issuer, audience, and expiry. HTTP APIs have a built-in JWT authorizer but no private endpoint type (Choose between REST APIs and HTTP APIs). If a public regional endpoint is acceptable for your broker, an HTTP API's JWT authorizer does the same checks with less code.
- Take identity only from the verified token's
subclaim, never from a username in the request. This rule is what enforces isolation. - Use two lookups to decide what the broker issues. The token's
groupsclaim decides which engines and clusters the user may use, and that mapping lives in the IdP. A broker table keyed onsubrecords each developer's database user (for example,dev_alice) and scope. If either lookup fails, the broker returns 403. - Keep the broker's IAM role small: read its admin secret, decrypt with one AWS Key Management Service (AWS KMS) key, write audit records, and
rds-db:connecton thedev_*database users. That last permission lets the broker connect as any developer, so the broker is the component to protect most carefully. - Give the fleet's instance role nothing for this flow. Calling the broker needs no AWS permissions. Add an explicit deny on
secretsmanager:GetSecretValueanyway, so a broader policy attached later can't open a back door. - Use single-session fleets. On multi-session fleets, each user's token and credentials sit on an instance shared with other users, which needs a different isolation design.
Making the credentials short-lived, or better yet, absent
PostgreSQL on Amazon RDS or Aurora: IAM database authentication
Enable IAM database authentication, give each developer a least-privilege database role, and have the broker generate an authentication token for that role:
CREATE USER "dev_alice" WITH LOGIN; GRANT rds_iam TO "dev_alice"; GRANT USAGE ON SCHEMA alice_schema TO "dev_alice"; GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA alice_schema TO "dev_alice"; -- Also cover tables created later by the role that runs this statement ALTER DEFAULT PRIVILEGES IN SCHEMA alice_schema GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO "dev_alice";
The token is valid for 15 minutes and acts as the password. Nothing is stored, so there's nothing to rotate or leak. Connections must use TLS.
The token controls only new connections. PostgreSQL checks it when a connection opens, so a connection opened at minute 14 stays open after the token expires (Connecting with IAM authentication). That has two consequences:
- The GUI needs a fresh token from the broker whenever it reconnects.
/revokemust also end the user's open connections. The broker can do this withpg_terminate_backend, connecting as a broker-only role that holdspg_signal_backendand also uses IAM authentication, so no password is stored.
Engines without IAM authentication: temporary users
When the engine has no IAM hook, the broker creates a temporary database user on each request, with a strong random password, a scope limited to the developer's data, and a built-in expiry. Two examples:
- MongoDB Atlas: create the user through the Atlas Admin API with a
deleteAfterDate, and Atlas deletes it at that time (Create One Database User). Temporary users count against Atlas's per-project database user limit, so size the limit for your peak concurrent sessions. - Self-managed PostgreSQL: create the role with
LOGIN PASSWORD '...' VALID UNTIL '...'(CREATE ROLE).VALID UNTILblocks new password logins after that time but doesn't drop the role, so the broker also needs a cleanup job.
For either engine, check whether open connections end when the user expires or is deleted. If they don't, revoke has to end them.
This approach leaves one long-lived secret per engine: the admin credential the broker uses to create users. Store it in Secrets Manager, encrypted with a customer managed KMS key and readable only by the broker role. Where the engine supports it, also restrict that credential to the broker's egress IP address (its NAT gateway's Elastic IP address).
When a session ends, a termination script calls the broker's /revoke route. If that call fails, expiry still limits new access: the PostgreSQL token can't open a connection after 15 minutes, and temporary users expire on schedule. Revoke cleans up quickly; expiry is the backstop.
Verifying the isolation
Before rollout, test with at least two users, Alice and Bob:
- Happy path: Alice signs in, and the GUI connects to her database only.
- Cross-user isolation: Alice's credential can't read Bob's database, collection, or schema.
- Request tampering: a request with Alice's valid token that names Bob's database returns only Alice's credential, or a 403.
- Token checks: requests with no token, a tampered token, an expired token, or a token issued for a different audience are rejected at the authorizer.
- Expiry: after 15 minutes, Alice's PostgreSQL token can't open a new connection, and temporary users disappear on schedule.
- Revocation: ending the session removes temporary users, ends open PostgreSQL connections, and marks the issuance record as revoked.
- Audit: every issue, revoke, and denial is logged against the right user. Add a CloudWatch alarm, driven by a metric filter on CloudTrail events, for any KMS
Decryptcall that doesn't come from the broker.
Living with it day to day
Onboarding: add the developer to the right IdP group, add their sub mapping, and create their database role for IAM-authenticated engines.
Offboarding: remove the developer from the group. A JWT stays valid until it expires, and its groups claim is fixed when it's issued, so they can still mint credentials until their current token expires. Keep the broker app's token lifetime short. For an immediate cutoff, have the broker check group membership with the IdP on each request, and end any open PostgreSQL connections.
New database engine: add one issuance path behind the same /credentials contract.
Before you build: choose the fleet type early
On Always-On and On-Demand fleets, session scripts are built into the image, and processes on the instance can use the fleet's IAM role through the appstream_machine_role credential profile. On Elastic fleets, session scripts are packaged as a .zip file in Amazon S3, and the fleet's VPC must allow access to that bucket.
The documentation doesn't describe exactly when private VPC endpoints and the fleet role become reachable from a session-start script on each fleet type. Confirm early that your startup script can reach the broker before the user's session begins. If it can't on an Elastic fleet, use an On-Demand or Always-On fleet.
Key takeaways
- Learn the platform's identity model before you design. A shared fleet role rules out per-user IAM roles, ABAC, and secret resource policies inside the session.
- When the platform can't tell users apart, verify a signed token yourself, in one small broker, and take identity only from
sub. - Prefer credentials that expire, and know what expiry covers. An expired token or user blocks new connections, not necessarily open ones.
- Treat session context as a hint, never as authorization.
The pattern applies beyond database tools: authenticate at a strong front door, verify the signed token in one place, return something short-lived and narrowly scoped, and use expiry as the backstop.
References
- Create and specify session scripts
- Using an IAM role with streaming instances
- Session context
- IAM database authentication for RDS
- Connecting to your DB instance using IAM authentication
- Private REST APIs in API Gateway
- Use API Gateway Lambda authorizers
- Choose between REST APIs and HTTP APIs
- MongoDB Atlas Admin API: Create One Database User
- PostgreSQL: CREATE ROLE
