Skip to content

✨ server: add issuer to kms#876

Draft
aguxez wants to merge 1 commit intogcp-allowerfrom
issuer
Draft

✨ server: add issuer to kms#876
aguxez wants to merge 1 commit intogcp-allowerfrom
issuer

Conversation

@aguxez
Copy link
Contributor

@aguxez aguxez commented Mar 11, 2026


This is part 2 of 2 in a stack made with GitButler:


Open with Devin

Summary by CodeRabbit

  • Bug Fixes

    • Removed embedded private key usage and switched issuer handling to a secure, KMS-backed approach for improved security.
  • Chores

    • Removed issuer private key from runtime and test configurations.
    • Updated account retrieval and signing flows to use lazy, asynchronous issuer resolution.
  • Tests

    • Updated mocks to include an issuer mock for test coverage.
  • Changelog

    • Added a changeset entry for a patch release.

@changeset-bot
Copy link

changeset-bot bot commented Mar 11, 2026

🦋 Changeset detected

Latest commit: 14cbdb1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@exactly/server Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@aguxez aguxez marked this pull request as draft March 11, 2026 11:04
@coderabbitai
Copy link

coderabbitai bot commented Mar 11, 2026

Walkthrough

Moves issuer private key handling to KMS-backed retrieval and lazy initialization: removes ISSUER_PRIVATE_KEY from configs, parameterizes account retrieval by key ("allower" | "issuer"), adds an issuer helper, updates signing to async use the dynamic issuer, and updates tests/mocks accordingly.

Changes

Cohort / File(s) Summary
Config & metadata
.changeset/tough-beans-help.md, .do/app.yaml, server/vitest.config.mts, server/script/openapi.ts
Adds a changeset and removes ISSUER_PRIVATE_KEY from deployment, test, and script env configs.
Account management
server/utils/accounts.ts
getAccount now accepts `key: "allower"
Signing logic
server/utils/panda.ts
Replaces synchronous issuer construction with lazy async factory (issuerPromise, getIssuer()); converts signIssuerOp to async and awaits the issuer; removes direct private-key parsing.
Tests / Mocks
server/test/mocks/accounts.ts
Adds padHex import and an issuer mock returning a privateKeyToAccount derived from padHex("0x420") to match the new issuer helper.

Sequence Diagram(s)

sequenceDiagram
  participant Caller as Caller
  participant Panda as server/utils/panda.ts
  participant Accounts as server/utils/accounts.ts
  participant KMS as GCP KMS
  participant Wallet as WalletClient

  Caller->>Panda: signIssuerOp({ account, amount, timestamp })
  Panda->>Panda: await getIssuer()
  Panda->>Accounts: getAccount("issuer")
  Accounts->>KMS: withRetry -> request key material for "issuer"
  KMS-->>Accounts: return key material / private key
  Accounts-->>Panda: LocalAccount (cached via issuerPromise)
  Panda->>Wallet: signTypedData(using issuer account)
  Wallet-->>Panda: signature
  Panda-->>Caller: signature
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Suggested reviewers

  • cruzdanilo
  • nfmelendez
🚥 Pre-merge checks | ✅ 2
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the main change: migrating the issuer account from environment variables to KMS (Key Management Service) integration, as evidenced by removal of ISSUER_PRIVATE_KEY env vars and addition of issuer account to the KMS-backed accounts system.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch issuer

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 4 additional findings.

Open in Devin Review

@sentry
Copy link

sentry bot commented Mar 11, 2026

✅ All tests passed.

@gemini-code-assist
Copy link

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the security posture of the server by transitioning the management of the issuer account's private key from direct environment variable storage to Google Cloud Key Management Service (KMS). This change eliminates the risk associated with hardcoding sensitive keys, ensuring that cryptographic operations for the issuer account are performed securely within a managed hardware security module (HSM) environment. The refactoring involved updating configuration files, test mocks, and core utility functions to seamlessly integrate with the new KMS-based key retrieval mechanism.

Highlights

  • Security Enhancement: Migrated the issuer account's private key management to Google Cloud Key Management Service (KMS) for enhanced security.
  • Configuration Cleanup: Removed hardcoded private key storage from environment configurations and test setups across various files.
  • Account Utility Refactoring: Refactored core account utility functions to support dynamic fetching of KMS-managed accounts, making the system more flexible and secure.
Changelog
  • .changeset/tough-beans-help.md
    • Added a changeset entry documenting the issuer account KMS migration.
  • .do/app.yaml
    • Removed the ISSUER_PRIVATE_KEY environment variable definition.
  • server/script/openapi.ts
    • Removed the ISSUER_PRIVATE_KEY assignment from the OpenAPI script.
  • server/test/mocks/accounts.ts
    • Updated account mocks to include padHex import and mock the issuer function for KMS-based account retrieval in tests.
  • server/utils/accounts.ts
    • Modified the getAccount function to be generic for different KMS keys (allower, issuer).
    • Introduced a new issuer export function to specifically retrieve the issuer account from KMS.
    • Updated the allower function to use the generalized getAccount with nonce management.
  • server/utils/panda.ts
    • Removed direct privateKeyToAccount usage.
    • Imported the new issuer utility from ./accounts.
    • Updated signIssuerOp to asynchronously fetch the issuer account from KMS before signing.
  • server/vitest.config.mts
    • Removed the ISSUER_PRIVATE_KEY definition from the Vitest environment configuration.
Activity
  • No human activity (comments, reviews, or progress updates) has been recorded for this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request successfully migrates the ISSUER_PRIVATE_KEY from direct environment variable storage to Google Cloud Key Management Service (KMS), significantly enhancing security. The changes involve updating environment configurations, modifying account retrieval logic to use KMS, and adjusting test setups accordingly. The implementation correctly handles the asynchronous nature of KMS operations and incorporates retry mechanisms for resilience. The getAccount function is now more flexible, supporting different account types, and the issuer account is lazily initialized and memoized, which is a good pattern for performance and resource management. Overall, the changes are well-executed and align with best practices for secure key management.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
server/test/mocks/accounts.ts (1)

19-24: ⚠️ Potential issue | 🟠 Major

Mock getAccount() too, not just issuer().

...original keeps the live KMS-backed getAccount export around. Any test that reaches getAccount("issuer") or getAccount("allower") will still execute the real GCP path even though issuer() is stubbed here.

🧪 Suggested change
 vi.mock("../../utils/accounts", async (importOriginal) => {
   const original = await importOriginal<typeof accounts>();
+  const getAccount = vi.fn((key: "allower" | "issuer") =>
+    Promise.resolve(privateKeyToAccount(padHex(key === "issuer" ? "0x420" : "0x421"))),
+  );
   return {
     ...original,
+    getAccount,
     allower: vi.fn(() => Promise.resolve({ allow: vi.fn().mockResolvedValue({}) })),
-    issuer: vi.fn(() => Promise.resolve(privateKeyToAccount(padHex("0x420")))),
+    issuer: vi.fn(() => getAccount("issuer")),
server/utils/accounts.ts (1)

346-360: ⚠️ Potential issue | 🟠 Major

Use separate KMS version config for allower and issuer.

CryptoKeyVersion numbers are scoped per CryptoKey, and different CryptoKeys in the same key ring can have different primary versions. This code uses a single GCP_KMS_KEY_VERSION for both keys, which only works while both keys happen to share the same version number. Use per-key version configuration or dynamically resolve the active version for each key.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 76280595-3431-4e06-98f0-da3eccac84b6

📥 Commits

Reviewing files that changed from the base of the PR and between 9edf934 and e460a39.

📒 Files selected for processing (7)
  • .changeset/tough-beans-help.md
  • .do/app.yaml
  • server/script/openapi.ts
  • server/test/mocks/accounts.ts
  • server/utils/accounts.ts
  • server/utils/panda.ts
  • server/vitest.config.mts
💤 Files with no reviewable changes (3)
  • server/script/openapi.ts
  • server/vitest.config.mts
  • .do/app.yaml

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 3de77ce6-ef9a-4339-9b2f-4c8ae21004c4

📥 Commits

Reviewing files that changed from the base of the PR and between e460a39 and c7c3335.

📒 Files selected for processing (7)
  • .changeset/tough-beans-help.md
  • .do/app.yaml
  • server/script/openapi.ts
  • server/test/mocks/accounts.ts
  • server/utils/accounts.ts
  • server/utils/panda.ts
  • server/vitest.config.mts
💤 Files with no reviewable changes (3)
  • .do/app.yaml
  • server/vitest.config.mts
  • server/script/openapi.ts

Comment on lines +336 to +342
function getIssuer() {
issuerPromise ??= createIssuer().catch((error: unknown) => {
issuerPromise = undefined;
throw error;
});
return issuerPromise;
}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion | 🟠 Major

Inline single-use getIssuer logic into signIssuerOp.

getIssuer() is only used once, so keep this logic inline to match repository conventions.

♻️ Proposed refactor
 let issuerPromise: ReturnType<typeof createIssuer> | undefined;
-function getIssuer() {
-  issuerPromise ??= createIssuer().catch((error: unknown) => {
-    issuerPromise = undefined;
-    throw error;
-  });
-  return issuerPromise;
-}
 
 export async function signIssuerOp({
   account,
   amount,
   timestamp,
 }: {
   account: Address;
   amount: bigint;
   timestamp: number;
 }) {
-  const issuerAccount = await getIssuer();
+  const issuerAccount = await (issuerPromise ??= createIssuer().catch((error: unknown) => {
+    issuerPromise = undefined;
+    throw error;
+  }));
   return issuerAccount.signTypedData({

As per coding guidelines: "Do not extract a value into a variable or logic into a function unless it is used in two or more places; keep single-use values and functions inline."

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

♻️ Duplicate comments (1)
server/utils/panda.ts (1)

335-342: 🧹 Nitpick | 🔵 Trivial

Consider inlining the single-use getIssuer logic.

The getIssuer() function is only called once (at line 353). Per coding guidelines, single-use logic should be kept inline.

♻️ Proposed refactor
 let issuerPromise: ReturnType<typeof createIssuer> | undefined;
-function getIssuer() {
-  issuerPromise ??= createIssuer().catch((error: unknown) => {
-    issuerPromise = undefined;
-    throw error;
-  });
-  return issuerPromise;
-}

 export async function signIssuerOp({
   account,
   amount,
   timestamp,
 }: {
   account: Address;
   amount: bigint;
   timestamp: number;
 }) {
-  const issuerAccount = await getIssuer();
+  const issuerAccount = await (issuerPromise ??= createIssuer().catch((error: unknown) => {
+    issuerPromise = undefined;
+    throw error;
+  }));
   return issuerAccount.signTypedData({

As per coding guidelines: "Do not extract a value into a variable or logic into a function unless it is used in two or more places; keep single-use values and functions inline."


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: f14fb45d-e8bb-4d08-af92-ff76e0b6247e

📥 Commits

Reviewing files that changed from the base of the PR and between c7c3335 and 14cbdb1.

📒 Files selected for processing (7)
  • .changeset/tough-beans-help.md
  • .do/app.yaml
  • server/script/openapi.ts
  • server/test/mocks/accounts.ts
  • server/utils/accounts.ts
  • server/utils/panda.ts
  • server/vitest.config.mts
💤 Files with no reviewable changes (3)
  • server/script/openapi.ts
  • server/vitest.config.mts
  • .do/app.yaml

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