bejancsaba commented on code in PR #6075: URL: https://github.com/apache/nifi/pull/6075#discussion_r891075210
########## c2/c2-client-bundle/c2-client-http/src/main/java/org/apache/nifi/c2/client/http/C2HttpClient.java: ########## @@ -0,0 +1,250 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.nifi.c2.client.http; + +import java.io.FileInputStream; +import java.io.IOException; +import java.security.KeyStore; +import java.security.NoSuchAlgorithmException; +import java.util.Optional; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSocketFactory; +import javax.net.ssl.TrustManager; +import javax.net.ssl.TrustManagerFactory; +import javax.net.ssl.X509TrustManager; +import okhttp3.MediaType; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; +import okhttp3.ResponseBody; +import okhttp3.logging.HttpLoggingInterceptor; +import org.apache.commons.lang3.StringUtils; +import org.apache.nifi.c2.client.C2ClientConfig; +import org.apache.nifi.c2.client.api.C2Client; +import org.apache.nifi.c2.client.api.C2Serializer; +import org.apache.nifi.c2.protocol.api.C2Heartbeat; +import org.apache.nifi.c2.protocol.api.C2HeartbeatResponse; +import org.apache.nifi.c2.protocol.api.C2OperationAck; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class C2HttpClient implements C2Client { + + private static final Logger logger = LoggerFactory.getLogger(C2HttpClient.class); + private static final MediaType MEDIA_TYPE_APPLICATION_JSON = MediaType.parse("application/json"); + + private final AtomicReference<OkHttpClient> httpClientReference = new AtomicReference<>(); + private final C2ClientConfig clientConfig; + private final C2Serializer serializer; + + public C2HttpClient(C2ClientConfig clientConfig, C2Serializer serializer) { + super(); + this.clientConfig = clientConfig; + this.serializer = serializer; + final OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder(); + + // Configure request and response logging + HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); + logging.setLevel(HttpLoggingInterceptor.Level.BASIC); + okHttpClientBuilder.addInterceptor(logging); + + // Set whether to follow redirects + okHttpClientBuilder.followRedirects(true); + + // Timeout for calls made to the server + okHttpClientBuilder.callTimeout(clientConfig.getCallTimeout(), TimeUnit.MILLISECONDS); + + // check if the ssl path is set and add the factory if so + if (StringUtils.isNotBlank(clientConfig.getKeystoreFilename())) { + try { + setSslSocketFactory(okHttpClientBuilder); + } catch (Exception e) { + throw new IllegalStateException("OkHttp TLS configuration failed", e); + } + } + + httpClientReference.set(okHttpClientBuilder.build()); + } + + @Override + public Optional<C2HeartbeatResponse> publishHeartbeat(C2Heartbeat heartbeat) { + return serializer.serialize(heartbeat).flatMap(this::sendHeartbeat); + } + + private Optional<C2HeartbeatResponse> sendHeartbeat(String heartbeat) { + logger.debug("Sending heartbeat to {}", clientConfig.getC2Url()); + + Optional<C2HeartbeatResponse> c2HeartbeatResponse = Optional.empty(); + Request request = new Request.Builder() + .post(RequestBody.create(heartbeat, MEDIA_TYPE_APPLICATION_JSON)) + .url(clientConfig.getC2Url()) + .build(); + + try (Response heartbeatResponse = httpClientReference.get().newCall(request).execute()) { + c2HeartbeatResponse = getResponseBody(heartbeatResponse).flatMap(response -> serializer.deserialize(response, C2HeartbeatResponse.class)); + } catch (IOException ce) { + logger.error("Send Heartbeat failed [{}]", clientConfig.getC2Url(), ce); + } + + return c2HeartbeatResponse; + } + + private Optional<String> getResponseBody(Response response) { + String responseBody = null; + + try { + responseBody = response.body().string(); + logger.debug("Received response body {}", responseBody); + } catch (IOException e) { + logger.error("HTTP Request failed", e); + } + + return Optional.ofNullable(responseBody); + } + + private void setSslSocketFactory(OkHttpClient.Builder okHttpClientBuilder) throws Exception { + final String keystoreLocation = clientConfig.getKeystoreFilename(); + final String keystoreType = clientConfig.getKeystoreType(); + final String keystorePass = clientConfig.getKeystorePass(); + + assertKeystorePropertiesSet(keystoreLocation, keystorePass, keystoreType); + + // prepare the keystore + final KeyStore keyStore = KeyStore.getInstance(keystoreType); + + try (FileInputStream keyStoreStream = new FileInputStream(keystoreLocation)) { + keyStore.load(keyStoreStream, keystorePass.toCharArray()); + } + + final KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); + keyManagerFactory.init(keyStore, keystorePass.toCharArray()); + + // load truststore + final String truststoreLocation = clientConfig.getTruststoreFilename(); + final String truststorePass = clientConfig.getTruststorePass(); + final String truststoreType = clientConfig.getTruststoreType(); + assertTruststorePropertiesSet(truststoreLocation, truststorePass, truststoreType); + + KeyStore truststore = KeyStore.getInstance(truststoreType); + final TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("X509"); + truststore.load(new FileInputStream(truststoreLocation), truststorePass.toCharArray()); + trustManagerFactory.init(truststore); + + final X509TrustManager x509TrustManager; + TrustManager[] trustManagers = trustManagerFactory.getTrustManagers(); + if (trustManagers[0] != null) { + x509TrustManager = (X509TrustManager) trustManagers[0]; + } else { + throw new IllegalStateException("List of trust managers is null"); + } + + SSLContext tempSslContext; + try { + tempSslContext = SSLContext.getInstance("TLS"); + } catch (NoSuchAlgorithmException e) { + throw new IllegalStateException("SSLContext creation failed", e); + } + + final SSLContext sslContext = tempSslContext; + sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null); + + final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); + okHttpClientBuilder.sslSocketFactory(sslSocketFactory, x509TrustManager); + } + + private void assertKeystorePropertiesSet(String location, String password, String type) { + if (location == null || location.isEmpty()) { + throw new IllegalArgumentException(clientConfig.getKeystoreFilename() + " is null or is empty"); + } + + if (password == null || password.isEmpty()) { + throw new IllegalArgumentException("The client's keystore filename is set but its password is not (or is empty). If the location is set, the password must also be."); + } + + if (type == null || type.isEmpty()) { + throw new IllegalArgumentException("The client's keystore filename is set but its type is not (or is empty). If the location is set, the type must also be."); + } + } + + private void assertTruststorePropertiesSet(String location, String password, String type) { + if (location == null || location.isEmpty()) { + throw new IllegalArgumentException("The client's truststore filename is not set or is empty"); + } + + if (password == null || password.isEmpty()) { + throw new IllegalArgumentException("The client's truststore filename is set but its password is not (or is empty). If the location is set, the password must also be."); + } + + if (type == null || type.isEmpty()) { + throw new IllegalArgumentException("The client's truststore filename is set but its type is not (or is empty). If the location is set, the type must also be."); + } + } + + @Override + public byte[] retrieveUpdateContent(String flowUpdateUrl) { + final Request.Builder requestBuilder = new Request.Builder() + .get() + .url(flowUpdateUrl); + final Request request = requestBuilder.build(); + + ResponseBody body; + try (final Response response = httpClientReference.get().newCall(request).execute()) { + logger.debug("Response received: {}", response); Review Comment: You are right, I removed it. ########## c2/c2-client-bundle/c2-client-service/src/main/java/org/apache/nifi/c2/client/service/operation/UpdateConfigurationOperationHandler.java: ########## @@ -0,0 +1,115 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.nifi.c2.client.service.operation; + +import static org.apache.commons.lang3.StringUtils.EMPTY; +import static org.apache.nifi.c2.protocol.api.OperandType.CONFIGURATION; +import static org.apache.nifi.c2.protocol.api.OperationType.UPDATE; + +import java.net.URI; +import java.util.Optional; +import java.util.function.Function; +import org.apache.nifi.c2.client.api.C2Client; +import org.apache.nifi.c2.client.service.FlowIdHolder; +import org.apache.nifi.c2.protocol.api.C2Operation; +import org.apache.nifi.c2.protocol.api.C2OperationAck; +import org.apache.nifi.c2.protocol.api.C2OperationState; +import org.apache.nifi.c2.protocol.api.OperandType; +import org.apache.nifi.c2.protocol.api.OperationType; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class UpdateConfigurationOperationHandler implements C2OperationHandler { + + private static final Logger logger = LoggerFactory.getLogger(UpdateConfigurationOperationHandler.class); + + private static final String LOCATION = "location"; + + private final C2Client client; + private final Function<byte[], Boolean> updateFlow; + private final FlowIdHolder flowIdHolder; + + public UpdateConfigurationOperationHandler(C2Client client, FlowIdHolder flowIdHolder, Function<byte[], Boolean> updateFlow) { + this.client = client; + this.updateFlow = updateFlow; + this.flowIdHolder = flowIdHolder; + } + + @Override + public OperationType getOperationType() { + return UPDATE; + } + + @Override + public OperandType getOperandType() { + return CONFIGURATION; + } + + @Override + public C2OperationAck handle(C2Operation operation) { + String opIdentifier = Optional.ofNullable(operation.getIdentifier()) + .orElse(EMPTY); + C2OperationAck operationAck = new C2OperationAck(); + C2OperationState state = new C2OperationState(); + operationAck.setOperationState(state); + operationAck.setOperationId(opIdentifier); + + String updateLocation = Optional.ofNullable(operation.getArgs()) + .map(map -> map.get(LOCATION)) + .orElse(EMPTY); + + String newFlowId = parseFlowId(updateLocation); + if (flowIdHolder.getFlowId() == null || !flowIdHolder.getFlowId().equals(newFlowId)) { + logger.info("Will perform flow update from {} for operation #{}. Previous flow id was {}, replacing with new id {}", updateLocation, opIdentifier, + flowIdHolder.getFlowId() == null ? "not set" : flowIdHolder.getFlowId(), newFlowId); + } else { + logger.info("Flow is current, no update is necessary..."); + } + + flowIdHolder.setFlowId(newFlowId); + byte[] updateContent = client.retrieveUpdateContent(updateLocation); + if (updateContent != null) { + if (updateFlow.apply(updateContent)) { + state.setState(C2OperationState.OperationState.FULLY_APPLIED); + logger.debug("Update configuration applied for operation #{}.", opIdentifier); + } else { + state.setState(C2OperationState.OperationState.NOT_APPLIED); + logger.error("Update resulted in error for operation #{}.", opIdentifier); + } + } else { + state.setState(C2OperationState.OperationState.NOT_APPLIED); + logger.error("Update content retrieval resulted in empty content so flow update was omitted for operation #{}.", opIdentifier); + } + + return operationAck; + } + + private String parseFlowId(String flowUpdateUrl) { + try { + URI flowUri = new URI(flowUpdateUrl); + String flowUriPath = flowUri.getPath(); + String[] split = flowUriPath.split("/"); + if (split.length > 4) { + return split[4]; + } else { + throw new Exception(); Review Comment: Applied -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
