turcsanyip commented on code in PR #11323:
URL: https://github.com/apache/nifi/pull/11323#discussion_r3391205389


##########
nifi-extension-bundles/nifi-gcp-bundle/nifi-gcp-processors/src/main/java/org/apache/nifi/processors/gcp/credentials/factory/ServiceAccountJsonValidator.java:
##########
@@ -0,0 +1,73 @@
+/*
+ * 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.processors.gcp.credentials.factory;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.nifi.components.ValidationContext;
+import org.apache.nifi.components.ValidationResult;
+import org.apache.nifi.components.Validator;
+import org.apache.nifi.processor.util.JsonValidator;
+
+/**
+ * Validates that a property value is a JSON object describing a Google 
Service Account key, that is a JSON object
+ * whose {@code type} field is {@code service_account}. Other Google 
credential types, such as {@code external_account},
+ * are rejected so that external identity credentials are configured through 
the Workload Identity Federation strategy.
+ */
+public class ServiceAccountJsonValidator implements Validator {
+    public static final ServiceAccountJsonValidator INSTANCE = new 
ServiceAccountJsonValidator();
+
+    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
+
+    private static final String TYPE_FIELD = "type";
+    private static final String SERVICE_ACCOUNT_TYPE = "service_account";
+
+    @Override
+    public ValidationResult validate(final String subject, final String input, 
final ValidationContext context) {
+        final ValidationResult jsonResult = 
JsonValidator.INSTANCE.validate(subject, input, context);
+        if (!jsonResult.isValid()) {
+            return jsonResult;
+        }
+
+        if (context.isExpressionLanguageSupported(subject) && 
context.isExpressionLanguagePresent(input)) {
+            return jsonResult;
+        }
+
+        final ValidationResult.Builder builder = new ValidationResult.Builder()
+                .subject(subject)
+                .input(input);
+
+        try {
+            final JsonNode rootNode = OBJECT_MAPPER.readTree(input);
+            final JsonNode typeNode = rootNode.get(TYPE_FIELD);
+            if (typeNode != null && 
SERVICE_ACCOUNT_TYPE.equals(typeNode.asText())) {
+                builder.valid(true);
+                builder.explanation("Service Account JSON found");
+            } else {
+                final String foundType = typeNode == null ? "none" : 
typeNode.asText();
+                builder.valid(false);
+                builder.explanation(("Expected a Service Account key with 
\"type\": \"service_account\" but found type: %s. "

Review Comment:
   ```suggestion
                   builder.explanation(("Expected a Service Account key with 
\"type\": \"service_account\" but found \"type\": \"%s\". "
   ```



##########
nifi-extension-bundles/nifi-gcp-bundle/nifi-gcp-processors/src/main/java/org/apache/nifi/processors/gcp/credentials/factory/strategies/AbstractServiceAccountCredentialsStrategy.java:
##########
@@ -38,14 +39,24 @@ public AbstractServiceAccountCredentialsStrategy(String 
name) {
     protected abstract InputStream 
getServiceAccountJson(Map<PropertyDescriptor, String> properties) throws 
IOException;
 
     @Override
-    public GoogleCredentials getGoogleCredentials(Map<PropertyDescriptor, 
String> properties, HttpTransportFactory transportFactory) throws IOException {
+    public GoogleCredentials getGoogleCredentials(final 
Map<PropertyDescriptor, String> properties, final HttpTransportFactory 
transportFactory) throws IOException {
+        final GoogleCredentials credentials;
+        try (final InputStream serviceAccountJson = 
getServiceAccountJson(properties)) {
+            credentials = GoogleCredentials.fromStream(serviceAccountJson, 
transportFactory);
+        }
+
+        if (!(credentials instanceof ServiceAccountCredentials)) {
+            throw new IOException(("Configured credentials must be a Google 
Service Account key (\"type\": \"service_account\") but resolved to %s. "
+                    + "Use the Workload Identity Federation strategy for 
external account 
credentials.").formatted(credentials.getClass().getSimpleName()));

Review Comment:
   Same as in `ServiceAccountJsonValidator`: the actual type should be checked 
for `ExternalAccountCredentials`.



##########
nifi-extension-bundles/nifi-gcp-bundle/nifi-gcp-processors/src/main/java/org/apache/nifi/processors/gcp/credentials/factory/ServiceAccountJsonValidator.java:
##########
@@ -0,0 +1,73 @@
+/*
+ * 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.processors.gcp.credentials.factory;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.nifi.components.ValidationContext;
+import org.apache.nifi.components.ValidationResult;
+import org.apache.nifi.components.Validator;
+import org.apache.nifi.processor.util.JsonValidator;
+
+/**
+ * Validates that a property value is a JSON object describing a Google 
Service Account key, that is a JSON object
+ * whose {@code type} field is {@code service_account}. Other Google 
credential types, such as {@code external_account},
+ * are rejected so that external identity credentials are configured through 
the Workload Identity Federation strategy.
+ */
+public class ServiceAccountJsonValidator implements Validator {
+    public static final ServiceAccountJsonValidator INSTANCE = new 
ServiceAccountJsonValidator();
+
+    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
+
+    private static final String TYPE_FIELD = "type";
+    private static final String SERVICE_ACCOUNT_TYPE = "service_account";
+
+    @Override
+    public ValidationResult validate(final String subject, final String input, 
final ValidationContext context) {
+        final ValidationResult jsonResult = 
JsonValidator.INSTANCE.validate(subject, input, context);
+        if (!jsonResult.isValid()) {
+            return jsonResult;
+        }
+
+        if (context.isExpressionLanguageSupported(subject) && 
context.isExpressionLanguagePresent(input)) {
+            return jsonResult;
+        }
+
+        final ValidationResult.Builder builder = new ValidationResult.Builder()
+                .subject(subject)
+                .input(input);
+
+        try {
+            final JsonNode rootNode = OBJECT_MAPPER.readTree(input);
+            final JsonNode typeNode = rootNode.get(TYPE_FIELD);
+            if (typeNode != null && 
SERVICE_ACCOUNT_TYPE.equals(typeNode.asText())) {
+                builder.valid(true);
+                builder.explanation("Service Account JSON found");
+            } else {
+                final String foundType = typeNode == null ? "none" : 
typeNode.asText();
+                builder.valid(false);
+                builder.explanation(("Expected a Service Account key with 
\"type\": \"service_account\" but found type: %s. "
+                        + "Use the Workload Identity Federation strategy for 
external account credentials.").formatted(foundType));

Review Comment:
   I think we should add this note only when the type is really 
`external_account`.



-- 
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]

Reply via email to