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

SNOW-1895884: Token cache refactor #2044

Open
wants to merge 7 commits into
base: oauth-code-flow
Choose a base branch
from

Conversation

sfc-gh-dheyman
Copy link
Contributor

@sfc-gh-dheyman sfc-gh-dheyman commented Jan 24, 2025

Overview

SNOW-1895884

Pre-review self checklist

  • PR branch is updated with all the changes from master branch
  • The code is correctly formatted (run mvn -P check-style validate)
  • New public API is not unnecessary exposed (run mvn verify and inspect target/japicmp/japicmp.html)
  • The pull request name is prefixed with SNOW-XXXX:
  • Code is in compliance with internal logging requirements

External contributors - please answer these questions before submitting a pull request. Thanks!

  1. What GitHub issue is this PR addressing? Make sure that there is an accompanying issue to your PR.

    Issue: #NNNN

  2. Fill out the following pre-review checklist:

    • I am adding a new automated test(s) to verify correctness of my new code
    • I am adding new logging messages
    • I am modifying authorization mechanisms
    • I am adding new credentials
    • I am modifying OCSP code
    • I am adding a new dependency or upgrading an existing one
    • I am adding new public/protected component not marked with @SnowflakeJdbcInternalApi (note that public/protected methods/fields in classes marked with this annotation are already internal)
  3. Please describe how your code solves the related issue.

    Please write a short description of how your code change solves the related issue.

@sfc-gh-dheyman sfc-gh-dheyman marked this pull request as ready for review January 29, 2025 15:02
@sfc-gh-dheyman sfc-gh-dheyman requested a review from a team as a code owner January 29, 2025 15:02
Map<String, String> hostMap = localCredCache.get(host.toUpperCase());
hostMap.put(SecureStorageManager.convertTarget(host, user, type), token);

localCredCache.computeIfAbsent(CACHE_FILE_TOKENS_OBJECT_NAME, tokensMap -> new HashMap<>());
Copy link
Contributor

Choose a reason for hiding this comment

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

We should read, update and write cache here under one file lock

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated logic

return null;
}

return hostMap.get(SecureStorageManager.convertTarget(host, user, type));
return tokensMap.get(SecureStorageManager.buildCredentialsKey(host, user, type));
}

/** May delete credentials which doesn't belong to this process */
public synchronized SecureStorageStatus deleteCredential(String host, String user, String type) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We should read, delete entry and write cache here under one file lock

Copy link
Contributor Author

Choose a reason for hiding this comment

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

refactored

Copy link

gitguardian bot commented Jan 30, 2025

️✅ There are no secrets present in this pull request anymore.

If these secrets were true positive and are still valid, we highly recommend you to revoke them.
While these secrets were previously flagged, we no longer have a reference to the
specific commits where they were detected. Once a secret has been leaked into a git
repository, you should consider it compromised, even if it was deleted immediately.
Find here more information about risks.


🦉 GitGuardian detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request.

}
for (Iterator<Map.Entry<String, JsonNode>> itr = m.fields(); itr.hasNext(); ) {
for (Iterator<Map.Entry<String, JsonNode>> itr = node.fields(); itr.hasNext(); ) {
Map.Entry<String, JsonNode> hostMap = itr.next();
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we change naming? Those are no longer hosts

private FileCacheManager fileCacheManager;

private final Map<String, Map<String, String>> localCredCache = new HashMap<>();
private final FileCacheManager fileCacheManager;

private SecureStorageLinuxManager() {
fileCacheManager =
FileCacheManager.builder()
.setCacheDirectorySystemProperty(CACHE_DIR_PROP)
.setCacheDirectoryEnvironmentVariable(CACHE_DIR_ENV)
.setBaseCacheFileName(CACHE_FILE_NAME)
Copy link
Contributor

Choose a reason for hiding this comment

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

We should respect XDG_CACHE_HOME

According to XDG specs:

There is a single base directory relative to which user-specific non-essential (cached) data should be written. This directory is defined by the environment variable $XDG_CACHE_HOME.

The design doc:

[Linux only] Directory present in environment var (XDG_CACHE_HOME/snowflake) if set and directory exists or can be created and we can set correct permissions for it (700)

}

/** Reads the cache file. */
JsonNode readCacheFile() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we check permissions for parent directory?

* @return true if success or false
*/
private boolean lockCacheFile() {
return cacheLockFile.mkdirs();
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems that this is the place where we create cache directory if it doesn't exits. I think it's not the best place since:

  • We can create a $HOME directory by mistake and not just .cache/snowflake directory
  • [Less important] We cannot fallback to other directories if we cannot use the one we chose in build function

@@ -230,32 +249,21 @@ JsonNode readCacheFile() {

void writeCacheFile(JsonNode input) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we check permissions for parent directory? should be 0700

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