Skip to content

Conversation

@feeblefakie
Copy link
Contributor

This is an automated backport of the following:

Please merge this PR after all checks have passed.

@gemini-code-assist
Copy link

Summary of Changes

Hello @feeblefakie, 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 backport addresses a NullPointerException that could occur when a client was misconfigured, attempting to use digital signatures where HMAC authentication was expected. The core of this change is the introduction of a dedicated MissingSecretException to provide more precise error handling. The system's secret management components have been updated to explicitly throw this exception when a secret is not found, preventing generic NullPointerExceptions and offering clearer diagnostic information for misconfigurations.

Highlights

  • New Exception for Missing Secrets: Introduced a new MissingSecretException to specifically handle scenarios where a required secret (like a digital signature key) cannot be found, providing clearer error messages than a generic NullPointerException.
  • Refined Secret Lookup Logic: Modified the SecretManager and ScalarSecretRegistry to throw the new MissingSecretException when a secret is not found during a lookup operation, instead of returning null. This ensures misconfigurations are caught with a specific error.
  • New Error Code: Added a new SECRET_NOT_FOUND error (status code 415) to CommonError and StatusCode enums to align with the new exception.
  • Updated Test Cases: Updated integration and unit tests to reflect the new exception-throwing behavior, specifically adding a test case to verify that MissingSecretException is thrown when a client is misconfigured with digital signatures while HMAC is expected.
  • File Removal: Removed the Constants.java file, which previously contained the KEYS_JSON_NAME constant.
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 backports a fix for a NullPointerException that occurs when a client is misconfigured for digital signatures. The main change involves modifying SecretRegistry.lookup to throw a MissingSecretException instead of returning null when a secret is not found, which is a good practice to prevent NPEs. The changes are consistent across the codebase, with related error codes, exceptions, and tests being updated accordingly. I've provided a few suggestions to improve code clarity and testing practices.

Comment on lines 59 to 66
public void register(SecretEntry entry) {
SecretEntry existing = registry.lookup(entry.getKey());
if (existing != null) {
try {
registry.lookup(entry.getKey());
throw new DatabaseException(CommonError.SECRET_ALREADY_REGISTERED);
} catch (MissingSecretException e) {
registry.bind(entry);
}
registry.bind(entry);
}

Choose a reason for hiding this comment

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

medium

Using a try-catch block for control flow, as done here, can make the code harder to understand. The expected path for registration (where the secret does not yet exist) is handled in the catch block, which is unconventional.

A clearer approach would be to introduce an exists() method in the SecretRegistry interface. This would make the intent of the code more explicit and improve readability.

For example, you could change the register method to:

public void register(SecretEntry entry) {
    if (registry.exists(entry.getKey())) {
        throw new DatabaseException(CommonError.SECRET_ALREADY_REGISTERED);
    }
    registry.bind(entry);
}

This would require adding an exists() method to the SecretRegistry interface and its implementations, but it would represent a more idiomatic way to handle this check.

Comment on lines +83 to +93
Result result;
try {
return storage.get(get).map(this::toSecretEntry).orElse(null);
result =
storage
.get(get)
.orElseThrow(() -> new MissingSecretException(CommonError.SECRET_NOT_FOUND));
} catch (ExecutionException e) {
throw new DatabaseException(CommonError.GETTING_SECRET_KEY_FAILED, e, e.getMessage());
}

return toSecretEntry(result);

Choose a reason for hiding this comment

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

medium

This implementation can be made more concise by combining the variable declaration and assignment, and returning the result of toSecretEntry from within the try block. This improves readability by reducing the scope of the result variable.

Suggested change
Result result;
try {
return storage.get(get).map(this::toSecretEntry).orElse(null);
result =
storage
.get(get)
.orElseThrow(() -> new MissingSecretException(CommonError.SECRET_NOT_FOUND));
} catch (ExecutionException e) {
throw new DatabaseException(CommonError.GETTING_SECRET_KEY_FAILED, e, e.getMessage());
}
return toSecretEntry(result);
try {
Result result =
storage
.get(get)
.orElseThrow(() -> new MissingSecretException(CommonError.SECRET_NOT_FOUND));
return toSecretEntry(result);
} catch (ExecutionException e) {
throw new DatabaseException(CommonError.GETTING_SECRET_KEY_FAILED, e, e.getMessage());
}

Comment on lines +47 to +48
MissingSecretException toThrow = mock(MissingSecretException.class);
when(registry.lookup(entry.getKey())).thenThrow(toThrow);

Choose a reason for hiding this comment

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

medium

It's generally better to avoid mocking simple data-holder classes like exceptions. You can directly instantiate the exception. This simplifies the test setup and avoids unnecessary mocking.

Suggested change
MissingSecretException toThrow = mock(MissingSecretException.class);
when(registry.lookup(entry.getKey())).thenThrow(toThrow);
when(registry.lookup(entry.getKey())).thenThrow(new MissingSecretException("secret not found"));

@jnmt jnmt merged commit d609f94 into 3.11 Oct 29, 2025
9 checks passed
@jnmt jnmt deleted the 3.11-pull-302 branch October 29, 2025 10:05
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