Skip to content

Commit 946c2fe

Browse files
ottobackwardsjoewitt
authored andcommitted
NIFI-5022 This closes #2588. InvokeAWSGatewayApi processor
per review, simplify header building and remove new map Per review: - removed apache headers from 3rd party files per https://www.apache.org/legal/src-headers.html#3party - referenced commit/repo in notice file - created rat configuration to account for files without headers Signed-off-by: joewitt <[email protected]>
1 parent e3b0949 commit 946c2fe

21 files changed

+3688
-4
lines changed

NOTICE

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,27 @@ This includes derived works from Apache Solr available under Apache Software Lic
6464

6565
This includes derived works from Apache Hadoop available under Apache Software License V2. Portions of the code found in
6666
https://github.com/apache/hadoop/blob/release-2.7.3-RC2/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/UserGroupInformation.java
67-
The code can be found nifi-commons/nifi-security-utils/src/main/java/org/apache/nifi/security/krb/KeytabUser.java
67+
The code can be found nifi-commons/nifi-security-utils/src/main/java/org/apache/nifi/security/krb/KeytabUser.java
68+
This includes derived works from apigateway-generic-java-sdk (ASLv2 licenced) project (https://github.com/rpgreen/apigateway-generic-java-sdk):
69+
The derived work is adapted from
70+
main/ca/ryangreen/apigateway/generic/
71+
GenericApiGatewayClient.java
72+
GenericApiGatewayClientBuilder.java
73+
GenericApiGatewayException.java
74+
GenericApiGatewayRequest.java
75+
GenericApiGatewayRequestBuilder.java
76+
test/ca/ryangreen/apigateway/generic/
77+
GenericApiGatewayClientTest.java
78+
LambdaMatcher.java
79+
and can be found in the directories:
80+
nifi-nar-bundles/nifi-aws-bundle/nifi-aws-abstract-processors/src/main/../wag/client/
81+
GenericApiGatewayClient.java
82+
GenericApiGatewayClientBuilder.java
83+
GenericApiGatewayException.java
84+
GenericApiGatewayRequest.java
85+
GenericApiGatewayRequestBuilder.java
86+
Validate.java
87+
nifi-nar-bundles/nifi-aws-bundle/nifi-aws-processors/src/test/../wag/
88+
RequestMatcher.java
89+
GetAWSGatewayApiTest.java
90+

nifi-nar-bundles/nifi-aws-bundle/nifi-aws-abstract-processors/pom.xml

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@
5353
<groupId>com.amazonaws</groupId>
5454
<artifactId>aws-java-sdk-sqs</artifactId>
5555
</dependency>
56+
<dependency>
57+
<groupId>commons-io</groupId>
58+
<artifactId>commons-io</artifactId>
59+
<version>2.6</version>
60+
</dependency>
5661
<dependency>
5762
<groupId>org.apache.commons</groupId>
5863
<artifactId>commons-lang3</artifactId>
@@ -81,5 +86,19 @@
8186
<artifactId>nifi-proxy-configuration-api</artifactId>
8287
</dependency>
8388
</dependencies>
84-
89+
<build>
90+
<plugins>
91+
<plugin>
92+
<groupId>org.apache.rat</groupId>
93+
<artifactId>apache-rat-plugin</artifactId>
94+
<configuration>
95+
<excludes>
96+
<!-- 3rd party APL 2.0 code brought in, should not have headers -->
97+
<!--https://www.apache.org/legal/src-headers.html#3party-->
98+
<exclude>src/main/java/org/apache/nifi/processors/aws/wag/client/*.java</exclude>
99+
</excludes>
100+
</configuration>
101+
</plugin>
102+
</plugins>
103+
</build>
85104
</project>

nifi-nar-bundles/nifi-aws-bundle/nifi-aws-abstract-processors/src/main/java/org/apache/nifi/processors/aws/AbstractAWSProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,11 @@ public abstract class AbstractAWSProcessor<ClientType extends AmazonWebServiceCl
156156
private static final ProxySpec[] PROXY_SPECS = {ProxySpec.HTTP_AUTH};
157157
public static final PropertyDescriptor PROXY_CONFIGURATION_SERVICE = ProxyConfiguration.createProxyConfigPropertyDescriptor(true, PROXY_SPECS);
158158

159-
private static AllowableValue createAllowableValue(final Regions region) {
159+
protected static AllowableValue createAllowableValue(final Regions region) {
160160
return new AllowableValue(region.getName(), AWSRegions.getRegionDisplayName(region.getName()));
161161
}
162162

163-
private static AllowableValue[] getAvailableRegions() {
163+
protected static AllowableValue[] getAvailableRegions() {
164164
final List<AllowableValue> values = new ArrayList<>();
165165
for (final Regions region : Regions.values()) {
166166
values.add(createAllowableValue(region));

nifi-nar-bundles/nifi-aws-bundle/nifi-aws-abstract-processors/src/main/java/org/apache/nifi/processors/aws/wag/AbstractAWSGatewayApiProcessor.java

Lines changed: 675 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package org.apache.nifi.processors.aws.wag.client;
2+
3+
import com.amazonaws.AmazonServiceException;
4+
import com.amazonaws.AmazonWebServiceClient;
5+
import com.amazonaws.ClientConfiguration;
6+
import com.amazonaws.DefaultRequest;
7+
import com.amazonaws.auth.AWS4Signer;
8+
import com.amazonaws.auth.AWSCredentialsProvider;
9+
import com.amazonaws.http.AmazonHttpClient;
10+
import com.amazonaws.http.ExecutionContext;
11+
import com.amazonaws.http.HttpMethodName;
12+
import com.amazonaws.http.HttpResponseHandler;
13+
import com.amazonaws.http.JsonResponseHandler;
14+
import com.amazonaws.internal.auth.DefaultSignerProvider;
15+
import com.amazonaws.protocol.json.JsonOperationMetadata;
16+
import com.amazonaws.protocol.json.SdkStructuredPlainJsonFactory;
17+
import com.amazonaws.regions.Region;
18+
import com.amazonaws.transform.JsonErrorUnmarshaller;
19+
import com.amazonaws.transform.JsonUnmarshallerContext;
20+
import com.amazonaws.transform.Unmarshaller;
21+
import com.fasterxml.jackson.databind.JsonNode;
22+
import java.io.InputStream;
23+
import java.net.URI;
24+
import java.util.Collections;
25+
import java.util.HashMap;
26+
import java.util.List;
27+
import java.util.Map;
28+
29+
public class GenericApiGatewayClient extends AmazonWebServiceClient {
30+
private static final String API_GATEWAY_SERVICE_NAME = "execute-api";
31+
private static final String API_KEY_HEADER = "x-api-key";
32+
33+
private final JsonResponseHandler<GenericApiGatewayResponse> responseHandler;
34+
private final HttpResponseHandler<AmazonServiceException> errorResponseHandler;
35+
private final AWSCredentialsProvider credentials;
36+
private String apiKey;
37+
private final AWS4Signer signer;
38+
39+
GenericApiGatewayClient(ClientConfiguration clientConfiguration, String endpoint, Region region,
40+
AWSCredentialsProvider credentials, String apiKey, AmazonHttpClient httpClient) {
41+
super(clientConfiguration);
42+
setRegion(region);
43+
setEndpoint(endpoint);
44+
this.credentials = credentials;
45+
this.apiKey = apiKey;
46+
this.signer = new AWS4Signer();
47+
this.signer.setServiceName(API_GATEWAY_SERVICE_NAME);
48+
this.signer.setRegionName(region.getName());
49+
50+
final JsonOperationMetadata metadata = new JsonOperationMetadata().withHasStreamingSuccessResponse(false).withPayloadJson(false);
51+
final Unmarshaller<GenericApiGatewayResponse, JsonUnmarshallerContext> responseUnmarshaller = in -> new GenericApiGatewayResponse(in.getHttpResponse());
52+
this.responseHandler = SdkStructuredPlainJsonFactory.SDK_JSON_FACTORY.createResponseHandler(metadata, responseUnmarshaller);
53+
JsonErrorUnmarshaller defaultErrorUnmarshaller = new JsonErrorUnmarshaller(GenericApiGatewayException.class, null) {
54+
@Override
55+
public AmazonServiceException unmarshall(JsonNode jsonContent) throws Exception {
56+
return new GenericApiGatewayException(jsonContent.toString());
57+
}
58+
};
59+
this.errorResponseHandler = SdkStructuredPlainJsonFactory.SDK_JSON_FACTORY.createErrorResponseHandler(
60+
Collections.singletonList(defaultErrorUnmarshaller), null);
61+
62+
if (httpClient != null) {
63+
super.client = httpClient;
64+
}
65+
}
66+
67+
public GenericApiGatewayResponse execute(GenericApiGatewayRequest request) {
68+
return execute(request.getHttpMethod(), request.getResourcePath(), request.getHeaders(), request.getParameters(), request.getBody());
69+
}
70+
71+
private GenericApiGatewayResponse execute(HttpMethodName method, String resourcePath, Map<String, String> headers, Map<String,List<String>> parameters, InputStream content) {
72+
final ExecutionContext executionContext = buildExecutionContext();
73+
74+
DefaultRequest request = new DefaultRequest(API_GATEWAY_SERVICE_NAME);
75+
request.setHttpMethod(method);
76+
request.setContent(content);
77+
request.setEndpoint(this.endpoint);
78+
request.setResourcePath(resourcePath);
79+
request.setHeaders(buildRequestHeaders(headers, apiKey));
80+
if (parameters != null) {
81+
request.setParameters(parameters);
82+
}
83+
return this.client.execute(request, responseHandler, errorResponseHandler, executionContext).getAwsResponse();
84+
}
85+
86+
private ExecutionContext buildExecutionContext() {
87+
final ExecutionContext executionContext = ExecutionContext.builder().withSignerProvider(
88+
new DefaultSignerProvider(this, signer)).build();
89+
executionContext.setCredentialsProvider(credentials);
90+
executionContext.setSigner(signer);
91+
return executionContext;
92+
}
93+
94+
private Map<String, String> buildRequestHeaders(Map<String, String> headers, String apiKey) {
95+
if (headers == null) {
96+
headers = new HashMap<>();
97+
}
98+
if (apiKey != null) {
99+
headers.put(API_KEY_HEADER, apiKey);
100+
}
101+
return headers;
102+
}
103+
104+
public URI getEndpoint() {
105+
return this.endpoint;
106+
}
107+
108+
@Override
109+
protected String getServiceNameIntern() {
110+
return API_GATEWAY_SERVICE_NAME;
111+
}
112+
}
113+
114+
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package org.apache.nifi.processors.aws.wag.client;
2+
3+
import com.amazonaws.ClientConfiguration;
4+
import com.amazonaws.auth.AWSCredentialsProvider;
5+
import com.amazonaws.http.AmazonHttpClient;
6+
import com.amazonaws.regions.Region;
7+
8+
public class GenericApiGatewayClientBuilder {
9+
private String endpoint;
10+
private Region region;
11+
private AWSCredentialsProvider credentials;
12+
private ClientConfiguration clientConfiguration;
13+
private String apiKey;
14+
private AmazonHttpClient httpClient;
15+
16+
public GenericApiGatewayClientBuilder withEndpoint(String endpoint) {
17+
this.endpoint = endpoint;
18+
return this;
19+
}
20+
21+
public GenericApiGatewayClientBuilder withRegion(Region region) {
22+
this.region = region;
23+
return this;
24+
}
25+
26+
public GenericApiGatewayClientBuilder withClientConfiguration(ClientConfiguration clientConfiguration) {
27+
this.clientConfiguration = clientConfiguration;
28+
return this;
29+
}
30+
31+
public GenericApiGatewayClientBuilder withCredentials(AWSCredentialsProvider credentials) {
32+
this.credentials = credentials;
33+
return this;
34+
}
35+
36+
public GenericApiGatewayClientBuilder withApiKey(String apiKey) {
37+
this.apiKey = apiKey;
38+
return this;
39+
}
40+
41+
public GenericApiGatewayClientBuilder withHttpClient(AmazonHttpClient client) {
42+
this.httpClient = client;
43+
return this;
44+
}
45+
46+
public AWSCredentialsProvider getCredentials() {
47+
return credentials;
48+
}
49+
50+
public String getApiKey() {
51+
return apiKey;
52+
}
53+
54+
public AmazonHttpClient getHttpClient() {
55+
return httpClient;
56+
}
57+
58+
public String getEndpoint() {
59+
return endpoint;
60+
}
61+
62+
public Region getRegion() {
63+
return region;
64+
}
65+
66+
public ClientConfiguration getClientConfiguration() {
67+
return clientConfiguration;
68+
}
69+
70+
public GenericApiGatewayClient build() {
71+
Validate.notEmpty(endpoint, "Endpoint");
72+
Validate.notNull(region, "Region");
73+
return new GenericApiGatewayClient(clientConfiguration, endpoint, region, credentials, apiKey, httpClient);
74+
}
75+
76+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.apache.nifi.processors.aws.wag.client;
2+
3+
import com.amazonaws.AmazonServiceException;
4+
5+
public class GenericApiGatewayException extends AmazonServiceException {
6+
public GenericApiGatewayException(String errorMessage) {
7+
super(errorMessage);
8+
}
9+
10+
public GenericApiGatewayException(String errorMessage, Exception cause) {
11+
super(errorMessage, cause);
12+
}
13+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.apache.nifi.processors.aws.wag.client;
2+
3+
import com.amazonaws.http.HttpMethodName;
4+
import java.io.InputStream;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
public class GenericApiGatewayRequest {
9+
10+
private final HttpMethodName httpMethod;
11+
private final String resourcePath;
12+
private final InputStream body;
13+
private final Map<String, String> headers;
14+
private final Map<String, List<String>> parameters;
15+
16+
public GenericApiGatewayRequest(HttpMethodName httpMethod, String resourcePath,
17+
InputStream body, Map<String, String> headers,
18+
Map<String, List<String>> parameters) {
19+
this.httpMethod = httpMethod;
20+
this.resourcePath = resourcePath;
21+
this.body = body;
22+
this.headers = headers;
23+
this.parameters = parameters;
24+
}
25+
26+
public HttpMethodName getHttpMethod() {
27+
return httpMethod;
28+
}
29+
30+
public String getResourcePath() {
31+
return resourcePath;
32+
}
33+
34+
public InputStream getBody() {
35+
return body;
36+
}
37+
38+
public Map<String, String> getHeaders() {
39+
return headers;
40+
}
41+
42+
public Map<String, List<String>> getParameters() {
43+
return parameters;
44+
}
45+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.apache.nifi.processors.aws.wag.client;
2+
3+
import com.amazonaws.http.HttpMethodName;
4+
import java.io.InputStream;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
public class GenericApiGatewayRequestBuilder {
9+
private HttpMethodName httpMethod;
10+
private String resourcePath;
11+
private InputStream body;
12+
private Map<String, String> headers;
13+
private Map<String, List<String>> parameters;
14+
15+
public GenericApiGatewayRequestBuilder withHttpMethod(HttpMethodName name) {
16+
httpMethod = name;
17+
return this;
18+
}
19+
20+
public GenericApiGatewayRequestBuilder withResourcePath(String path) {
21+
resourcePath = path;
22+
return this;
23+
}
24+
25+
public GenericApiGatewayRequestBuilder withBody(InputStream content) {
26+
this.body = content;
27+
return this;
28+
}
29+
30+
public GenericApiGatewayRequestBuilder withHeaders(Map<String, String> headers) {
31+
this.headers = headers;
32+
return this;
33+
}
34+
35+
public GenericApiGatewayRequestBuilder withParameters(Map<String,List<String>> parameters) {
36+
this.parameters = parameters;
37+
return this;
38+
}
39+
40+
public boolean hasBody() {
41+
return this.body != null;
42+
}
43+
44+
public GenericApiGatewayRequest build() {
45+
Validate.notNull(httpMethod, "HTTP method");
46+
Validate.notEmpty(resourcePath, "Resource path");
47+
return new GenericApiGatewayRequest(httpMethod, resourcePath, body, headers, parameters);
48+
}
49+
}

0 commit comments

Comments
 (0)