Repository navigation
docs: SK-3253: Document automatic retry on 401 for expired bearer tokens #450
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
Open
promptless
wants to merge
2
commits into
main
Choose a base branch
from
promptless/sk-3253-bearer-token-401-retry
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
90 changes: 36 additions & 54 deletions
90
skyvault/samples/src/main/java/com/example/serviceaccount/BearerTokenExpiryExample.java
This file contains hidden or 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 |
|---|---|---|
| @@ -1,88 +1,70 @@ | ||
| package com.example.serviceaccount; | ||
|
|
||
| import com.skyflow.Skyflow; | ||
| import com.skyflow.config.ConnectionConfig; | ||
| import com.skyflow.config.Credentials; | ||
| import com.skyflow.config.VaultConfig; | ||
| import com.skyflow.enums.Env; | ||
| import com.skyflow.enums.LogLevel; | ||
| import com.skyflow.enums.RedactionType; | ||
| import com.skyflow.enums.RequestMethod; | ||
| import com.skyflow.errors.SkyflowException; | ||
| import com.skyflow.vault.tokens.DetokenizeData; | ||
| import com.skyflow.vault.tokens.DetokenizeRequest; | ||
| import com.skyflow.vault.tokens.DetokenizeResponse; | ||
| import io.github.cdimascio.dotenv.Dotenv; | ||
| import java.util.ArrayList; | ||
| import com.skyflow.vault.connection.InvokeConnectionRequest; | ||
| import com.skyflow.vault.connection.InvokeConnectionResponse; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * This example demonstrates how to configure and use the Skyflow SDK | ||
| * to detokenize sensitive data stored in a Skyflow vault. | ||
| * It includes setting up credentials, configuring the vault, and | ||
| * making a detokenization request. The code also implements a retry | ||
| * mechanism to handle unauthorized access errors (HTTP 401). | ||
| * This example demonstrates how to handle a bearer token that expires while a | ||
| * connection request is in flight. Vault and Detect calls retry once on their own | ||
| * when this happens, but connection calls return the 401 error, so this example | ||
| * retries the connection call once when it fails with HTTP 401. | ||
| */ | ||
| public class BearerTokenExpiryExample { | ||
| public static void main(String[] args) { | ||
| try { | ||
| // Setting up credentials for accessing the Skyflow vault | ||
| Credentials vaultCredentials = new Credentials(); | ||
| vaultCredentials.setCredentialsString("<YOUR_CREDENTIALS_STRING>"); | ||
| // Let the SDK generate bearer tokens from service account credentials | ||
| Credentials credentials = new Credentials(); | ||
| credentials.setCredentialsString("<YOUR_CREDENTIALS_STRING>"); | ||
|
|
||
| // Configuring the Skyflow vault with necessary details | ||
| VaultConfig vaultConfig = new VaultConfig(); | ||
| vaultConfig.setVaultId("<YOUR_VAULT_ID>"); // Vault ID | ||
| vaultConfig.setClusterId("<YOUR_CLUSTER_ID>"); // Cluster ID | ||
| vaultConfig.setEnv(Env.PROD); // Environment (e.g., DEV, PROD) | ||
| vaultConfig.setCredentials(vaultCredentials); // Setting credentials | ||
| ConnectionConfig connectionConfig = new ConnectionConfig(); | ||
| connectionConfig.setConnectionId("<CONNECTION_ID>"); | ||
| connectionConfig.setConnectionUrl("<CONNECTION_URL>"); | ||
| connectionConfig.setCredentials(credentials); | ||
|
|
||
| // Creating a Skyflow client instance with the configured vault | ||
| Skyflow skyflowClient = Skyflow.builder() | ||
| .setLogLevel(LogLevel.ERROR) // Setting log level to ERROR | ||
| .addVaultConfig(vaultConfig) // Adding vault configuration | ||
| .setLogLevel(LogLevel.ERROR) | ||
| .addConnectionConfig(connectionConfig) | ||
| .build(); | ||
|
|
||
| // Attempting to detokenize data using the Skyflow client | ||
| try { | ||
| detokenizeData(skyflowClient); | ||
| invokeConnection(skyflowClient); | ||
| } catch (SkyflowException e) { | ||
| // Retry detokenization if the error is due to unauthorized access (HTTP 401) | ||
| if (e.getHttpCode() == 401) { | ||
| detokenizeData(skyflowClient); | ||
| // The SDK generates a new bearer token at the start of this call | ||
| invokeConnection(skyflowClient); | ||
| } else { | ||
| // Rethrow the exception for other error codes | ||
| throw e; | ||
| } | ||
| } | ||
| } catch (SkyflowException e) { | ||
| // Handling any exceptions that occur during the process | ||
| System.out.println("An error occurred: " + e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Method to detokenize data using the Skyflow client. | ||
| * It sends a detokenization request with a list of tokens and prints the response. | ||
| * | ||
| * @param skyflowClient The Skyflow client instance used for detokenization. | ||
| * @throws SkyflowException If an error occurs during the detokenization process. | ||
| */ | ||
| public static void detokenizeData(Skyflow skyflowClient) throws SkyflowException { | ||
| // Creating a list of tokens to be detokenized | ||
| DetokenizeData detokenizeDataToken1 = new DetokenizeData("<YOUR_TOKEN_VALUE_1>", RedactionType.PLAIN_TEXT); | ||
| DetokenizeData detokenizeDataToken2 = new DetokenizeData("<YOUR_TOKEN_VALUE_2>"); | ||
| ArrayList<DetokenizeData> detokenizeDataList = new ArrayList<>(); | ||
| detokenizeDataList.add(detokenizeDataToken1); // First token | ||
| detokenizeDataList.add(detokenizeDataToken2); // Second token | ||
| public static void invokeConnection(Skyflow skyflowClient) throws SkyflowException { | ||
| Map<String, String> requestBody = new HashMap<>(); | ||
| requestBody.put("<FIELD_NAME>", "<FIELD_VALUE>"); | ||
|
|
||
| // Building a detokenization request with the token list and configuration | ||
| DetokenizeRequest detokenizeRequest = DetokenizeRequest.builder() | ||
| .detokenizeData(detokenizeDataList) // Adding tokens to the request | ||
| .continueOnError(false) // Stop on error | ||
| .build(); | ||
| Map<String, String> requestHeaders = new HashMap<>(); | ||
| requestHeaders.put("Content-Type", "application/json"); | ||
|
|
||
| // Sending the detokenization request and receiving the response | ||
| DetokenizeResponse detokenizeResponse = skyflowClient.vault().detokenize(detokenizeRequest); | ||
| InvokeConnectionRequest invokeConnectionRequest = InvokeConnectionRequest.builder() | ||
| .method(RequestMethod.POST) | ||
| .requestBody(requestBody) | ||
| .requestHeaders(requestHeaders) | ||
| .build(); | ||
|
|
||
| // Printing the detokenized response | ||
| System.out.println(detokenizeResponse); | ||
| InvokeConnectionResponse invokeConnectionResponse = | ||
| skyflowClient.connection("<CONNECTION_ID>").invoke(invokeConnectionRequest); | ||
| System.out.println(invokeConnectionResponse); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.