-
Notifications
You must be signed in to change notification settings - Fork 3
Backport to branch(3.11) : Fix NullPointerException when a client is misconfigured with digital signature #315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -17,10 +17,10 @@ | |||||||||||||||||||||||||||||||||||||||||||
| import com.scalar.dl.ledger.database.SecretRegistry; | ||||||||||||||||||||||||||||||||||||||||||||
| import com.scalar.dl.ledger.error.CommonError; | ||||||||||||||||||||||||||||||||||||||||||||
| import com.scalar.dl.ledger.exception.DatabaseException; | ||||||||||||||||||||||||||||||||||||||||||||
| import com.scalar.dl.ledger.exception.MissingSecretException; | ||||||||||||||||||||||||||||||||||||||||||||
| import com.scalar.dl.ledger.exception.UnexpectedValueException; | ||||||||||||||||||||||||||||||||||||||||||||
| import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||||||||||||||||||||||||||||||||||||||||||||
| import java.nio.charset.StandardCharsets; | ||||||||||||||||||||||||||||||||||||||||||||
| import javax.annotation.Nullable; | ||||||||||||||||||||||||||||||||||||||||||||
| import javax.annotation.concurrent.Immutable; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| @Immutable | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -72,7 +72,6 @@ public void unbind(SecretEntry.Key key) { | |||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||||||||||||
| @Nullable | ||||||||||||||||||||||||||||||||||||||||||||
| public SecretEntry lookup(SecretEntry.Key key) { | ||||||||||||||||||||||||||||||||||||||||||||
| Get get = | ||||||||||||||||||||||||||||||||||||||||||||
| new Get( | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -81,11 +80,17 @@ public SecretEntry lookup(SecretEntry.Key key) { | |||||||||||||||||||||||||||||||||||||||||||
| .withConsistency(Consistency.SEQUENTIAL) | ||||||||||||||||||||||||||||||||||||||||||||
| .forTable(TABLE); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| 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); | ||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+83
to
+93
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This implementation can be made more concise by combining the variable declaration and assignment, and returning the result of
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| private SecretEntry toSecretEntry(Result result) { | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package com.scalar.dl.ledger.exception; | ||
|
|
||
| import com.scalar.dl.ledger.error.ScalarDlError; | ||
| import com.scalar.dl.ledger.service.StatusCode; | ||
|
|
||
| public class MissingSecretException extends DatabaseException { | ||
|
|
||
| public MissingSecretException(String message) { | ||
| super(message, StatusCode.SECRET_NOT_FOUND); | ||
| } | ||
|
|
||
| public MissingSecretException(String message, Throwable cause) { | ||
| super(message, cause, StatusCode.SECRET_NOT_FOUND); | ||
| } | ||
|
|
||
| public MissingSecretException(ScalarDlError error, Object... args) { | ||
| super(error.buildMessage(args), error.getStatusCode()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |||||||
| import com.google.common.cache.CacheBuilder; | ||||||||
| import com.scalar.dl.ledger.database.SecretRegistry; | ||||||||
| import com.scalar.dl.ledger.exception.DatabaseException; | ||||||||
| import com.scalar.dl.ledger.exception.MissingSecretException; | ||||||||
| import org.junit.jupiter.api.BeforeEach; | ||||||||
| import org.junit.jupiter.api.Test; | ||||||||
| import org.mockito.Mock; | ||||||||
|
|
@@ -43,7 +44,8 @@ public void setUp() { | |||||||
| @Test | ||||||||
| public void register_ProperSecretEntryGiven_ShouldCallBind() { | ||||||||
| // Arrange | ||||||||
| when(registry.lookup(entry.getKey())).thenReturn(null); | ||||||||
| MissingSecretException toThrow = mock(MissingSecretException.class); | ||||||||
| when(registry.lookup(entry.getKey())).thenThrow(toThrow); | ||||||||
|
Comment on lines
+47
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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
|
||||||||
|
|
||||||||
| // Act | ||||||||
| manager.register(entry); | ||||||||
|
|
||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using a
try-catchblock 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 thecatchblock, which is unconventional.A clearer approach would be to introduce an
exists()method in theSecretRegistryinterface. This would make the intent of the code more explicit and improve readability.For example, you could change the
registermethod to:This would require adding an
exists()method to theSecretRegistryinterface and its implementations, but it would represent a more idiomatic way to handle this check.