Skip to content

Commit

Permalink
Auth and Encryption error handling (#226)
Browse files Browse the repository at this point in the history
* Update auth and encryption logging

* Update to persistent store
  • Loading branch information
angjordn authored Nov 15, 2024
1 parent d02a95e commit 475850e
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public final class DefaultAuthCredentialsService implements AuthCredentialsServi
private LspEncryptionManager encryptionManager;

private DefaultAuthCredentialsService(final Builder builder) {
this.lspProvider = Objects.requireNonNull(builder.lspProvider, "lspProvider cannot be null");
this.encryptionManager = Objects.requireNonNull(builder.encryptionManager, "encryption manager cannot be null");
this.lspProvider = Objects.requireNonNull(builder.lspProvider, "lspProvider must not be null");
this.encryptionManager = Objects.requireNonNull(builder.encryptionManager, "encryptionManager must not be null");
}

public static Builder builder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ public void toLoggedOut() {
@Override
public void toExpired() {
if (loginType == null || loginType.equals(LoginType.NONE) || loginParams == null) {
Activator.getLogger().error("Attempted to switch to expired state but missing required parameteres for"
+ " re-authentication. Switching to logged out state instead.");
Activator.getLogger().error("Attempted to transition to an expired state but the required parameters for"
+ " re-authentication are not available. Transitioning to a logged out state instead.");
toLoggedOut();
return;
}
Expand Down Expand Up @@ -128,7 +128,11 @@ private void syncAuthStateWithPluginStore() {
String ssoTokenId = authPluginStore.getSsoTokenId();

if (loginType.equals(LoginType.NONE)) {
toLoggedOut();
try {
toLoggedOut();
} catch (Exception ex) {
Activator.getLogger().error("Failed to transition to a logged out state after syncing auth state with the persistent store", ex);
}
return;
}

Expand All @@ -148,8 +152,8 @@ private void syncAuthStateWithPluginStore() {
*/
try {
toLoggedIn(loginType, loginParams, ssoTokenId);
} catch (IllegalArgumentException ex) {
Activator.getLogger().warn("Failed to transition to logged in state when syncing with plugin store: " + ex.getMessage());
} catch (Exception ex) {
Activator.getLogger().error("Failed to transition to a logged in state after syncing auth state with the persistent store", ex);
toLoggedOut();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public final class DefaultAuthTokenService implements AuthTokenService {
private LspProvider lspProvider;

private DefaultAuthTokenService(final Builder builder) {
this.lspProvider = Objects.requireNonNull(builder.lspProvider, "lspProvider cannot be null");
this.lspProvider = Objects.requireNonNull(builder.lspProvider, "lspProvider must not be null");
}

public static Builder builder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public final void start() throws IOException {
DefaultLspEncryptionManager lspEncryption = DefaultLspEncryptionManager.getInstance();
OutputStream serverStdIn = getOutputStream();

lspEncryption.initializeEncrypedCommunication(serverStdIn);
lspEncryption.initializeEncryptedCommunication(serverStdIn);
} catch (Exception e) {
Activator.getLogger().error("Error occured while initializing communication with Amazon Q Lsp Server", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ public static Builder builder() {
public static synchronized DefaultLspEncryptionManager getInstance() {
if (instance == null) {
try {
instance = DefaultLspEncryptionManager.builder()
.build();
instance = DefaultLspEncryptionManager.builder().build();
} catch (Exception e) {
throw new AmazonQPluginException("Failed to initialize LspEncryptionManager", e);
}
Expand All @@ -47,7 +46,7 @@ public String decrypt(final String jwt) {
* has been initiated, it waits for an encryption key to be sent over stdin before initiating the LSP protocol.
* The server saves the provided encryption key and will use the key for future requests (such as Q Chat Requests)
*/
public void initializeEncrypedCommunication(final OutputStream serverStdin) {
public void initializeEncryptedCommunication(final OutputStream serverStdin) {
// Ensure the message does not contain any newline characters. The server will
// process characters up to the first newline.
String message = String.format("""
Expand All @@ -62,7 +61,7 @@ public void initializeEncrypedCommunication(final OutputStream serverStdin) {
serverStdin.write((message + "\n").getBytes());
serverStdin.flush();
} catch (Exception e) {
throw new AmazonQPluginException("Failed to initialize encrypted communication", e);
throw new AmazonQPluginException("Failed to initialize encrypted communication with LSP server", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static SecretKey generateKey() {
keyGen.init(256);
return keyGen.generateKey();
} catch (Exception e) {
throw new AmazonQPluginException("Error occurred while generating lsp encryption key", e);
throw new AmazonQPluginException("Error occurred while generating LSP encryption key", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
public interface LspEncryptionManager {
String encrypt(Object data);
String decrypt(String jwt);
void initializeEncrypedCommunication(OutputStream serverStdin);
void initializeEncryptedCommunication(OutputStream serverStdin);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static String encrypt(final SecretKey encryptionKey, final Object data) {

return jweObject.serialize();
} catch (Exception e) {
throw new AmazonQPluginException("Error occurred while encrypting jwt", e);
throw new AmazonQPluginException("Error occurred while encrypting JWT", e);
}
}

Expand All @@ -43,7 +43,7 @@ public static String decrypt(final SecretKey encryptionKey, final String jwt) {

return jweObject.getPayload().toString();
} catch (Exception e) {
throw new AmazonQPluginException("Error occurred while decrypting jwt", e);
throw new AmazonQPluginException("Error occurred while decrypting JWT", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ void testStartInitializesEncryptedCommunication() throws IOException {
LspEncryptionManager lspEncryptionManagerMock = lspEncryptionManagerStaticMockExtension.getMock(
LspEncryptionManager.class
);
verify(lspEncryptionManagerMock).initializeEncrypedCommunication(mockOutputStream);
verify(lspEncryptionManagerMock).initializeEncryptedCommunication(mockOutputStream);
}

@Test
Expand All @@ -176,7 +176,7 @@ void testStartLogsErrorOnException() throws IOException {
);

doThrow(testException).when(lspEncryptionManagerMock)
.initializeEncrypedCommunication(any());
.initializeEncryptedCommunication(any());

provider.start();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ void testInitializeEncryptedCommunicationGoldenPath() throws IOException {
}\
""", encodedKey);

assertDoesNotThrow(() -> lspEncryptionManager.initializeEncrypedCommunication(outputStream));
assertDoesNotThrow(() -> lspEncryptionManager.initializeEncryptedCommunication(outputStream));
verify(outputStream, times(1)).write((expectedResult + "\n").getBytes());
verify(outputStream, times(1)).flush();
}
Expand All @@ -135,7 +135,7 @@ void testInitializeEncryptedCommunicationException() throws IOException {
.when(outputStream).write(any());

assertThrows(AmazonQPluginException.class,
() -> lspEncryptionManager.initializeEncrypedCommunication(outputStream));
() -> lspEncryptionManager.initializeEncryptedCommunication(outputStream));
}

}

0 comments on commit 475850e

Please sign in to comment.