-
Notifications
You must be signed in to change notification settings - Fork 22
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 Azure blob storage #…
…251 (#255) Co-authored-by: Aliaksandr Stsiapanay <[email protected]>
- Loading branch information
1 parent
711f711
commit a42eb71
Showing
4 changed files
with
81 additions
and
2 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
47 changes: 47 additions & 0 deletions
47
src/main/java/com/epam/aidial/core/storage/credential/AzureCredentialProvider.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,47 @@ | ||
package com.epam.aidial.core.storage.credential; | ||
|
||
import com.azure.core.credential.AccessToken; | ||
import com.azure.core.credential.TokenRequestContext; | ||
import com.azure.identity.DefaultAzureCredential; | ||
import com.azure.identity.DefaultAzureCredentialBuilder; | ||
import org.jclouds.domain.Credentials; | ||
|
||
import java.time.OffsetDateTime; | ||
|
||
public class AzureCredentialProvider implements CredentialProvider { | ||
|
||
private static final long EXPIRATION_WINDOW_IN_SEC = 10; | ||
|
||
private Credentials credentials; | ||
|
||
private DefaultAzureCredential defaultCredential; | ||
|
||
private AccessToken accessToken; | ||
|
||
private TokenRequestContext tokenRequestContext; | ||
|
||
public AzureCredentialProvider(String identity, String secret) { | ||
if (identity != null && secret != null) { | ||
this.credentials = new Credentials(identity, secret); | ||
} else { | ||
defaultCredential = new DefaultAzureCredentialBuilder().build(); | ||
tokenRequestContext = (new TokenRequestContext()).addScopes("https://storage.azure.com/.default"); | ||
} | ||
} | ||
|
||
@Override | ||
public Credentials getCredentials() { | ||
if (credentials != null) { | ||
return credentials; | ||
} | ||
return getTemporaryCredentials(); | ||
} | ||
|
||
private synchronized Credentials getTemporaryCredentials() { | ||
OffsetDateTime date = OffsetDateTime.now().minusSeconds(EXPIRATION_WINDOW_IN_SEC); | ||
if (accessToken == null || date.isAfter(accessToken.getExpiresAt())) { | ||
accessToken = defaultCredential.getTokenSync(tokenRequestContext); | ||
} | ||
return new Credentials("", accessToken.getToken()); | ||
} | ||
} |
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