adamdebreceni commented on code in PR #6075: URL: https://github.com/apache/nifi/pull/6075#discussion_r891024533
########## 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)); Review Comment: added a comment in the ticket [NIFI-9667](https://issues.apache.org/jira/browse/NIFI-9667) for this to be possibly addressed later ########## 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]; Review Comment: added a comment in the ticket [NIFI-9667](https://issues.apache.org/jira/browse/NIFI-9667) for this to be possibly addressed later -- 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]
