Skip to content

fix(appauth): [OCISDEV-1570] key app passwords by a stable user id, migrate old keys on load - #777

Merged
2403905 merged 1 commit into
mainfrom
fix/appauth-stable-user-key-on-load
Oct 21, 2026
Merged

2403905 merged 1 commit into
mainfrom
fix/appauth-stable-user-key-on-load

Conversation

@2403905

@2403905 2403905 commented Oct 20, 2026

Copy link
Copy Markdown

Problem

The json app auth manager in pkg/appauth/manager/json/json.go stored the app passwords of each user under UserId.String().
That is the protobuf text format.
Protobuf-go changes its whitespace between builds.
After an upgrade, the manager does not find the stored app passwords.
Every request with an existing app password fails with password not found.

Changes

  • userKey(id) returns <idp>|<opaque id>. It replaces every UserId.String() lookup.
  • migrateLegacyKeys() runs once in New().
    • It parses each old key back into a user id.
    • It moves the bucket to the new key.
    • It merges buckets of one user that differ only in whitespace. The stable bucket wins on a conflict.
    • It keeps keys that are not a user id.
    • It builds a new map and swaps it in. It does not change the map while it ranges over it.
  • New() saves the file only when a key changed, or when the purge of expired tokens runs. A failed save is not fatal. The next start repeats the migration.
  • The request path does one lookup under the stable key. It does no legacy work.

Tests

  • Existing tests build their state with userKey(...).
  • New tests in pkg/appauth/manager/json/json_migrate_test.go:
    • Load migrates the one-space and two-space keys, in memory and on disk.
    • Another user's token never authenticates the user.
    • A second start does not rewrite the file.
    • The stable token wins on a conflict.
    • A missing file, an empty file, {} and null load as an empty state.
  • go test -race ./pkg/appauth/... passes.

Notes

Changelog: changelog/unreleased/bugfix-appauth-stable-user-key.md

🤖 Generated with Claude Code

@2403905
2403905 requested a review from a team as a code owner October 20, 2026 22:01
@kw-security

kw-security commented Oct 20, 2026 •

Copy link
Copy Markdown

✅ Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
✅ Open Source Security 0 0 0 0 0 issues
✅ Licenses 0 0 0 0 0 issues
✅ Code Security 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

… on load

The json app auth manager stored the app passwords of each user under
UserId.String(). That is the protobuf text format. Protobuf-go changes its
whitespace between builds. After an upgrade, the manager did not find the
stored app passwords, and every app password failed with "password not found".

- Store the app passwords under the key "<idp>|<opaque id>".
- Move the buckets with the old keys to the new key once, in New().
- Merge buckets of one user that differ only in whitespace. The stable
  bucket wins on a conflict.
- Keep keys that are not a user id. Save the file only when a key changed.

Co-authored-by: Paul Faure <[email protected]>
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@2403905
2403905 force-pushed the fix/appauth-stable-user-key-on-load branch from d290117 to 6f38083 Compare October 20, 2026 22:05
@2403905 2403905 changed the title fix(appauth): key app passwords by a stable user id, migrate old keys on load fix(appauth): [OCISDEV-1570] key app passwords by a stable user id, migrate old keys on load Oct 21, 2026
@2403905
2403905 requested a review from jvillafanez October 21, 2026 08:56
@jvillafanez

Copy link
Copy Markdown
Member

The code looks fine, but there are some conceptual problems:

  • The migration code runs on every startup regardless of the actual state. This could slow down the service startup noticeably if there are too many app passwords even if those have been already migrated.
  • In general, we lack a migration workflow. The more migrations are needed, the more polluted the code will be.

If we can't invest time for a proper migration workflow (also including all the possible services, not just this one), it might be better to move the code to a cli command so the admins can execute it only once when they want, as I suggested in #755 (comment)

@2403905

2403905 commented Oct 21, 2026

Copy link
Copy Markdown
Author

The code looks fine, but there are some conceptual problems:

  • The migration code runs on every startup regardless of the actual state. This could slow down the service startup noticeably if there are too many app passwords even if those have been already migrated.
  • In general, we lack a migration workflow. The more migrations are needed, the more polluted the code will be.

If we can't invest time for a proper migration workflow (also including all the possible services, not just this one), it might be better to move the code to a cli command so the admins can execute it only once when they want, as I suggested in #755 (comment)

Given the current architecture of the app-auth, this doesn't really matter, because the file is loaded in its entirety at the start of the application anyway, and one iteration over the map shouldn't take too long.
Also, given the multi-instance unsafe architecture of this code, I don’t think we should come up with anything new without refactoring it first.

// protobuf-go varies its whitespace between binaries, so the same user gets a
// different key after a rebuild and loses access to the stored app passwords.
func userKey(id *userpb.UserId) string {
return id.GetIdp() + "|" + id.GetOpaqueId()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should we include a prefix so the new keys are easily identified later? Something like "kv90"

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Given the lifetime of the keys and any number of keys can be created for a user at any time, I don’t think this is critical.

@2403905
2403905 merged commit bacb0c7 into main Oct 21, 2026
1 check was pending
@2403905
2403905 deleted the fix/appauth-stable-user-key-on-load branch October 21, 2026 13:19
2403905 added a commit that referenced this pull request Oct 21, 2026
…rate old keys on load (#779)

Backport of #777 to stable-8.2.

The json app auth manager stored the app passwords of each user under
UserId.String(). Protobuf-go changes the whitespace of that text between
builds. After an upgrade, the manager did not find the stored app passwords.

- Store the app passwords under the key "<idp>|<opaque id>".
- Move the buckets with the old keys to the new key once, in New().

Co-authored-by: Claude Sonnet 5.5 <[email protected]>
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.

3 participants