You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by "bejancsaba (via GitHub)" <gi...@apache.org> on 2023/04/05 19:42:24 UTC

[GitHub] [nifi] bejancsaba commented on a diff in pull request #7125: NIFI-11366 Proxy aware C2 communication

bejancsaba commented on code in PR #7125:
URL: https://github.com/apache/nifi/pull/7125#discussion_r1158870015


##########
c2/c2-client-bundle/c2-client-api/src/main/java/org/apache/nifi/c2/client/api/C2Client.java:
##########
@@ -57,4 +57,14 @@ public interface C2Client {
      * @return optional error message if any issues occurred
      */
     Optional<String> uploadBundle(String callbackUrl, byte[] bundle);
+
+    /**
+     * Creates a callback URL according to proxy aware C2 settings
+     *
+     * @param absoluteUrl absolute url sent by C2 server
+     * @param relativeUrl relative url sent by C2 server
+     * @return finalised callback url
+     * @throws Exception when the callback url can not be created as per the current configuration and parameters
+     */
+    String getCallbackUrl(String absoluteUrl, String relativeUrl) throws Exception;

Review Comment:
   Would it be possible to retrun optional similarly to the other functions here (this way not requiring exception to be thrown) or there are downstream constraints why this approach wouldn't fit?



##########
c2/c2-client-bundle/c2-client-http/src/main/java/org/apache/nifi/c2/client/http/url/C2UrlProvider.java:
##########
@@ -0,0 +1,26 @@
+/*
+ * 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.url;
+
+public interface C2UrlProvider {
+
+    String getHeartbeatUrl();

Review Comment:
   Could you please add short Javadoc for the functions in the interface?



##########
c2/c2-client-bundle/c2-client-http/src/main/java/org/apache/nifi/c2/client/http/url/C2UrlProviderFactory.java:
##########
@@ -0,0 +1,40 @@
+/*
+ * 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.url;
+
+import static org.apache.commons.lang3.StringUtils.isNoneBlank;
+
+import org.apache.nifi.c2.client.C2ClientConfig;
+
+public class C2UrlProviderFactory {
+
+    private final C2ClientConfig clientConfig;
+
+    public C2UrlProviderFactory(C2ClientConfig clientConfig) {
+        this.clientConfig = clientConfig;
+    }
+
+    public C2UrlProvider create() {
+        if (isNoneBlank(clientConfig.getC2RestApi(), clientConfig.getC2RestPathHeartbeat(), clientConfig.getC2RestPathAcknowledge())) {
+            return new ProxyAwareC2UrlProvider(clientConfig.getC2RestApi(), clientConfig.getC2RestPathHeartbeat(), clientConfig.getC2RestPathAcknowledge());
+        } else if (isNoneBlank(clientConfig.getC2Url(), clientConfig.getC2AckUrl())) {
+            return new LegacyC2UrlProvider(clientConfig.getC2Url(), clientConfig.getC2AckUrl());
+        } else {
+            throw new IllegalArgumentException("Incorrect configuration. Please configure legacy or proxy aware C2 URL properties");

Review Comment:
   I think it would make sense to provide a little more information here I don't think all users would understand what legacy / proxy aware C2 URL properties means. Listing explicitly all the properties for legacy and for proxy aware would be a big help I suppose. What do you think?



##########
minifi/minifi-nar-bundles/minifi-framework-bundle/minifi-framework/minifi-resources/src/main/resources/conf/bootstrap.conf:
##########
@@ -134,8 +134,13 @@ java.arg.14=-Djava.awt.headless=true
 # Enabling C2 Uncomment each of the following options
 #c2.enable=true
 ## define protocol parameters
+# legacy properties
 #c2.rest.url=
 #c2.rest.url.ack=
+# new c2 properties
+#c2.rest.api=

Review Comment:
   Naming is very subjective (as always) just trying to provide alternates maybe they are better or owrse will leave it to you or anyone else who reviews it:
   - c2.rest.host - maybe this is misleading as it could contain not just the host
   - c2.rest.base or c2.rest.path.base
   
   What do you think?



##########
c2/c2-client-bundle/c2-client-http/src/main/java/org/apache/nifi/c2/client/http/url/ProxyAwareC2UrlProvider.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * 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.url;
+
+import static org.apache.commons.lang3.StringUtils.isNotBlank;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+
+public class ProxyAwareC2UrlProvider implements C2UrlProvider {
+
+    private final String c2RestApi;
+    private final String c2RestPathHeartbeat;
+    private final String c2RestPathAcknowledge;
+
+    ProxyAwareC2UrlProvider(String c2RestApi, String c2RestPathHeartbeat, String c2RestPathAcknowledge) {
+        this.c2RestApi = c2RestApi;
+        this.c2RestPathHeartbeat = c2RestPathHeartbeat;
+        this.c2RestPathAcknowledge = c2RestPathAcknowledge;
+    }
+
+    @Override
+    public String getHeartbeatUrl() {
+        return toAbsoluteUrl(c2RestPathHeartbeat);
+    }
+
+    @Override
+    public String getAcknowledgeUrl() {
+        return toAbsoluteUrl(c2RestPathAcknowledge);
+    }
+
+    @Override
+    public String getCallbackUrl(String absoluteUrl, String relativeUrl) throws Exception {

Review Comment:
   What do you think about making the decisions below in C2HttpClient (where this is called) and introducing a "fromRelativeUrl" and a "fromAbsoluteUrl" function and the fromAbsolute could have  adefault implementation in C2UrlProvider.



##########
c2/c2-client-bundle/c2-client-http/src/main/java/org/apache/nifi/c2/client/http/url/ProxyAwareC2UrlProvider.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * 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.url;
+
+import static org.apache.commons.lang3.StringUtils.isNotBlank;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+
+public class ProxyAwareC2UrlProvider implements C2UrlProvider {
+
+    private final String c2RestApi;
+    private final String c2RestPathHeartbeat;
+    private final String c2RestPathAcknowledge;
+
+    ProxyAwareC2UrlProvider(String c2RestApi, String c2RestPathHeartbeat, String c2RestPathAcknowledge) {
+        this.c2RestApi = c2RestApi;
+        this.c2RestPathHeartbeat = c2RestPathHeartbeat;
+        this.c2RestPathAcknowledge = c2RestPathAcknowledge;
+    }
+
+    @Override
+    public String getHeartbeatUrl() {
+        return toAbsoluteUrl(c2RestPathHeartbeat);
+    }
+
+    @Override
+    public String getAcknowledgeUrl() {
+        return toAbsoluteUrl(c2RestPathAcknowledge);
+    }
+
+    @Override
+    public String getCallbackUrl(String absoluteUrl, String relativeUrl) throws Exception {
+        if (isNotBlank(relativeUrl)) {
+            return toAbsoluteUrl(relativeUrl);
+        }
+        if (isNotBlank(absoluteUrl)) {
+            return absoluteUrl;
+        }
+        throw new Exception("Unable to provide callback url as both parameters were empty or null");
+    }
+
+    private String toAbsoluteUrl(String path) {
+        try {
+            URL baseUrl = new URL(c2RestApi);

Review Comment:
   You could do this in the init phase so if something is misconfigured it would be evident instantly at startup. Just an idea.



##########
c2/c2-client-bundle/c2-client-http/src/main/java/org/apache/nifi/c2/client/http/url/ProxyAwareC2UrlProvider.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * 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.url;
+
+import static org.apache.commons.lang3.StringUtils.isNotBlank;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+
+public class ProxyAwareC2UrlProvider implements C2UrlProvider {
+
+    private final String c2RestApi;
+    private final String c2RestPathHeartbeat;
+    private final String c2RestPathAcknowledge;
+
+    ProxyAwareC2UrlProvider(String c2RestApi, String c2RestPathHeartbeat, String c2RestPathAcknowledge) {
+        this.c2RestApi = c2RestApi;
+        this.c2RestPathHeartbeat = c2RestPathHeartbeat;
+        this.c2RestPathAcknowledge = c2RestPathAcknowledge;
+    }
+
+    @Override
+    public String getHeartbeatUrl() {
+        return toAbsoluteUrl(c2RestPathHeartbeat);
+    }
+
+    @Override
+    public String getAcknowledgeUrl() {
+        return toAbsoluteUrl(c2RestPathAcknowledge);
+    }
+
+    @Override
+    public String getCallbackUrl(String absoluteUrl, String relativeUrl) throws Exception {
+        if (isNotBlank(relativeUrl)) {
+            return toAbsoluteUrl(relativeUrl);
+        }
+        if (isNotBlank(absoluteUrl)) {
+            return absoluteUrl;
+        }
+        throw new Exception("Unable to provide callback url as both parameters were empty or null");
+    }
+
+    private String toAbsoluteUrl(String path) {
+        try {
+            URL baseUrl = new URL(c2RestApi);
+            URL fullUrl = new URL(baseUrl, path);
+            return fullUrl.toString();
+        } catch (MalformedURLException e) {
+            throw new RuntimeException(e);

Review Comment:
   Can you add a liitle text stating that the absolute url creation failed or something along those line?



-- 
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: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org