Skip to content
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

Implement ReloadingKeyStoreAccess #17

Merged
merged 4 commits into from
May 28, 2024
Merged

Conversation

eemhu
Copy link
Contributor

@eemhu eemhu commented May 28, 2024

Allows reloading the KeyStore from disk after cache expiration

@eemhu eemhu requested a review from kortemik May 28, 2024 09:10
@@ -89,23 +89,31 @@ public Boolean load(UserNameAndPassword pe) throws Exception {

}

public PasswordEntry loadKey(final String username) throws UnrecoverableEntryException, KeyStoreException, InvalidKeyException {
public PasswordEntry loadKey(final String username) throws UnrecoverableEntryException, KeyStoreException, InvalidKeyException, ExecutionException {
Copy link
Member

Choose a reason for hiding this comment

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

I think these new ExecutionExceptions that may happen in case loading of the store fails, should be part of the KeyStoreException or other available exceptions that are already in there instead of adding new ones.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

merged ExecutionExceptions into KeyStoreException


CacheLoader<Long, IKeyStoreAccess> cacheLoader = new CacheLoader<Long, IKeyStoreAccess>() {
@Override
public IKeyStoreAccess load(Long key) throws Exception {
Copy link
Member

Choose a reason for hiding this comment

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

if this throws it is always a KeyStoreException of some sort?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

does not throw, removed throws in method declaration

}

public void saveKey(final String username, final char[] password) throws ExecutionException, KeyStoreException {
loadingCache.get(0L).saveKey(username, password);
Copy link
Member

Choose a reason for hiding this comment

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

should catch and throw KeyStoreException?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

caught and thrown as KeyStoreException

cksa.deleteKey(userName);
cksa.saveKey(userName, userPassWord.toCharArray());
Files.deleteIfExists(Paths.get(keyStorePath));
Thread.sleep(1000); // KeyStore refreshes every second
Copy link
Member

Choose a reason for hiding this comment

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

increase to 1200 milliseconds so they do not race

Copy link
Contributor Author

Choose a reason for hiding this comment

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

increased

…s from ReloadingKeyStoreAccess' cacheLoader load() function, increase sleep in tests
@eemhu eemhu requested a review from kortemik May 28, 2024 11:55
import java.security.UnrecoverableEntryException;
import java.security.spec.InvalidKeySpecException;

public interface IKeyStoreAccess {
Copy link
Member

Choose a reason for hiding this comment

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

please rename interface without I and rename the implementation class to KeyStoreAccessImpl

Copy link
Contributor Author

Choose a reason for hiding this comment

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

renamed

Copy link
Member

@kortemik kortemik left a comment

Choose a reason for hiding this comment

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

lgtm

in last commit git diff goes haywire but diff is essentially:

--- KeyStoreAccess.java 2024-05-28 15:59:20.175771463 +0300
+++ KeyStoreAccessImpl.java     2024-05-28 15:58:02.331335951 +0300
@@ -60,17 +60,17 @@
  * and deleting entries. Keeps track of the username->alias mapping
  * via the UserToAliasMapping object.
  */
-public class KeyStoreAccess implements IKeyStoreAccess {
+public class KeyStoreAccessImpl implements KeyStoreAccess {
     private final String keyStorePath;
     private final char[] keyStorePassword;
     private final EntryAliasFactory entryAliasFactory;
     private final UserToAliasMapping userToAliasMapping;
     private final KeyStore keyStore;
 
-    public KeyStoreAccess(final KeyStore keyStore, final String keyStorePath, final char[] keyStorePassword) {
+    public KeyStoreAccessImpl(final KeyStore keyStore, final String keyStorePath, final char[] keyStorePassword) {
         this(keyStore, keyStorePath, keyStorePassword, new EntryAliasFactory());
     }
-    public KeyStoreAccess(final KeyStore keyStore, final String keyStorePath, final char[] keyStorePassword, final EntryAliasFactory entryAliasFactory) {
+    public KeyStoreAccessImpl(final KeyStore keyStore, final String keyStorePath, final char[] keyStorePassword, final EntryAliasFactory entryAliasFactory) {
         this.entryAliasFactory = entryAliasFactory;
         this.keyStorePassword = keyStorePassword;
         this.keyStorePath = keyStorePath;

@kortemik kortemik merged commit 2a98ae8 into teragrep:main May 28, 2024
1 check passed
@kortemik kortemik linked an issue May 28, 2024 that may be closed by this pull request
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.

Implement ReloadingKeyStoreAccess
2 participants