-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support available credentials providers for GCP blob storage #239…
… (#245) Co-authored-by: Aliaksandr Stsiapanay <aliaksandr_stsiapanay@epam.com>
- Loading branch information
1 parent
ef128cf
commit 0f1791e
Showing
9 changed files
with
139 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...l/core/storage/AwsCredentialProvider.java → ...age/credential/AwsCredentialProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...dial/core/storage/CredentialProvider.java → ...torage/credential/CredentialProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/main/java/com/epam/aidial/core/storage/credential/CredentialProviderFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.epam.aidial.core.storage.credential; | ||
|
||
import com.epam.aidial.core.storage.StorageProvider; | ||
import lombok.experimental.UtilityClass; | ||
|
||
@UtilityClass | ||
public class CredentialProviderFactory { | ||
public static CredentialProvider create(String providerName, String identity, String credential) { | ||
StorageProvider provider = StorageProvider.from(providerName); | ||
return switch (provider) { | ||
case S3, AZURE_BLOB -> new DefaultCredentialProvider(identity, credential); | ||
case GOOGLE_CLOUD_STORAGE -> new GcpCredentialProvider(identity, credential); | ||
case FILESYSTEM -> new DefaultCredentialProvider("identity", "credential"); | ||
case AWS_S3 -> new AwsCredentialProvider(identity, credential); | ||
}; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...re/storage/DefaultCredentialProvider.java → ...credential/DefaultCredentialProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/main/java/com/epam/aidial/core/storage/credential/GcpCredentialProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.epam.aidial.core.storage.credential; | ||
|
||
import com.google.auth.oauth2.AccessToken; | ||
import com.google.auth.oauth2.GoogleCredentials; | ||
import com.google.common.io.Files; | ||
import lombok.SneakyThrows; | ||
import org.jclouds.domain.Credentials; | ||
import org.jclouds.googlecloud.GoogleCredentialsFromJson; | ||
|
||
import java.io.File; | ||
import java.time.Instant; | ||
import java.util.Date; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
||
public class GcpCredentialProvider implements CredentialProvider { | ||
|
||
private static final long EXPIRATION_WINDOW_IN_MS = 10_000; | ||
|
||
private Credentials credentials; | ||
|
||
private AccessToken accessToken; | ||
|
||
private GoogleCredentials googleCredentials; | ||
|
||
/** | ||
* | ||
* @param identity client email address | ||
* @param credential could be private key or path to JSON file where the private resides | ||
*/ | ||
@SneakyThrows | ||
public GcpCredentialProvider(String identity, String credential) { | ||
if (identity != null && credential != null) { | ||
// credential is a client email address | ||
this.credentials = new Credentials(identity, credential); | ||
} else if (credential != null) { | ||
// credential is a path to private key JSON file | ||
this.credentials = getCredentialsFromJsonKeyFile(credential); | ||
} else { | ||
// use temporary credential provided by GCP | ||
this.googleCredentials = GoogleCredentials.getApplicationDefault(); | ||
} | ||
} | ||
|
||
@Override | ||
public Credentials getCredentials() { | ||
if (credentials != null) { | ||
return credentials; | ||
} | ||
return getTemporaryCredentials(); | ||
} | ||
|
||
@SneakyThrows | ||
private synchronized Credentials getTemporaryCredentials() { | ||
Date expireAt = Date.from(Instant.ofEpochMilli(System.currentTimeMillis() - EXPIRATION_WINDOW_IN_MS)); | ||
if (accessToken == null || expireAt.after(accessToken.getExpirationTime())) { | ||
accessToken = googleCredentials.refreshAccessToken(); | ||
} | ||
return new Credentials("", accessToken.getTokenValue()); | ||
} | ||
|
||
@SneakyThrows | ||
private static Credentials getCredentialsFromJsonKeyFile(String filename) { | ||
String fileContents = Files.asCharSource(new File(filename), UTF_8).read(); | ||
GoogleCredentialsFromJson credentialSupplier = new GoogleCredentialsFromJson(fileContents); | ||
return credentialSupplier.get(); | ||
} | ||
} |