Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/com/skyflow/utils/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ public final class Constants {
public static final String SDK_METRIC_RUNTIME_DETAILS_PREFIX = "Java@";
public static final String SDK_AUTH_HEADER_KEY = "x-skyflow-authorization";
public static final String SDK_METRICS_HEADER_KEY = "sky-metadata";
public static final String REQUEST_ID_HEADER_KEY = "x-request-id";
}
24 changes: 17 additions & 7 deletions src/main/java/com/skyflow/vault/controller/VaultController.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import com.skyflow.enums.RedactionType;
import com.skyflow.errors.SkyflowException;
import com.skyflow.generated.rest.ApiException;
import com.skyflow.generated.rest.ApiResponse;
import com.skyflow.generated.rest.models.*;
import com.skyflow.logs.ErrorLogs;
import com.skyflow.logs.InfoLogs;
import com.skyflow.utils.Constants;
import com.skyflow.utils.logger.LogUtil;
import com.skyflow.utils.validations.Validations;
import com.skyflow.vault.data.*;
Expand All @@ -18,6 +20,7 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public final class VaultController extends VaultClient {
private static final Gson gson = new GsonBuilder().serializeNulls().create();
Expand Down Expand Up @@ -111,7 +114,7 @@ private static synchronized HashMap<String, Object> getFormattedQueryRecord(V1Fi
public InsertResponse insert(InsertRequest insertRequest) throws SkyflowException {
LogUtil.printInfoLog(InfoLogs.INSERT_TRIGGERED.getLog());
V1InsertRecordResponse bulkInsertResult = null;
V1BatchOperationResponse batchInsertResult = null;
ApiResponse<V1BatchOperationResponse> batchInsertResult = null;
ArrayList<HashMap<String, Object>> insertedFields = new ArrayList<>();
ArrayList<HashMap<String, Object>> errorFields = new ArrayList<>();
Boolean continueOnError = insertRequest.getContinueOnError();
Expand All @@ -121,15 +124,18 @@ public InsertResponse insert(InsertRequest insertRequest) throws SkyflowExceptio
setBearerToken();
if (continueOnError) {
RecordServiceBatchOperationBody insertBody = super.getBatchInsertRequestBody(insertRequest);
batchInsertResult = super.getRecordsApi().recordServiceBatchOperation(super.getVaultConfig().getVaultId(), insertBody);
batchInsertResult = super.getRecordsApi().recordServiceBatchOperationWithHttpInfo(super.getVaultConfig().getVaultId(), insertBody);
LogUtil.printInfoLog(InfoLogs.INSERT_REQUEST_RESOLVED.getLog());
List<Object> records = batchInsertResult.getResponses();
Map<String, List<String>> responseHeaders = batchInsertResult.getHeaders();
String requestId = responseHeaders.get(Constants.REQUEST_ID_HEADER_KEY).get(0);
List<Object> records = batchInsertResult.getData().getResponses();
for (int index = 0; index < records.size(); index++) {
Object record = records.get(index);
HashMap<String, Object> insertRecord = getFormattedBatchInsertRecord(record, index);
if (insertRecord.containsKey("skyflowId")) {
insertedFields.add(insertRecord);
} else {
insertRecord.put("requestId", requestId);
errorFields.add(insertRecord);
}
}
Expand All @@ -156,23 +162,27 @@ public InsertResponse insert(InsertRequest insertRequest) throws SkyflowExceptio

public DetokenizeResponse detokenize(DetokenizeRequest detokenizeRequest) throws SkyflowException {
LogUtil.printInfoLog(InfoLogs.DETOKENIZE_TRIGGERED.getLog());
V1DetokenizeResponse result = null;
ApiResponse<V1DetokenizeResponse> result = null;
ArrayList<DetokenizeRecordResponse> detokenizedFields = new ArrayList<>();
ArrayList<DetokenizeRecordResponse> errorRecords = new ArrayList<>();
try {
LogUtil.printInfoLog(InfoLogs.VALIDATE_DETOKENIZE_REQUEST.getLog());
Validations.validateDetokenizeRequest(detokenizeRequest);
setBearerToken();
V1DetokenizePayload payload = super.getDetokenizePayload(detokenizeRequest);
result = super.getTokensApi().recordServiceDetokenize(super.getVaultConfig().getVaultId(), payload);
result = super.getTokensApi().recordServiceDetokenizeWithHttpInfo(super.getVaultConfig().getVaultId(), payload);
LogUtil.printInfoLog(InfoLogs.DETOKENIZE_REQUEST_RESOLVED.getLog());
List<V1DetokenizeRecordResponse> records = result.getRecords();
Map<String, List<String>> responseHeaders = result.getHeaders();
String requestId = responseHeaders.get(Constants.REQUEST_ID_HEADER_KEY).get(0);
List<V1DetokenizeRecordResponse> records = result.getData().getRecords();

if (records != null) {
for (V1DetokenizeRecordResponse record : records) {
DetokenizeRecordResponse recordResponse = new DetokenizeRecordResponse(record);
if (record.getError() != null) {
DetokenizeRecordResponse recordResponse = new DetokenizeRecordResponse(record, requestId);
errorRecords.add(recordResponse);
} else {
DetokenizeRecordResponse recordResponse = new DetokenizeRecordResponse(record);
detokenizedFields.add(recordResponse);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ public class DetokenizeRecordResponse {
private final String value;
private final String type;
private final String error;
private final String requestId;

public DetokenizeRecordResponse(V1DetokenizeRecordResponse record) {
this(record, null);
}

public DetokenizeRecordResponse(V1DetokenizeRecordResponse record, String requestId) {
this.token = record.getToken();
this.value = record.getValue().isEmpty() ? null : record.getValue();
this.type = record.getValueType().getValue().equals("NONE") ? null : record.getValueType().getValue();
this.error = record.getError();
this.requestId = requestId;
}

public String getError() {
Expand All @@ -30,4 +36,8 @@ public String getValue() {
public String getType() {
return type;
}

public String getRequestId() {
return requestId;
}
}
10 changes: 8 additions & 2 deletions src/test/java/com/skyflow/vault/data/InsertTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
public class InsertTests {
private static final String INVALID_EXCEPTION_THROWN = "Should not have thrown any exception";
private static final String EXCEPTION_NOT_THROWN = "Should have thrown an exception";
private static final String requestId = "95be08fc-4d13-4335-8b8d-24e85d53ed1d";
private static String vaultID = null;
private static String clusterID = null;
private static String table = null;
Expand Down Expand Up @@ -408,6 +409,11 @@ public void testEmptyValueInTokensInInsertRequestValidations() {
public void testInsertResponse() {
try {
ArrayList<HashMap<String, Object>> errorFields = new ArrayList<>();
HashMap<String, Object> error = new HashMap<>();
error.put("requestIndex", 0);
error.put("requestId", requestId);
error.put("error", "Insert failed");
errorFields.add(error);
values.add(valueMap);
values.add(valueMap);
InsertResponse response = new InsertResponse(values, errorFields);
Expand All @@ -416,9 +422,9 @@ public void testInsertResponse() {
"\"test_column_2\":\"test_value_2\"}," +
"{\"test_column_1\":\"test_value_1\"," +
"\"test_column_2\":\"test_value_2\"}]" +
",\"errors\":" + errorFields + "}";
",\"errors\":[{\"requestIndex\":0,\"requestId\":\"" + requestId + "\",\"error\":\"Insert failed\"}]}";
Assert.assertEquals(2, response.getInsertedFields().size());
Assert.assertTrue(response.getErrors().isEmpty());
Assert.assertEquals(1, response.getErrors().size());
Assert.assertEquals(responseString, response.toString());
} catch (Exception e) {
Assert.fail(INVALID_EXCEPTION_THROWN);
Expand Down
8 changes: 5 additions & 3 deletions src/test/java/com/skyflow/vault/tokens/DetokenizeTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
public class DetokenizeTests {
private static final String INVALID_EXCEPTION_THROWN = "Should not have thrown any exception";
private static final String EXCEPTION_NOT_THROWN = "Should have thrown an exception";
private static final String requestId = "95be08fc-4d13-4335-8b8d-24e85d53ed1d";
private static ArrayList<DetokenizeData> detokenizeData = null;
private static DetokenizeData maskedRedactionRecord = null;
private static DetokenizeData plainRedactionRecord = null;
Expand Down Expand Up @@ -156,7 +157,7 @@ public void testDetokenizeResponse() {
record2.setToken("3456-7890-1234-5678");
record2.setValue("");
record2.setError("Invalid token");
DetokenizeRecordResponse error = new DetokenizeRecordResponse(record2);
DetokenizeRecordResponse error = new DetokenizeRecordResponse(record2, requestId);

ArrayList<DetokenizeRecordResponse> fields = new ArrayList<>();
fields.add(field);
Expand All @@ -170,14 +171,15 @@ public void testDetokenizeResponse() {
String responseString = "{\"detokenizedFields\":[{" +
"\"token\":\"1234-5678-9012-3456\",\"value\":\"4111111111111111\",\"type\":\"STRING\"}," +
"{\"token\":\"1234-5678-9012-3456\",\"value\":\"4111111111111111\",\"type\":\"STRING\"}]," +
"\"errors\":[{\"token\":\"3456-7890-1234-5678\",\"error\":\"Invalid token\"}," +
"{\"token\":\"3456-7890-1234-5678\",\"error\":\"Invalid token\"}]}";
"\"errors\":[{\"token\":\"3456-7890-1234-5678\",\"error\":\"Invalid token\",\"requestId\":\"" + requestId + "\"}," +
"{\"token\":\"3456-7890-1234-5678\",\"error\":\"Invalid token\",\"requestId\":\"" + requestId + "\"}]}";
Assert.assertEquals(2, response.getDetokenizedFields().size());
Assert.assertEquals(2, response.getErrors().size());
Assert.assertEquals("1234-5678-9012-3456", response.getDetokenizedFields().get(0).getToken());
Assert.assertEquals("4111111111111111", response.getDetokenizedFields().get(0).getValue());
Assert.assertEquals("STRING", response.getDetokenizedFields().get(0).getType());
Assert.assertEquals("Invalid token", response.getErrors().get(0).getError());
Assert.assertEquals(requestId, response.getErrors().get(0).getRequestId());
Assert.assertEquals(responseString, response.toString());
} catch (Exception e) {
Assert.fail(INVALID_EXCEPTION_THROWN);
Expand Down