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

600: Cached Our Private Key Only After Successful Auth with RS #604

Merged
merged 9 commits into from
Oct 25, 2023

Conversation

halprin
Copy link
Member

@halprin halprin commented Oct 24, 2023

Cached Our Private Key Only After Successful Auth with RS

Before this PR, we cached our private key after retrieving it no matter what happened. This prevented us from having to continually call Azure for our secret each time we need to login to ReportStream (which is about every 5 minutes). But, if the key was bad for some reason, we would fail to login to RS in perpetuity until we restarted our application (which would clear the cache). This happened because once we retrieved the secret, we cached it without ever clearing it.

Now, we don't even cache our private key in the first place if we fail to login to RS. We also only cache the key if it wasn't already cached. This prevents us from continuously caching the key whenever we successfully authenticate with a key that was already cached.

Issue

#600.

Checklist

  • I have added tests to cover my changes
  • I have added logging where useful (with appropriate log level)
  • I have added JavaDocs where required
  • I have updated the documentation accordingly

var senderPrivateKey =
"trusted-intermediary-private-key-" + ApplicationContext.getEnvironment();
String key = this.keyCache.get(senderPrivateKey);
String key = keyCache.get(OUR_PRIVATE_KEY_ID);
if (key != null) {
return key;
Copy link
Contributor

Choose a reason for hiding this comment

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

For record keeping. We still need to have a system in place that will change our private key in the cache, when it expires or when it is not valid.

Copy link
Member Author

Choose a reason for hiding this comment

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

Exactly! Thanks for calling that out and adding that item to our engineering task list channel.

token = extractToken(rsResponse);
} catch (Exception e) {
throw new UnableToSendOrderException(
"Error getting the API token from ReportStream", e);
}

// only cache our private key if we successfully authenticate to RS
cachePrivateKeyIfNotCachedAlready(ourPrivateKey);
Copy link
Contributor

@jorg3lopez jorg3lopez Oct 25, 2023

Choose a reason for hiding this comment

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

Great work here. I wonder since cachePrivateKeyIfNotCachedAlready() deals with only our private key, what are your thoughts on renaming it cacheOurPrivateKeyIfNotCachedAlready()?

Copy link
Member Author

Choose a reason for hiding this comment

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

Great idea. I'll do that!

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed.

Copy link
Contributor

@jorg3lopez jorg3lopez left a comment

Choose a reason for hiding this comment

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

Great work. I only had some comments but everything looks good.

@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

100.0% 100.0% Coverage
0.0% 0.0% Duplication

@halprin halprin merged commit 6195a7d into main Oct 25, 2023
15 checks passed
@halprin halprin deleted the task-600-cached_secrets branch October 25, 2023 20:28
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.

3 participants