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
47 changes: 43 additions & 4 deletions src/main/java/com/skyflow/Skyflow.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.skyflow.utils.logger.LogUtil;
import com.skyflow.utils.validations.Validations;
import com.skyflow.vault.controller.ConnectionController;
import com.skyflow.vault.controller.DetectController;
import com.skyflow.vault.controller.VaultController;

import java.util.LinkedHashMap;
Expand Down Expand Up @@ -101,25 +102,57 @@ public VaultController vault(String vaultId) throws SkyflowException {
return controller;
}

public ConnectionController connection() {
String connectionId = (String) this.builder.connectionsMap.keySet().toArray()[0];

public ConnectionController connection() throws SkyflowException {
Object[] array = this.builder.connectionsMap.keySet().toArray();
if (array.length < 1) {
LogUtil.printErrorLog(ErrorLogs.CONNECTION_CONFIG_DOES_NOT_EXIST.getLog());
throw new SkyflowException(ErrorCode.INVALID_INPUT.getCode(), ErrorMessage.ConnectionIdNotInConfigList.getMessage());
}
String connectionId = (String) array[0];
return this.connection(connectionId);
}

public ConnectionController connection(String connectionId) {
return this.builder.connectionsMap.get(connectionId);
public ConnectionController connection(String connectionId) throws SkyflowException {
ConnectionController controller = this.builder.connectionsMap.get(connectionId);
if (controller == null) {
LogUtil.printErrorLog(ErrorLogs.CONNECTION_CONFIG_DOES_NOT_EXIST.getLog());
throw new SkyflowException(ErrorCode.INVALID_INPUT.getCode(), ErrorMessage.ConnectionIdNotInConfigList.getMessage());
}
return controller;
}

public DetectController detect() throws SkyflowException {
Object[] array = this.builder.detectClientsMap.keySet().toArray();
if (array.length < 1) {
LogUtil.printErrorLog(ErrorLogs.VAULT_CONFIG_DOES_NOT_EXIST.getLog());
throw new SkyflowException(ErrorCode.INVALID_INPUT.getCode(), ErrorMessage.VaultIdNotInConfigList.getMessage());
}
String detectId = (String) array[0];
return this.detect(detectId);
}

public DetectController detect(String vaultId) throws SkyflowException {
DetectController controller = this.builder.detectClientsMap.get(vaultId);
if (controller == null) {
LogUtil.printErrorLog(ErrorLogs.VAULT_CONFIG_DOES_NOT_EXIST.getLog());
throw new SkyflowException(ErrorCode.INVALID_INPUT.getCode(), ErrorMessage.VaultIdNotInConfigList.getMessage());
}
return controller;
}

public static final class SkyflowClientBuilder {
private final LinkedHashMap<String, ConnectionController> connectionsMap;
private final LinkedHashMap<String, VaultController> vaultClientsMap;
private final LinkedHashMap<String, DetectController> detectClientsMap;
private final LinkedHashMap<String, VaultConfig> vaultConfigMap;
private final LinkedHashMap<String, ConnectionConfig> connectionConfigMap;
private Credentials skyflowCredentials;
private LogLevel logLevel;

public SkyflowClientBuilder() {
this.vaultClientsMap = new LinkedHashMap<>();
this.detectClientsMap = new LinkedHashMap<>();
this.vaultConfigMap = new LinkedHashMap<>();
this.connectionsMap = new LinkedHashMap<>();
this.connectionConfigMap = new LinkedHashMap<>();
Expand All @@ -139,8 +172,11 @@ public SkyflowClientBuilder addVaultConfig(VaultConfig vaultConfig) throws Skyfl
} else {
this.vaultConfigMap.put(vaultConfig.getVaultId(), vaultConfig);
this.vaultClientsMap.put(vaultConfig.getVaultId(), new VaultController(vaultConfig, this.skyflowCredentials));
this.detectClientsMap.put(vaultConfig.getVaultId(), new DetectController(vaultConfig, this.skyflowCredentials));
LogUtil.printInfoLog(Utils.parameterizedString(
InfoLogs.VAULT_CONTROLLER_INITIALIZED.getLog(), vaultConfig.getVaultId()));
LogUtil.printInfoLog(Utils.parameterizedString(
InfoLogs.DETECT_CONTROLLER_INITIALIZED.getLog(), vaultConfig.getVaultId()));
}
return this;
}
Expand Down Expand Up @@ -226,6 +262,9 @@ public SkyflowClientBuilder addSkyflowCredentials(Credentials credentials) throw
for (VaultController vault : this.vaultClientsMap.values()) {
vault.setCommonCredentials(this.skyflowCredentials);
}
for (DetectController detect : this.detectClientsMap.values()) {
detect.setCommonCredentials(this.skyflowCredentials);
}
for (ConnectionController connection : this.connectionsMap.values()) {
connection.setCommonCredentials(this.skyflowCredentials);
}
Expand Down
179 changes: 176 additions & 3 deletions src/main/java/com/skyflow/VaultClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.skyflow.config.Credentials;
import com.skyflow.config.VaultConfig;
import com.skyflow.enums.DetectEntities;
import com.skyflow.errors.ErrorCode;
import com.skyflow.errors.ErrorMessage;
import com.skyflow.errors.SkyflowException;
Expand All @@ -12,10 +13,15 @@
import com.skyflow.generated.rest.resources.records.requests.RecordServiceBatchOperationBody;
import com.skyflow.generated.rest.resources.records.requests.RecordServiceInsertRecordBody;
import com.skyflow.generated.rest.resources.records.requests.RecordServiceUpdateRecordBody;
import com.skyflow.generated.rest.resources.strings.StringsClient;
import com.skyflow.generated.rest.resources.strings.requests.DeidentifyStringRequest;
import com.skyflow.generated.rest.resources.strings.requests.ReidentifyStringRequest;
import com.skyflow.generated.rest.resources.strings.types.ReidentifyStringRequestFormat;
import com.skyflow.generated.rest.resources.tokens.TokensClient;
import com.skyflow.generated.rest.resources.tokens.requests.V1DetokenizePayload;
import com.skyflow.generated.rest.resources.tokens.requests.V1TokenizePayload;
import com.skyflow.generated.rest.types.*;
import com.skyflow.generated.rest.types.Transformations;
import com.skyflow.logs.InfoLogs;
import com.skyflow.serviceaccount.util.Token;
import com.skyflow.utils.Constants;
Expand All @@ -24,16 +30,17 @@
import com.skyflow.utils.validations.Validations;
import com.skyflow.vault.data.InsertRequest;
import com.skyflow.vault.data.UpdateRequest;
import com.skyflow.vault.detect.*;
import com.skyflow.vault.tokens.ColumnValue;
import com.skyflow.vault.tokens.DetokenizeData;
import com.skyflow.vault.tokens.DetokenizeRequest;
import com.skyflow.vault.tokens.TokenizeRequest;
import io.github.cdimascio.dotenv.Dotenv;
import io.github.cdimascio.dotenv.DotenvException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;


public class VaultClient {
private final VaultConfig vaultConfig;
Expand Down Expand Up @@ -61,6 +68,10 @@ protected TokensClient getTokensApi() {
return this.apiClient.tokens();
}

protected StringsClient getDetectTextApi() {
return this.apiClient.strings();
}

protected QueryClient getQueryApi() {
return this.apiClient.query();
}
Expand Down Expand Up @@ -209,6 +220,168 @@ protected void setBearerToken() throws SkyflowException {
this.apiClient = this.apiClientBuilder.build();
}

protected DeidentifyTextResponse getDeIdentifyTextResponse(DeidentifyStringResponse deidentifyStringResponse) {
List<EntityInfo> entities = deidentifyStringResponse.getEntities() != null
? deidentifyStringResponse.getEntities().stream()
.map(this::convertDetectedEntityToEntityInfo)
.collect(Collectors.toList())
: null;

return new DeidentifyTextResponse(
deidentifyStringResponse.getProcessedText(),
entities,
deidentifyStringResponse.getWordCount(),
deidentifyStringResponse.getCharacterCount()
);
}

protected DeidentifyStringRequest getDeidentifyStringRequest(DeidentifyTextRequest deIdentifyTextRequest, String vaultId) throws SkyflowException {
List<DetectEntities> entities = deIdentifyTextRequest.getEntities();

List<EntityType> mappedEntityTypes = null;
if (entities != null) {
mappedEntityTypes = deIdentifyTextRequest.getEntities().stream()
.map(detectEntity -> EntityType.valueOf(detectEntity.name()))
.collect(Collectors.toList());
}

TokenFormat tokenFormat = deIdentifyTextRequest.getTokenFormat();

Optional<List<EntityType>> vaultToken = Optional.empty();
Optional<List<EntityType>> entityTypes = Optional.empty();
Optional<List<EntityType>> entityUniqueCounter = Optional.empty();
Optional<List<String>> allowRegex = Optional.ofNullable(deIdentifyTextRequest.getAllowRegexList());
Optional<List<String>> restrictRegex = Optional.ofNullable(deIdentifyTextRequest.getRestrictRegexList());
Optional<Transformations> transformations = Optional.ofNullable(getTransformations(deIdentifyTextRequest.getTransformations()));

if (tokenFormat != null) {
if (tokenFormat.getVaultToken() != null && !tokenFormat.getVaultToken().isEmpty()) {
vaultToken = Optional.of(tokenFormat.getVaultToken().stream()
.map(detectEntity -> EntityType.valueOf(detectEntity.name()))
.collect(Collectors.toList()));
}

if (tokenFormat.getEntityOnly() != null && !tokenFormat.getEntityOnly().isEmpty()) {
entityTypes = Optional.of(tokenFormat.getEntityOnly().stream()
.map(detectEntity -> EntityType.valueOf(detectEntity.name()))
.collect(Collectors.toList()));
}

if (tokenFormat.getEntityUniqueCounter() != null && !tokenFormat.getEntityUniqueCounter().isEmpty()) {
entityUniqueCounter = Optional.of(tokenFormat.getEntityUniqueCounter().stream()
.map(detectEntity -> EntityType.valueOf(detectEntity.name()))
.collect(Collectors.toList()));
}
}

TokenType tokenType = TokenType.builder()
.vaultToken(vaultToken)
.entityOnly(entityTypes)
.entityUnqCounter(entityUniqueCounter)
.build();


return DeidentifyStringRequest.builder()
.vaultId(vaultId)
.text(deIdentifyTextRequest.getText())
.entityTypes(mappedEntityTypes)
.tokenType(tokenType)
.allowRegex(allowRegex)
.restrictRegex(restrictRegex)
.transformations(transformations)
.build();
}

protected ReidentifyStringRequest getReidentifyStringRequest(ReidentifyTextRequest reidentifyTextRequest, String vaultId) throws SkyflowException {
List<EntityType> maskEntities = null;
List<EntityType> redactedEntities = null;
List<EntityType> plaintextEntities = null;

if (reidentifyTextRequest.getMaskedEntities() != null) {
maskEntities = reidentifyTextRequest.getMaskedEntities().stream()
.map(detectEntity -> EntityType.valueOf(detectEntity.name()))
.collect(Collectors.toList());
}

if (reidentifyTextRequest.getPlainTextEntities() != null) {
plaintextEntities = reidentifyTextRequest.getPlainTextEntities().stream()
.map(detectEntity -> EntityType.valueOf(detectEntity.name()))
.collect(Collectors.toList());
}

if (reidentifyTextRequest.getRedactedEntities() != null) {
redactedEntities = reidentifyTextRequest.getRedactedEntities().stream()
.map(detectEntity -> EntityType.valueOf(detectEntity.name()))
.collect(Collectors.toList());
}

ReidentifyStringRequestFormat reidentifyStringRequestFormat = ReidentifyStringRequestFormat.builder()
.masked(maskEntities)
.plaintext(plaintextEntities)
.redacted(redactedEntities)
.build();


return ReidentifyStringRequest.builder()
.text(reidentifyTextRequest.getText())
.vaultId(vaultId)
.format(reidentifyStringRequestFormat)
.build();
}


private EntityInfo convertDetectedEntityToEntityInfo(DetectedEntity detectedEntity) {
TextIndex textIndex = new TextIndex(
detectedEntity.getLocation().get().getStartIndex().orElse(0),
detectedEntity.getLocation().get().getEndIndex().orElse(0)
);
TextIndex processedIndex = new TextIndex(
detectedEntity.getLocation().get().getStartIndexProcessed().orElse(0),
detectedEntity.getLocation().get().getEndIndexProcessed().orElse(0)
);

Map<String, Float> entityScores = detectedEntity.getEntityScores()
.map(doubleMap -> doubleMap.entrySet().stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
entry -> entry.getValue().floatValue()
)))
.orElse(Collections.emptyMap());


return new EntityInfo(
detectedEntity.getToken().orElse(""),
detectedEntity.getValue().orElse(""),
textIndex,
processedIndex,
detectedEntity.getEntityType().orElse(""),
entityScores);
}


private Transformations getTransformations(com.skyflow.vault.detect.Transformations transformations) {
if (transformations == null || transformations.getShiftDates() == null) {
return null;
}

List<TransformationsShiftDatesEntityTypesItem> entityTypes = null;
if (!transformations.getShiftDates().getEntities().isEmpty()) {
entityTypes = transformations.getShiftDates().getEntities().stream()
.map(entity -> TransformationsShiftDatesEntityTypesItem.valueOf(entity.name()))
.collect(Collectors.toList());
} else {
entityTypes = Collections.emptyList();
}

return Transformations.builder()
.shiftDates(TransformationsShiftDates.builder()
.maxDays(transformations.getShiftDates().getMax())
.minDays(transformations.getShiftDates().getMin())
.entityTypes(entityTypes)
.build())
.build();
}

private void setApiKey() {
if (apiKey == null) {
apiKey = this.finalCredentials.getApiKey();
Expand Down
Loading
Loading