# Inheriting a codebase that's locked out of its own auth and secrets

> I took over a SaaS whose auth server and secrets store still belonged to the previous agency, and both went dark. How I moved them onto the client's AWS and launched.

Source: https://ebrahimsameh.com/blog/inheriting-a-locked-out-codebase/
Updated: 2026-11-25

When you inherit a codebase from another agency, the code is often the easy part. The hard part is everything it depends on that the old team still controls. On [Guardix](/projects/guardix/), a SaaS for security-guard companies, the login server and the secrets store both lived on the previous agency's infrastructure. Around the time of the handover, both stopped working for us. The fix was to move each of them onto accounts the client owns, then remove every reference to the old ones, and only then build anything new.

## What I walked into

Guardix has a Django backend, a Next.js web dashboard, an Expo guard app for iOS and Android, and a client portal. Auth runs through Logto over OIDC. All three apps send users to Logto, and Django checks their tokens against Logto's public keys.

By 2 March 2026 the picture was this:

- The staging Logto server ran on a machine a former developer managed, and it was returning HTTP 503. With no login, nobody could use staging or run the apps locally against real auth.
- Every CI workflow, both Django Dockerfiles and the Amplify web build fetched their secrets from an Infisical server hosted by the previous agency. It started answering `403 org_internal`. We couldn't read a single secret or log in to the CLI.

So nobody could log in, and nothing could build or deploy. The code itself looked fine.

## Step one: own the auth server

I self-hosted Logto next to Django. Locally it went into Docker Compose with its own Postgres. On staging it ran on the same EC2 server as Django behind Traefik, with DNS managed in Terraform.

The previous team had put Logto in the staging compose file, but nothing else needed to make it run was there. A fresh deploy crashed straight away because Logto's database had no schema. I built that pipeline from scratch:

1. Start Logto's Postgres first and wait for it to accept connections.
2. Seed the schema with Logto's CLI.
3. Deploy Logto's database alterations. Logto refuses to start until row-level security is enabled on their own tables.
4. Bring up the rest of the stack and poll Logto's OIDC discovery URL, up to 18 times, 10 seconds apart. Logto takes 60 to 90 seconds to start, and the old workflow checked once and failed the deploy.
5. Serve the auth domains over HTTPS with Let's Encrypt through Traefik, with a restart policy on the container.

HTTPS wasn't optional. The login flow uses PKCE, which needs `crypto.subtle`, and browsers only expose that on secure origins. Without TLS on the auth domain, nothing in the browser could log in.

There were a few smaller traps. Logto's internal ports have to match the ports you publish, or token validation fails when Django fetches the keys. Django inside Docker has to reach Logto by its service name, while the browser uses the public URL. And the mobile app was failing with `oidc.invalid_client` because it reused the web app's client. A native app needs its own native client in Logto, with its own redirect URI and the `roles` scope.

## Step two: get the secrets out of someone else's vault

Infisical was wired into nearly everything. Its CLI was even installed inside the production Docker images. I took it out of five GitHub Actions workflows, both Dockerfiles, the Amplify build script and the pipeline that provisions pull-request preview environments on ECS Fargate. I deleted the leftover config and import scripts so nobody would try to use them again.

Secrets went to GitHub Secrets and Amplify environment variables first, because that was the fastest way to get builds green. I also renamed them as I moved them. Names like `INFISICAL_SSH_KEY` became `EC2_SSH_KEY`, and I wrote a setup guide for every secret: where its value comes from and what format it expects. A few days later, on 8 March, runtime secrets moved again, to AWS Secrets Manager in the client's own account. Production later used the same setup: the servers read their config from Secrets Manager through their IAM role, and GitHub Actions deploys through an OIDC role, so CI holds no long-lived AWS keys.

That same day I went looking for anything else the previous developers could still use, such as admin accounts and passwords they had created in Django, and rotated it. If someone outside your team could still log in, the handover isn't done.

## Step three: make staging boring

With auth and secrets under the client's control, I went through the rest of staging. By 4 March every blocker was cleared. The pass touched 329 files across the backend, web, mobile, shared packages, infrastructure and CI. A few of the fixes:

- A tenant data leak. The endpoint that listed roles returned every tenant's roles to any logged-in user. I fixed it in layers: an explicit tenant filter in the query on top of Postgres row-level security, organization IDs in the frontend cache keys, queries that wait until an organization is selected, and a header on every request telling the backend not to bypass tenant scoping.
- The web build. Amplify couldn't resolve the monorepo's shared packages, and the server bundle was over Amplify's 220 MB limit. I rewrote the build to install from the monorepo root, build the shared packages in order and strip about 80 MB of binaries, cache and source maps.
- The deploy workflow. It now validates the env file and the compose config before starting anything, cleans up Docker disk space before pulling, and cancels overlapping deploys.
- Preview environments. Each PR gets its own database, copied from staging, and its own DNS record. Closing the PR tears them down, and a nightly job removes any that were missed.

## Then the real work

Once staging was stable, the project could move like a normal one. The client sent findings reports from their own testing, and I worked through them one item at a time, sorting the later batches from quick fixes to multi-day features. After each batch I sent the client a plain list of what was done. Production went live on AWS on 20 April 2026, and the first Android release went to Google Play on 24 April.

## What I'd check on any inherited project

If you're taking a project over from another team, find out early what it depends on that you don't control. This is the list I'd run through now, before touching any features:

- Who runs the identity provider? Find the server, the account, the domain and the admin login. If the answer is "the old agency", that's the first thing to move.
- Where do secrets come from at build time and at runtime? Grep the workflows, Dockerfiles, build configs and setup scripts for the vendor's CLI and hostnames. One reference left behind is enough to break a deploy months later.
- Who owns the DNS, the cloud accounts and the app store listings? They should all sit with the client, with you added as a user.
- What can the previous team still log in to? Rotate admin accounts, API keys and deploy keys, even when the handover is friendly.
- Can you deploy from a clean checkout using only the client's accounts? Until you can, you don't really control the project.

The client sees very little of this work, so tell them about it in plain terms. On Guardix it came before any feature work, and everything after it, including the April launch, ran on accounts the client owns.
