-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathVaultClient.java
More file actions
309 lines (282 loc) · 14.4 KB
/
Copy pathVaultClient.java
File metadata and controls
309 lines (282 loc) · 14.4 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package com.skyflow;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
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.flowservice.FlowserviceClient;
import com.skyflow.generated.rest.resources.flowservice.requests.V1InsertRequest;
import com.skyflow.generated.rest.types.FlowEnumUpdateType;
import com.skyflow.generated.rest.types.V1InsertRecordData;
import com.skyflow.generated.rest.types.V1Upsert;
import com.skyflow.logs.InfoLogs;
import com.skyflow.serviceaccount.util.Token;
import com.skyflow.generated.rest.core.RetryInterceptor;
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.DeleteTokensRequest;
import com.skyflow.vault.data.DetokenizeRequest;
import com.skyflow.vault.data.InsertRecord;
import com.skyflow.vault.data.TokenizeRecord;
import com.skyflow.vault.data.TokenizeRequest;
import io.github.cdimascio.dotenv.Dotenv;
import io.github.cdimascio.dotenv.DotenvException;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import okhttp3.Request;
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;
private OkHttpClient sharedHttpClient = null;
private String currentVaultURL = null;
// Client-wide (Skyflow builder) HTTP config defaults; null => fall back to the SDK defaults below.
private Integer commonTimeout;
private Integer commonMaxRetries;
private Long commonInitialRetryDelay;
private Long commonMaxRetryDelay;
// SDK defaults, used when neither the vault-level nor the client-wide value is set.
private static final int DEFAULT_TIMEOUT_SECONDS = 60;
private static final int DEFAULT_MAX_RETRIES = 0; // retries OFF by default (opt-in) so non-idempotent writes aren't auto-retried
private static final long DEFAULT_INITIAL_RETRY_DELAY_MILLIS = 500L;
private static final long DEFAULT_MAX_RETRY_DELAY_MILLIS = 2000L;
protected VaultClient(VaultConfig vaultConfig, Credentials credentials) throws SkyflowException {
super();
this.vaultConfig = vaultConfig;
this.commonCredentials = credentials;
this.apiClientBuilder = new ApiClientBuilder();
this.apiClient = null;
updateVaultURL();
}
protected FlowserviceClient getRecordsApi() {
return this.apiClient.flowservice();
}
protected VaultConfig getVaultConfig() {
return vaultConfig;
}
protected void setCommonCredentials(Credentials commonCredentials) throws SkyflowException {
this.commonCredentials = commonCredentials;
prioritiseCredentials();
}
/**
* Client-wide HTTP timeout/retry defaults from the Skyflow builder. Nulls out the cached client
* so the next call rebuilds with the new values.
*/
protected void setCommonHttpConfig(Integer timeout, Integer maxRetries,
Long initialRetryDelay, Long maxRetryDelay) {
this.commonTimeout = timeout;
this.commonMaxRetries = maxRetries;
this.commonInitialRetryDelay = initialRetryDelay;
this.commonMaxRetryDelay = maxRetryDelay;
this.sharedHttpClient = null;
this.apiClient = null;
}
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());
}
if (apiClient == null) {
updateExecutorInHTTP();
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);
if (!vaultURL.equals(this.currentVaultURL)) {
this.currentVaultURL = vaultURL;
this.apiClient = null;
}
}
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() {
if (sharedHttpClient == null) {
int timeoutSeconds = resolveInt(vaultConfig.getTimeout(), commonTimeout, DEFAULT_TIMEOUT_SECONDS);
int maxRetries = resolveInt(vaultConfig.getMaxRetries(), commonMaxRetries, DEFAULT_MAX_RETRIES);
long initialRetryDelay = resolveLong(
vaultConfig.getInitialRetryDelay(), commonInitialRetryDelay, DEFAULT_INITIAL_RETRY_DELAY_MILLIS);
long maxRetryDelay = resolveLong(
vaultConfig.getMaxRetryDelay(), commonMaxRetryDelay, DEFAULT_MAX_RETRY_DELAY_MILLIS);
sharedHttpClient = new OkHttpClient.Builder()
.connectionPool(new ConnectionPool(10, 1, TimeUnit.MINUTES))
.callTimeout(timeoutSeconds, TimeUnit.SECONDS) // overall ceiling; bounds the whole call incl. retries
.addInterceptor(new RetryInterceptor( // OUTER: retries (Fern generated; jitter default 0.2)
maxRetries, Optional.of(initialRetryDelay), Optional.of(maxRetryDelay), Optional.empty()))
.addInterceptor(chain -> { // INNER: auth (reads this.token per request)
Request requestWithAuth = chain.request().newBuilder()
.header("Authorization", "Bearer " + this.token)
.build();
return chain.proceed(requestWithAuth);
})
.build();
apiClientBuilder.httpClient(sharedHttpClient);
}
}
/** Resolve an int setting: vault-level override, else client-wide default, else SDK default. */
private static int resolveInt(Integer vaultLevel, Integer clientLevel, int defaultValue) {
if (vaultLevel != null) {
return vaultLevel;
}
return clientLevel != null ? clientLevel : defaultValue;
}
/** Resolve a long setting: vault-level override, else client-wide default, else SDK default. */
private static long resolveLong(Long vaultLevel, Long clientLevel, long defaultValue) {
if (vaultLevel != null) {
return vaultLevel;
}
return clientLevel != null ? clientLevel : defaultValue;
}
protected V1InsertRequest getBulkInsertRequestBody(com.skyflow.vault.data.InsertRequest request, VaultConfig config) {
ArrayList<InsertRecord> records = request.getRecords();
List<V1InsertRecordData> insertRecordDataList = new ArrayList<>();
for (InsertRecord record : records) {
V1InsertRecordData.Builder data = V1InsertRecordData.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) {
FlowEnumUpdateType updateType = null;
if (record.getUpsertType() == UpsertType.REPLACE) {
updateType = FlowEnumUpdateType.REPLACE;
} else if (record.getUpsertType() == UpsertType.UPDATE) {
updateType = FlowEnumUpdateType.UPDATE;
}
V1Upsert upsert = V1Upsert.builder().uniqueColumns(record.getUpsert()).updateType(updateType).build();
data.upsert(upsert);
} else {
V1Upsert upsert = V1Upsert.builder().uniqueColumns(record.getUpsert()).build();
data.upsert(upsert);
}
}
insertRecordDataList.add(data.build());
}
V1InsertRequest.Builder builder = V1InsertRequest.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) {
FlowEnumUpdateType updateType = null;
if (request.getUpsertType() == UpsertType.REPLACE) {
updateType = FlowEnumUpdateType.REPLACE;
} else if (request.getUpsertType() == UpsertType.UPDATE) {
updateType = FlowEnumUpdateType.UPDATE;
}
V1Upsert upsert = V1Upsert.builder().uniqueColumns(request.getUpsert()).updateType(updateType).build();
builder.upsert(upsert);
} else {
V1Upsert upsert = V1Upsert.builder().uniqueColumns(request.getUpsert()).build();
builder.upsert(upsert);
}
}
return builder.build();
}
protected com.skyflow.generated.rest.resources.flowservice.requests.V1FlowDetokenizeRequest getDetokenizeRequestBody(DetokenizeRequest request) {
List<String> tokens = request.getTokens();
com.skyflow.generated.rest.resources.flowservice.requests.V1FlowDetokenizeRequest.Builder builder =
com.skyflow.generated.rest.resources.flowservice.requests.V1FlowDetokenizeRequest.builder()
.vaultId(this.vaultConfig.getVaultId())
.tokens(tokens);
if (request.getTokenGroupRedactions() != null) {
List<com.skyflow.generated.rest.types.V1TokenGroupRedactions> tokenGroupRedactionsList = new ArrayList<>();
for (com.skyflow.vault.data.TokenGroupRedactions tokenGroupRedactions : request.getTokenGroupRedactions()) {
com.skyflow.generated.rest.types.V1TokenGroupRedactions redactions =
com.skyflow.generated.rest.types.V1TokenGroupRedactions.builder()
.tokenGroupName(tokenGroupRedactions.getTokenGroupName())
.redaction(tokenGroupRedactions.getRedaction())
.build();
tokenGroupRedactionsList.add(redactions);
}
builder.tokenGroupRedactions(tokenGroupRedactionsList);
}
return builder.build();
}
protected com.skyflow.generated.rest.resources.flowservice.requests.V1FlowDeleteTokenRequest getDeleteTokensRequestBody(DeleteTokensRequest request) {
return com.skyflow.generated.rest.resources.flowservice.requests.V1FlowDeleteTokenRequest.builder()
.vaultId(this.vaultConfig.getVaultId())
.tokens(request.getTokens())
.build();
}
protected com.skyflow.generated.rest.resources.flowservice.requests.V1FlowTokenizeRequest getTokenizeRequestBody(TokenizeRequest request) {
List<com.skyflow.generated.rest.types.V1FlowTokenizeRequestObject> dataList = new ArrayList<>();
for (TokenizeRecord record : request.getData()) {
com.skyflow.generated.rest.types.V1FlowTokenizeRequestObject obj =
com.skyflow.generated.rest.types.V1FlowTokenizeRequestObject.builder()
.value(record.getValue())
.tokenGroupNames(record.getTokenGroupNames())
.build();
dataList.add(obj);
}
return com.skyflow.generated.rest.resources.flowservice.requests.V1FlowTokenizeRequest.builder()
.vaultId(this.vaultConfig.getVaultId())
.data(dataList)
.build();
}
}