Skip to content

CLOUDP-441133: fix service account teardown failing with 401 - #36

Merged
fmenezes merged 1 commit into
mainfrom
CLOUDP-441133
Sep 1, 2026
Merged

fmenezes merged 1 commit into
mainfrom
CLOUDP-441133

Conversation

@fmenezes

@fmenezes fmenezes commented Sep 1, 2026

Copy link
Copy Markdown
Collaborator

The Setup a Service Account and create a project job fails 100% of the time at its final Delete the Service Account step, blocking every unrelated PR in this repo (currently #34 and #35). It is not flaky — confirmed across 4 rerun attempts on #34 and again on #35.

build/generate-service-account.sh creates the service account against ${MONGODB_ATLAS_OPS_MANAGER_URL} (https://cloud-dev.mongodb.com/, from vars.CLOUD_DEV_URL), but build/terminate-service-account.sh hardcoded https://cloud.mongodb.com/. So the account is created in cloud-dev and the delete is attempted against production, where the configured API keys have no rights:

MONGODB_ATLAS_OPS_MANAGER_URL: https://cloud-dev.mongodb.com/
Failed to delete service account with Client ID mdb_sa_id_6a96f1cdfef2f1eb6d9fce78. Response:
{
  "error" : 401,
  "reason" : "Unauthorized",
  "detail" : "You are not authorized for this resource."
}

@fmenezes
fmenezes requested a review from a team as a code owner September 1, 2026 15:59
The CI job "Setup a Service Account and create a project" failed 100% of
the time at its final teardown step, blocking every unrelated PR.

generate-service-account.sh creates the service account against
$MONGODB_ATLAS_OPS_MANAGER_URL (cloud-dev), but
terminate-service-account.sh hardcoded https://cloud.mongodb.com/. The
account was created in cloud-dev and the delete attempted against prod,
where the configured API keys have no rights, so every run left an
orphaned ORG_OWNER service account behind.

Also fixed in the same area:

- Both scripts now use curl --fail-with-body, so a non-2xx response is a
  non-zero exit while the response body is still available for
  diagnostics. terminate-service-account.sh previously parsed the status
  out of the body with `jq -r '.error'`; on a successful 204 the body is
  empty, so the check became `[ "" -ge 300 ]`, which errored on stderr
  and then fell through to the success branch by accident. A failure
  returning a non-JSON body (e.g. an HTML 503) was reported as success.

- Masked the service account secret with ::add-mask:: so it is no longer
  echoed unredacted into this public repo's run logs. Unlike the API
  keys, this value is minted at runtime and passed between steps as an
  output, so GitHub does not mask it automatically.

- Added the missing MONGODB_ATLAS_OPS_MANAGER_URL guard to the terminate
  script, and corrected the generate script's guard, which reported
  MONGODB_ATLAS_ORG_ID as the missing variable.
@fmenezes
fmenezes enabled auto-merge (squash) September 1, 2026 16:12
error_code=$(echo "$output" | jq -r '.error')

if [ "$error_code" -ge 300 ]; then
-X DELETE "${MONGODB_ATLAS_OPS_MANAGER_URL}api/atlas/v2/orgs/${MONGODB_ATLAS_ORG_ID}/serviceAccounts/${CLIENT_ID}"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huh, I'm suprised that this only started failing lately cuz this bug was implemented (by me oops) nearly a year ago.

Any hypothesis on why this cropped up now?
I thought it might have been that we hit a max number of SAs for the dev org but it doesn't look to be the case

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this has happened before we just never paid attention

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not so sure. We should've been blocked on merging PRs since the bug was introduced.
Either way, just curious :)

@cveticm cveticm left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the fix

@fmenezes
fmenezes merged commit 0130510 into main Sep 1, 2026
10 checks passed
@fmenezes
fmenezes deleted the CLOUDP-441133 branch September 1, 2026 16:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants