-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathVaultClient.java
More file actions
219 lines (198 loc) · 9.56 KB
/
VaultClient.java
File metadata and controls
219 lines (198 loc) · 9.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package com.skyflow;
import com.skyflow.config.Credentials;
import com.skyflow.config.VaultConfig;
import com.skyflow.enums.UpsertType;
import com.skyflow.errors.ErrorCode;
import com.skyflow.errors.ErrorMessage;
import com.skyflow.errors.SkyflowException;
import com.skyflow.generated.rest.ApiClient;
import com.skyflow.generated.rest.ApiClientBuilder;
import com.skyflow.generated.rest.resources.recordservice.RecordserviceClient;
import com.skyflow.generated.rest.resources.recordservice.requests.InsertRequest;
import com.skyflow.generated.rest.types.EnumUpdateType;
import com.skyflow.generated.rest.types.InsertRecordData;
import com.skyflow.generated.rest.types.Upsert;
import com.skyflow.logs.InfoLogs;
import com.skyflow.serviceaccount.util.Token;
import com.skyflow.utils.Constants;
import com.skyflow.utils.Utils;
import com.skyflow.utils.logger.LogUtil;
import com.skyflow.utils.validations.Validations;
import com.skyflow.vault.data.DetokenizeRequest;
import com.skyflow.vault.data.InsertRecord;
import io.github.cdimascio.dotenv.Dotenv;
import io.github.cdimascio.dotenv.DotenvException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import java.util.ArrayList;
import java.util.List;
public class VaultClient {
private final VaultConfig vaultConfig;
private final ApiClientBuilder apiClientBuilder;
private ApiClient apiClient;
private Credentials commonCredentials;
private Credentials finalCredentials;
private String token;
private String apiKey;
protected VaultClient(VaultConfig vaultConfig, Credentials credentials) throws SkyflowException {
super();
this.vaultConfig = vaultConfig;
this.commonCredentials = credentials;
this.apiClientBuilder = new ApiClientBuilder();
this.apiClient = null;
updateVaultURL();
}
protected RecordserviceClient getRecordsApi() {
return this.apiClient.recordservice();
}
protected VaultConfig getVaultConfig() {
return vaultConfig;
}
protected void setCommonCredentials(Credentials commonCredentials) throws SkyflowException {
this.commonCredentials = commonCredentials;
prioritiseCredentials();
}
protected void setBearerToken() throws SkyflowException {
prioritiseCredentials();
Validations.validateCredentials(this.finalCredentials);
if (this.finalCredentials.getApiKey() != null) {
LogUtil.printInfoLog(InfoLogs.USE_API_KEY.getLog());
token = this.finalCredentials.getApiKey();
} else if (token == null || token.trim().isEmpty()) {
token = Utils.generateBearerToken(this.finalCredentials);
} else if (Token.isExpired(token)) {
LogUtil.printInfoLog(InfoLogs.BEARER_TOKEN_EXPIRED.getLog());
token = Utils.generateBearerToken(this.finalCredentials);
} else {
LogUtil.printInfoLog(InfoLogs.REUSE_BEARER_TOKEN.getLog());
}
updateExecutorInHTTP(); // update executor
this.apiClient = this.apiClientBuilder.build();
}
private void updateVaultURL() throws SkyflowException {
// Fetch vaultURL from ENV
String vaultURL = Utils.getEnvVaultURL();
// If vaultURL from ENV is null or empty, fetch vaultURL from vault config
if (vaultURL == null || vaultURL.isEmpty()) {
vaultURL = this.vaultConfig.getVaultURL();
}
// If vaultURL from vault config is also null or empty, construct vaultURL from clusterId passed in vault config
if (vaultURL == null || vaultURL.isEmpty()) {
vaultURL = Utils.getVaultURL(this.vaultConfig.getClusterId(), this.vaultConfig.getEnv());
}
this.apiClientBuilder.url(vaultURL);
}
private void prioritiseCredentials() throws SkyflowException {
try {
Credentials original = this.finalCredentials;
if (this.vaultConfig.getCredentials() != null) {
this.finalCredentials = this.vaultConfig.getCredentials();
} else if (this.commonCredentials != null) {
this.finalCredentials = this.commonCredentials;
} else {
String sysCredentials = System.getenv(Constants.ENV_CREDENTIALS_KEY_NAME);
if (sysCredentials == null) {
Dotenv dotenv = Dotenv.load();
sysCredentials = dotenv.get(Constants.ENV_CREDENTIALS_KEY_NAME);
}
if (sysCredentials == null) {
throw new SkyflowException(ErrorCode.INVALID_INPUT.getCode(), ErrorMessage.EmptyCredentials.getMessage());
} else {
this.finalCredentials = new Credentials();
this.finalCredentials.setCredentialsString(sysCredentials);
}
}
if (original != null && !original.equals(this.finalCredentials)) {
token = null;
apiKey = null;
}
} catch (DotenvException e) {
throw new SkyflowException(ErrorCode.INVALID_INPUT.getCode(),
ErrorMessage.EmptyCredentials.getMessage());
}
// catch (Exception e) {
// throw new RuntimeException(e);
// }
}
protected void updateExecutorInHTTP() {
OkHttpClient httpClient = new OkHttpClient.Builder()
.addInterceptor(chain -> {
Request original = chain.request();
Request requestWithAuth = original.newBuilder()
.header("Authorization", "Bearer " + this.token)
.build();
return chain.proceed(requestWithAuth);
})
.build();
apiClientBuilder.httpClient(httpClient);
}
protected InsertRequest getBulkInsertRequestBody(com.skyflow.vault.data.InsertRequest request, VaultConfig config) {
ArrayList<InsertRecord> records = request.getRecords();
List<InsertRecordData> insertRecordDataList = new ArrayList<>();
for (InsertRecord record : records) {
InsertRecordData.Builder data = InsertRecordData.builder();
data.data(record.getData());
if (record.getTable() != null && !record.getTable().isEmpty()) {
data.tableName(record.getTable());
}
if (record.getUpsert() != null && !record.getUpsert().isEmpty()) {
if (record.getUpsertType() != null) {
EnumUpdateType updateType = null;
if (record.getUpsertType() == UpsertType.REPLACE) {
updateType = EnumUpdateType.REPLACE;
} else if (record.getUpsertType() == UpsertType.UPDATE) {
updateType = EnumUpdateType.UPDATE;
}
Upsert upsert = Upsert.builder().uniqueColumns(record.getUpsert()).updateType(updateType).build();
data.upsert(upsert);
} else {
Upsert upsert = Upsert.builder().uniqueColumns(record.getUpsert()).build();
data.upsert(upsert);
}
}
insertRecordDataList.add(data.build());
}
InsertRequest.Builder builder = InsertRequest.builder()
.vaultId(config.getVaultId())
.records(insertRecordDataList);
if (request.getTable() != null && !request.getTable().isEmpty()) {
builder.tableName(request.getTable());
}
if (request.getUpsert() != null && !request.getUpsert().isEmpty()) {
if (request.getUpsertType() != null) {
EnumUpdateType updateType = null;
if (request.getUpsertType() == UpsertType.REPLACE) {
updateType = EnumUpdateType.REPLACE;
} else if (request.getUpsertType() == UpsertType.UPDATE) {
updateType = EnumUpdateType.UPDATE;
}
Upsert upsert = Upsert.builder().uniqueColumns(request.getUpsert()).updateType(updateType).build();
builder.upsert(upsert);
} else {
Upsert upsert = Upsert.builder().uniqueColumns(request.getUpsert()).build();
builder.upsert(upsert);
}
}
return builder.build();
}
protected com.skyflow.generated.rest.resources.recordservice.requests.DetokenizeRequest getDetokenizeRequestBody(DetokenizeRequest request) {
List<String> tokens = request.getTokens();
com.skyflow.generated.rest.resources.recordservice.requests.DetokenizeRequest.Builder builder =
com.skyflow.generated.rest.resources.recordservice.requests.DetokenizeRequest.builder()
.vaultId(this.vaultConfig.getVaultId())
.tokens(tokens);
if (request.getTokenGroupRedactions() != null) {
List<com.skyflow.generated.rest.types.TokenGroupRedactions> tokenGroupRedactionsList = new ArrayList<>();
for (com.skyflow.vault.data.TokenGroupRedactions tokenGroupRedactions : request.getTokenGroupRedactions()) {
com.skyflow.generated.rest.types.TokenGroupRedactions redactions =
com.skyflow.generated.rest.types.TokenGroupRedactions.builder()
.tokenGroupName(tokenGroupRedactions.getTokenGroupName())
.redaction(tokenGroupRedactions.getRedaction())
.build();
tokenGroupRedactionsList.add(redactions);
}
builder.tokenGroupRedactions(tokenGroupRedactionsList);
}
return builder.build();
}
}