-
Notifications
You must be signed in to change notification settings - Fork 872
/
AirflowRESTClient.java
515 lines (470 loc) · 21.3 KB
/
AirflowRESTClient.java
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/*
* Copyright 2021 Collate
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openmetadata.service.clients.pipeline.airflow;
import com.fasterxml.jackson.core.type.TypeReference;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.security.KeyStoreException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.net.ssl.SSLContext;
import javax.ws.rs.core.Response;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.utils.URIBuilder;
import org.json.JSONObject;
import org.openmetadata.schema.ServiceEntityInterface;
import org.openmetadata.schema.api.configuration.pipelineServiceClient.PipelineServiceClientConfiguration;
import org.openmetadata.schema.entity.app.App;
import org.openmetadata.schema.entity.app.AppMarketPlaceDefinition;
import org.openmetadata.schema.entity.automations.Workflow;
import org.openmetadata.schema.entity.services.ingestionPipelines.IngestionPipeline;
import org.openmetadata.schema.entity.services.ingestionPipelines.PipelineServiceClientResponse;
import org.openmetadata.schema.entity.services.ingestionPipelines.PipelineStatus;
import org.openmetadata.sdk.PipelineServiceClient;
import org.openmetadata.sdk.exception.PipelineServiceClientException;
import org.openmetadata.service.exception.IngestionPipelineDeploymentException;
import org.openmetadata.service.util.JsonUtils;
import org.openmetadata.service.util.SSLUtil;
@Slf4j
public class AirflowRESTClient extends PipelineServiceClient {
private static final String PLATFORM = "Airflow";
private static final String USERNAME_KEY = "username";
private static final String PASSWORD_KEY = "password";
private static final String TIMEOUT_KEY = "timeout";
private static final String TRUSTSTORE_PATH_KEY = "truststorePath";
private static final String TRUSTSTORE_PASSWORD_KEY = "truststorePassword";
private static final String DOCS_LINK =
"Follow [this guide](https://docs.open-metadata.org/deployment/ingestion/openmetadata) for further details.";
protected final String username;
protected final String password;
protected final HttpClient client;
protected final URL serviceURL;
private static final List<String> API_ENDPOINT_SEGMENTS = List.of("api", "v1", "openmetadata");
private static final String DAG_ID = "dag_id";
public AirflowRESTClient(PipelineServiceClientConfiguration config) throws KeyStoreException {
super(config);
this.setPlatform(PLATFORM);
this.username = (String) config.getParameters().getAdditionalProperties().get(USERNAME_KEY);
this.password = (String) config.getParameters().getAdditionalProperties().get(PASSWORD_KEY);
this.serviceURL = validateServiceURL(config.getApiEndpoint());
SSLContext sslContext = createAirflowSSLContext(config);
HttpClient.Builder clientBuilder =
HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1)
.connectTimeout(
Duration.ofSeconds(
(Integer) config.getParameters().getAdditionalProperties().get(TIMEOUT_KEY)));
if (sslContext == null) {
this.client = clientBuilder.build();
} else {
this.client = clientBuilder.sslContext(sslContext).build();
}
}
private static SSLContext createAirflowSSLContext(PipelineServiceClientConfiguration config)
throws KeyStoreException {
String truststorePath =
(String) config.getParameters().getAdditionalProperties().get(TRUSTSTORE_PATH_KEY);
String truststorePassword =
(String) config.getParameters().getAdditionalProperties().get(TRUSTSTORE_PASSWORD_KEY);
return SSLUtil.createSSLContext(truststorePath, truststorePassword, PLATFORM);
}
public final HttpResponse<String> post(String endpoint, String payload, boolean authenticate)
throws IOException, InterruptedException {
HttpRequest.Builder requestBuilder =
HttpRequest.newBuilder(URI.create(endpoint))
.header(CONTENT_HEADER, CONTENT_TYPE)
.POST(HttpRequest.BodyPublishers.ofString(payload));
if (authenticate) {
requestBuilder.header(AUTH_HEADER, getBasicAuthenticationHeader(username, password));
}
return client.send(requestBuilder.build(), HttpResponse.BodyHandlers.ofString());
}
public final HttpResponse<String> post(String endpoint, String payload)
throws IOException, InterruptedException {
return post(endpoint, payload, true);
}
@Override
public PipelineServiceClientResponse deployPipeline(
IngestionPipeline ingestionPipeline, ServiceEntityInterface service) {
HttpResponse<String> response;
try {
String deployUrl = buildURI("deploy").build().toString();
String pipelinePayload = JsonUtils.pojoToJson(ingestionPipeline);
response = post(deployUrl, pipelinePayload);
if (response.statusCode() == 200) {
ingestionPipeline.setDeployed(true);
return getResponse(200, response.body());
}
} catch (IOException | URISyntaxException e) {
throw IngestionPipelineDeploymentException.byMessage(
ingestionPipeline.getName(), DEPLOYMENT_ERROR, e.getMessage());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw IngestionPipelineDeploymentException.byMessage(
ingestionPipeline.getName(), DEPLOYMENT_ERROR, e.getMessage());
}
throw new PipelineServiceClientException(
String.format(
"%s Failed to deploy Ingestion Pipeline due to airflow API returned %s and response %s",
ingestionPipeline.getName(),
Response.Status.fromStatusCode(response.statusCode()),
response.body()));
}
@Override
public PipelineServiceClientResponse deletePipeline(IngestionPipeline ingestionPipeline) {
String pipelineName = ingestionPipeline.getName();
HttpResponse<String> response;
try {
URIBuilder uri = buildURI("delete");
uri.addParameter(DAG_ID, pipelineName);
response = deleteRequestAuthenticatedForJsonContent(uri.build().toString());
if (response.statusCode() == 200) {
return getResponse(200, response.body());
}
} catch (IOException | URISyntaxException e) {
LOG.error(
String.format("Failed to delete Airflow Pipeline %s from Airflow DAGS", pipelineName));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
LOG.error(
String.format("Failed to delete Airflow Pipeline %s from Airflow DAGS", pipelineName));
}
return getResponse(
500, String.format("Failed to delete Airflow Pipeline %s from Airflow DAGS", pipelineName));
}
@Override
public PipelineServiceClientResponse runPipeline(
IngestionPipeline ingestionPipeline, ServiceEntityInterface service) {
String pipelineName = ingestionPipeline.getName();
HttpResponse<String> response;
try {
String triggerUrl = buildURI("trigger").build().toString();
JSONObject requestPayload = new JSONObject();
requestPayload.put(DAG_ID, pipelineName);
response = post(triggerUrl, requestPayload.toString());
if (response.statusCode() == 200) {
return getResponse(200, response.body());
}
} catch (IOException | URISyntaxException e) {
throw IngestionPipelineDeploymentException.byMessage(
pipelineName, TRIGGER_ERROR, e.getMessage());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw IngestionPipelineDeploymentException.byMessage(
pipelineName, TRIGGER_ERROR, e.getMessage());
}
throw IngestionPipelineDeploymentException.byMessage(
pipelineName,
TRIGGER_ERROR,
"Failed to trigger IngestionPipeline",
Response.Status.fromStatusCode(response.statusCode()));
}
@Override
public PipelineServiceClientResponse toggleIngestion(IngestionPipeline ingestionPipeline) {
HttpResponse<String> response;
try {
String toggleUrl;
JSONObject requestPayload = new JSONObject();
requestPayload.put(DAG_ID, ingestionPipeline.getName());
// If the pipeline is currently enabled, disable it
if (ingestionPipeline.getEnabled().equals(Boolean.TRUE)) {
toggleUrl = buildURI("disable").build().toString();
response = post(toggleUrl, requestPayload.toString());
if (response.statusCode() == 200) {
ingestionPipeline.setEnabled(false);
return getResponse(200, response.body());
} else if (response.statusCode() == 404) {
ingestionPipeline.setDeployed(false);
return getResponse(404, response.body());
}
// otherwise, enable it back
} else {
toggleUrl = buildURI("enable").build().toString();
response = post(toggleUrl, requestPayload.toString());
if (response.statusCode() == 200) {
ingestionPipeline.setEnabled(true);
ingestionPipeline.setEnabled(false);
} else if (response.statusCode() == 404) {
ingestionPipeline.setDeployed(false);
return getResponse(404, response.body());
}
}
} catch (IOException | URISyntaxException e) {
throw clientException(ingestionPipeline, e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw clientException(ingestionPipeline, e);
}
throw clientException(ingestionPipeline, "Failed to toggle ingestion pipeline state", response);
}
@Override
public List<PipelineStatus> getQueuedPipelineStatusInternal(IngestionPipeline ingestionPipeline) {
HttpResponse<String> response;
try {
URIBuilder uri = buildURI("status");
uri.addParameter(DAG_ID, ingestionPipeline.getName());
uri.addParameter("only_queued", "true");
response = getRequestAuthenticatedForJsonContent(uri.build().toString());
if (response.statusCode() == 200) {
return JsonUtils.readObjects(response.body(), PipelineStatus.class);
}
} catch (IOException | URISyntaxException e) {
throw clientException(ingestionPipeline, e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw clientException(ingestionPipeline, e);
}
// Return an empty list. We'll just show the stored status from the Ingestion Pipeline
LOG.error(
String.format(
"Got status code [%s] trying to get queued statuses: [%s]",
response.statusCode(), response.body()));
return new ArrayList<>();
}
/**
* Scenarios handled here: 1. Failed to access Airflow APIs: No response from Airflow; APIs might not be installed 2.
* Auth failed when accessing Airflow APIs 3. Different versions between server and client
*/
@Override
public PipelineServiceClientResponse getServiceStatusInternal() {
HttpResponse<String> response;
try {
String healthUrl = buildURI("health-auth").build().toString();
response = getRequestAuthenticatedForJsonContent(healthUrl);
// We can reach the APIs and get the status back from Airflow
if (response.statusCode() == 200) {
JSONObject responseJSON = new JSONObject(response.body());
String ingestionVersion = responseJSON.getString("version");
return Boolean.TRUE.equals(validServerClientVersions(ingestionVersion))
? buildHealthyStatus(ingestionVersion)
: buildUnhealthyStatus(
buildVersionMismatchErrorMessage(ingestionVersion, SERVER_VERSION));
}
// Auth error when accessing the APIs
if (response.statusCode() == 401 || response.statusCode() == 403) {
return buildUnhealthyStatus(
String.format(
"Authentication failed for user [%s] trying to access the Airflow APIs.",
this.username));
}
// APIs URL not found
if (response.statusCode() == 404) {
return buildUnhealthyStatus(
String.format(
"Airflow APIs not found. Please validate if the OpenMetadata Airflow plugin is installed correctly. %s",
DOCS_LINK));
}
return buildUnhealthyStatus(
String.format(
"Unexpected status response: code [%s] - [%s]",
response.statusCode(), response.body()));
} catch (IOException | URISyntaxException e) {
String exceptionMsg;
if (e.getMessage() != null) {
exceptionMsg = String.format("Failed to get Airflow status due to [%s].", e.getMessage());
} else {
exceptionMsg =
String.format(
"Failed to connect to Airflow due to %s. Is the host available at %s?",
e.getCause().toString(), serviceURL.toString());
}
return buildUnhealthyStatus(String.format("%s %s", exceptionMsg, DOCS_LINK));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return buildUnhealthyStatus(
String.format(
"Failed to connect to Airflow due to %s. Is the host available at %s? %s.",
e.getMessage(), serviceURL.toString(), DOCS_LINK));
}
}
@Override
public PipelineServiceClientResponse runAutomationsWorkflow(Workflow workflow) {
HttpResponse<String> response;
try {
String automationsUrl = buildURI("run_automation").build().toString();
String workflowPayload = JsonUtils.pojoToJson(workflow);
response = post(automationsUrl, workflowPayload);
if (response.statusCode() == 200) {
return getResponse(200, response.body());
}
} catch (IOException | URISyntaxException e) {
// We can end up here if the test connection is not sending back anything after the POST
// request
// due to the connection to the source service not being properly resolved.
throw IngestionPipelineDeploymentException.byMessage(
workflow.getName(),
TRIGGER_ERROR,
"No response from the test connection. Make sure your service is reachable and accepting connections");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw IngestionPipelineDeploymentException.byMessage(
workflow.getName(), TRIGGER_ERROR, e.getMessage());
}
throw new PipelineServiceClientException(
String.format(
"%s Failed to trigger workflow due to airflow API returned %s and response %s",
workflow.getName(),
Response.Status.fromStatusCode(response.statusCode()),
response.body()));
}
@Override
public PipelineServiceClientResponse runApplicationFlow(App application) {
return sendPost(APP_TRIGGER, application);
}
@Override
public PipelineServiceClientResponse validateAppRegistration(
AppMarketPlaceDefinition appMarketPlaceDefinition) {
return getResponse(200, "Success");
// TODO: Currently only internal apps are available, external apps will need this validation
// return sendPost(APP_VALIDATE, appMarketPlaceDefinition);
}
private PipelineServiceClientResponse sendPost(String endpoint, Object request) {
HttpResponse<String> response;
String workflowPayload = JsonUtils.pojoToJson(request);
try {
String automationsUrl = buildURI(endpoint).build().toString();
response = post(automationsUrl, workflowPayload);
if (response.statusCode() == 200) {
return getResponse(200, response.body());
}
} catch (IOException | URISyntaxException e) {
throw IngestionPipelineDeploymentException.byMessage(
workflowPayload, DEPLOYMENT_ERROR, e.getMessage());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw IngestionPipelineDeploymentException.byMessage(
workflowPayload, DEPLOYMENT_ERROR, e.getMessage());
}
throw new PipelineServiceClientException(
String.format(
"%s Failed to trigger flow due to airflow API returned %s and response %s",
workflowPayload,
Response.Status.fromStatusCode(response.statusCode()),
response.body()));
}
@Override
public PipelineServiceClientResponse killIngestion(IngestionPipeline ingestionPipeline) {
HttpResponse<String> response;
try {
String killUrl;
killUrl = buildURI("kill").build().toString();
JSONObject requestPayload = new JSONObject();
requestPayload.put(DAG_ID, ingestionPipeline.getName());
response = post(killUrl, requestPayload.toString());
if (response.statusCode() == 200) {
return getResponse(200, response.body());
}
} catch (IOException | URISyntaxException e) {
throw clientException("Failed to kill running workflows", e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw clientException("Failed to kill running workflows", e);
}
throw new PipelineServiceClientException(
String.format("Failed to kill running workflows due to %s", response.body()));
}
@Override
public Map<String, String> requestGetHostIp() {
HttpResponse<String> response;
try {
response = getRequestAuthenticatedForJsonContent(buildURI("ip").build().toString());
if (response.statusCode() == 200) {
return JsonUtils.readValue(response.body(), new TypeReference<>() {});
}
} catch (IOException | URISyntaxException e) {
throw clientException("Failed to get Pipeline Service host IP.", e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw clientException("Failed to get Pipeline Service host IP.", e);
}
throw new PipelineServiceClientException(
String.format("Failed to get Pipeline Service host IP due to %s", response.body()));
}
@Override
public Map<String, String> getLastIngestionLogs(
IngestionPipeline ingestionPipeline, String after) {
HttpResponse<String> response;
String taskId = TYPE_TO_TASK.get(ingestionPipeline.getPipelineType().toString());
// Init empty after query param
URIBuilder uri = buildURI("last_dag_logs");
if (after != null) {
uri.addParameter("after", after);
}
uri.addParameter(DAG_ID, ingestionPipeline.getName());
uri.addParameter("task_id", taskId);
try {
response = getRequestAuthenticatedForJsonContent(uri.build().toString());
if (response.statusCode() == 200) {
return JsonUtils.readValue(response.body(), new TypeReference<>() {});
}
} catch (IOException | URISyntaxException e) {
throw clientException("Failed to get last ingestion logs.", e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw clientException("Failed to get last ingestion logs.", e);
}
throw new PipelineServiceClientException(
String.format("Failed to get last ingestion logs due to %s", response.body()));
}
public URIBuilder buildURI(String path) {
try {
List<String> pathInternal = new ArrayList<>(API_ENDPOINT_SEGMENTS);
pathInternal.add(path);
URIBuilder builder = new URIBuilder(String.valueOf(serviceURL));
List<String> segments = new ArrayList<>(builder.getPathSegments());
segments.addAll(pathInternal);
return builder.setPathSegments(segments);
} catch (Exception e) {
throw clientException(String.format("Failed to built request URI for path [%s].", path), e);
}
}
private HttpResponse<String> getRequestAuthenticatedForJsonContent(String url)
throws IOException, InterruptedException {
HttpRequest request = authenticatedRequestBuilder(url).GET().build();
return client.send(request, HttpResponse.BodyHandlers.ofString());
}
private HttpResponse<String> deleteRequestAuthenticatedForJsonContent(String url)
throws IOException, InterruptedException {
HttpRequest request = authenticatedRequestBuilder(url).DELETE().build();
return client.send(request, HttpResponse.BodyHandlers.ofString());
}
private HttpRequest.Builder authenticatedRequestBuilder(String url) {
return HttpRequest.newBuilder(URI.create(url))
.header(CONTENT_HEADER, CONTENT_TYPE)
.header(AUTH_HEADER, getBasicAuthenticationHeader(username, password));
}
private PipelineServiceClientResponse getResponse(int code, String body) {
return new PipelineServiceClientResponse()
.withCode(code)
.withReason(body)
.withPlatform(this.getPlatform());
}
private PipelineServiceClientException clientException(String message, Exception e) {
return PipelineServiceClientException.byMessage(message, e.getMessage());
}
private PipelineServiceClientException clientException(IngestionPipeline pipeline, Exception e) {
return clientException(pipeline.getName(), e);
}
private PipelineServiceClientException clientException(
IngestionPipeline pipeline, String message, HttpResponse<String> response) {
return PipelineServiceClientException.byMessage(
pipeline.getName(), message, Response.Status.fromStatusCode(response.statusCode()));
}
}