klion26 commented on code in PR #3396:
URL: https://github.com/apache/amoro/pull/3396#discussion_r1906266934


##########
amoro-common/src/main/java/org/apache/amoro/config/shade/utils/ConfigShadeUtils.java:
##########
@@ -0,0 +1,139 @@
+/*
+ * 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.amoro.config.shade.utils;
+
+import org.apache.amoro.config.ConfigOption;
+import org.apache.amoro.config.ConfigOptions;
+import org.apache.amoro.config.Configurations;
+import org.apache.amoro.config.shade.ConfigShade;
+import 
org.apache.amoro.shade.guava32.com.google.common.annotations.VisibleForTesting;
+import org.apache.amoro.shade.guava32.com.google.common.base.Preconditions;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.ServiceLoader;
+import java.util.function.BiFunction;
+
+/** Config shade utilities. */
+public final class ConfigShadeUtils {
+  private static final Logger LOG = 
LoggerFactory.getLogger(ConfigShadeUtils.class);
+
+  private static final Map<String, ConfigShade> CONFIG_SHADES = new 
HashMap<>();
+
+  private static final ConfigShade DEFAULT_SHADE = new DefaultConfigShade();
+
+  public static final ConfigOption<String> SHADE_IDENTIFIER_OPTION =
+      ConfigOptions.key("shade.identifier")
+          .stringType()
+          .defaultValue("default")
+          .withDescription(
+              "The identifier of the encryption method for decryption. 
Defaults to \"default\", indicating no encryption");
+
+  public static final ConfigOption<List<String>> SHADE_SENSITIVE_KEYWORDS =
+      ConfigOptions.key("shade.sensitive-keywords")
+          .stringType()
+          .asList()
+          .defaultValues("admin-password", "database.password")
+          .withDescription(
+              "A semicolon-separated list of keywords for the configuration 
items to be decrypted.");
+
+  static {
+    ServiceLoader<ConfigShade> serviceLoader = 
ServiceLoader.load(ConfigShade.class);
+    Iterator<ConfigShade> it = serviceLoader.iterator();
+    it.forEachRemaining(
+        configShade -> {
+          CONFIG_SHADES.put(configShade.getIdentifier(), configShade);
+        });
+    LOG.info("Load config shade spi: {}", CONFIG_SHADES.keySet());
+  }
+
+  @VisibleForTesting
+  public static String decryptOption(String identifier, String content) {
+    ConfigShade configShade = CONFIG_SHADES.getOrDefault(identifier, 
DEFAULT_SHADE);
+    return configShade.decrypt(content);
+  }
+
+  public static Map<String, Object> decryptConfig(Map<String, Object> 
configMap) throws Exception {
+    Configurations serviceConfig = Configurations.fromObjectMap(configMap);
+    String identifier = serviceConfig.get(SHADE_IDENTIFIER_OPTION);
+    List<String> sensitiveOptions = 
serviceConfig.get(SHADE_SENSITIVE_KEYWORDS);
+    return decryptConfig(identifier, configMap, sensitiveOptions, 
serviceConfig);
+  }
+
+  @SuppressWarnings("unchecked")

Review Comment:
   this annotation can be removed



##########
amoro-common/src/main/java/org/apache/amoro/config/shade/utils/ConfigShadeUtils.java:
##########
@@ -0,0 +1,139 @@
+/*
+ * 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.amoro.config.shade.utils;
+
+import org.apache.amoro.config.ConfigOption;
+import org.apache.amoro.config.ConfigOptions;
+import org.apache.amoro.config.Configurations;
+import org.apache.amoro.config.shade.ConfigShade;
+import 
org.apache.amoro.shade.guava32.com.google.common.annotations.VisibleForTesting;
+import org.apache.amoro.shade.guava32.com.google.common.base.Preconditions;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.ServiceLoader;
+import java.util.function.BiFunction;
+
+/** Config shade utilities. */
+public final class ConfigShadeUtils {
+  private static final Logger LOG = 
LoggerFactory.getLogger(ConfigShadeUtils.class);
+
+  private static final Map<String, ConfigShade> CONFIG_SHADES = new 
HashMap<>();
+
+  private static final ConfigShade DEFAULT_SHADE = new DefaultConfigShade();
+
+  public static final ConfigOption<String> SHADE_IDENTIFIER_OPTION =
+      ConfigOptions.key("shade.identifier")
+          .stringType()
+          .defaultValue("default")
+          .withDescription(
+              "The identifier of the encryption method for decryption. 
Defaults to \"default\", indicating no encryption");
+
+  public static final ConfigOption<List<String>> SHADE_SENSITIVE_KEYWORDS =
+      ConfigOptions.key("shade.sensitive-keywords")
+          .stringType()
+          .asList()
+          .defaultValues("admin-password", "database.password")

Review Comment:
   seems the the key here is only located in `ams` part in the configuration, 
not sure if this needs to be apply the whole config



##########
amoro-common/src/main/java/org/apache/amoro/config/shade/utils/ConfigShadeUtils.java:
##########
@@ -0,0 +1,139 @@
+/*
+ * 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.amoro.config.shade.utils;
+
+import org.apache.amoro.config.ConfigOption;
+import org.apache.amoro.config.ConfigOptions;
+import org.apache.amoro.config.Configurations;
+import org.apache.amoro.config.shade.ConfigShade;
+import 
org.apache.amoro.shade.guava32.com.google.common.annotations.VisibleForTesting;
+import org.apache.amoro.shade.guava32.com.google.common.base.Preconditions;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.ServiceLoader;
+import java.util.function.BiFunction;
+
+/** Config shade utilities. */
+public final class ConfigShadeUtils {
+  private static final Logger LOG = 
LoggerFactory.getLogger(ConfigShadeUtils.class);
+
+  private static final Map<String, ConfigShade> CONFIG_SHADES = new 
HashMap<>();
+
+  private static final ConfigShade DEFAULT_SHADE = new DefaultConfigShade();
+
+  public static final ConfigOption<String> SHADE_IDENTIFIER_OPTION =
+      ConfigOptions.key("shade.identifier")
+          .stringType()
+          .defaultValue("default")
+          .withDescription(
+              "The identifier of the encryption method for decryption. 
Defaults to \"default\", indicating no encryption");
+
+  public static final ConfigOption<List<String>> SHADE_SENSITIVE_KEYWORDS =
+      ConfigOptions.key("shade.sensitive-keywords")
+          .stringType()
+          .asList()
+          .defaultValues("admin-password", "database.password")
+          .withDescription(
+              "A semicolon-separated list of keywords for the configuration 
items to be decrypted.");
+
+  static {
+    ServiceLoader<ConfigShade> serviceLoader = 
ServiceLoader.load(ConfigShade.class);
+    Iterator<ConfigShade> it = serviceLoader.iterator();
+    it.forEachRemaining(
+        configShade -> {
+          CONFIG_SHADES.put(configShade.getIdentifier(), configShade);

Review Comment:
   Do we need to log the relationship for the identifier and the configShade 
here? I assume the shade loaded here would not a big number.



##########
amoro-common/src/main/java/org/apache/amoro/config/shade/ConfigShade.java:
##########
@@ -0,0 +1,52 @@
+/*
+ * 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.amoro.config.shade;
+
+import org.apache.amoro.config.Configurations;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * The interface that provides the ability to decrypt {@link
+ * org.apache.amoro.config.Configurations}.
+ */
+public interface ConfigShade {
+  Logger LOG = LoggerFactory.getLogger(ConfigShade.class);

Review Comment:
   seems the `LOG` here is no more used



##########
amoro-ams/src/test/java/org/apache/amoro/server/config/TestConfigShade.java:
##########
@@ -0,0 +1,122 @@
+/*
+ * 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.amoro.server.config;
+
+import static org.apache.amoro.server.AmoroServiceContainer.expandConfigMap;
+
+import org.apache.amoro.config.shade.utils.ConfigShadeUtils;
+import org.apache.amoro.server.AmoroManagementConf;
+import org.apache.amoro.shade.guava32.com.google.common.collect.ImmutableMap;
+import org.apache.amoro.shade.guava32.com.google.common.collect.Maps;
+import org.apache.amoro.shade.guava32.com.google.common.io.Resources;
+import 
org.apache.amoro.shade.jackson2.com.fasterxml.jackson.core.type.TypeReference;
+import org.apache.amoro.shade.jackson2.com.fasterxml.jackson.databind.JsonNode;
+import org.apache.amoro.utils.JacksonUtil;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.yaml.snakeyaml.Yaml;
+
+import java.net.URL;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.Map;
+
+public class TestConfigShade {
+  private static final String USERNAME = "admin";
+  private static final String PASSWORD = "password";
+
+  @Test
+  public void testDecryptOptions() {
+    String encryptUsername = "YWRtaW4=";

Review Comment:
   we could add some comments here to show that these are the message have been 
transformed by base64



##########
amoro-common/src/main/java/org/apache/amoro/config/shade/utils/ConfigShadeUtils.java:
##########
@@ -0,0 +1,139 @@
+/*
+ * 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.amoro.config.shade.utils;
+
+import org.apache.amoro.config.ConfigOption;
+import org.apache.amoro.config.ConfigOptions;
+import org.apache.amoro.config.Configurations;
+import org.apache.amoro.config.shade.ConfigShade;
+import 
org.apache.amoro.shade.guava32.com.google.common.annotations.VisibleForTesting;
+import org.apache.amoro.shade.guava32.com.google.common.base.Preconditions;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.ServiceLoader;
+import java.util.function.BiFunction;
+
+/** Config shade utilities. */
+public final class ConfigShadeUtils {
+  private static final Logger LOG = 
LoggerFactory.getLogger(ConfigShadeUtils.class);
+
+  private static final Map<String, ConfigShade> CONFIG_SHADES = new 
HashMap<>();
+
+  private static final ConfigShade DEFAULT_SHADE = new DefaultConfigShade();
+
+  public static final ConfigOption<String> SHADE_IDENTIFIER_OPTION =
+      ConfigOptions.key("shade.identifier")
+          .stringType()
+          .defaultValue("default")
+          .withDescription(
+              "The identifier of the encryption method for decryption. 
Defaults to \"default\", indicating no encryption");
+
+  public static final ConfigOption<List<String>> SHADE_SENSITIVE_KEYWORDS =
+      ConfigOptions.key("shade.sensitive-keywords")
+          .stringType()
+          .asList()
+          .defaultValues("admin-password", "database.password")
+          .withDescription(
+              "A semicolon-separated list of keywords for the configuration 
items to be decrypted.");
+
+  static {
+    ServiceLoader<ConfigShade> serviceLoader = 
ServiceLoader.load(ConfigShade.class);
+    Iterator<ConfigShade> it = serviceLoader.iterator();
+    it.forEachRemaining(
+        configShade -> {
+          CONFIG_SHADES.put(configShade.getIdentifier(), configShade);
+        });
+    LOG.info("Load config shade spi: {}", CONFIG_SHADES.keySet());
+  }
+
+  @VisibleForTesting
+  public static String decryptOption(String identifier, String content) {
+    ConfigShade configShade = CONFIG_SHADES.getOrDefault(identifier, 
DEFAULT_SHADE);

Review Comment:
   do we need to add some logs here if there is no `ConfigShade` with the given 
`identifier`



##########
amoro-common/src/main/java/org/apache/amoro/config/shade/utils/ConfigShadeUtils.java:
##########
@@ -0,0 +1,139 @@
+/*
+ * 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.amoro.config.shade.utils;
+
+import org.apache.amoro.config.ConfigOption;
+import org.apache.amoro.config.ConfigOptions;
+import org.apache.amoro.config.Configurations;
+import org.apache.amoro.config.shade.ConfigShade;
+import 
org.apache.amoro.shade.guava32.com.google.common.annotations.VisibleForTesting;
+import org.apache.amoro.shade.guava32.com.google.common.base.Preconditions;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.ServiceLoader;
+import java.util.function.BiFunction;
+
+/** Config shade utilities. */
+public final class ConfigShadeUtils {
+  private static final Logger LOG = 
LoggerFactory.getLogger(ConfigShadeUtils.class);
+
+  private static final Map<String, ConfigShade> CONFIG_SHADES = new 
HashMap<>();
+
+  private static final ConfigShade DEFAULT_SHADE = new DefaultConfigShade();
+
+  public static final ConfigOption<String> SHADE_IDENTIFIER_OPTION =
+      ConfigOptions.key("shade.identifier")
+          .stringType()
+          .defaultValue("default")
+          .withDescription(
+              "The identifier of the encryption method for decryption. 
Defaults to \"default\", indicating no encryption");
+
+  public static final ConfigOption<List<String>> SHADE_SENSITIVE_KEYWORDS =
+      ConfigOptions.key("shade.sensitive-keywords")
+          .stringType()
+          .asList()
+          .defaultValues("admin-password", "database.password")
+          .withDescription(
+              "A semicolon-separated list of keywords for the configuration 
items to be decrypted.");
+
+  static {
+    ServiceLoader<ConfigShade> serviceLoader = 
ServiceLoader.load(ConfigShade.class);
+    Iterator<ConfigShade> it = serviceLoader.iterator();
+    it.forEachRemaining(
+        configShade -> {
+          CONFIG_SHADES.put(configShade.getIdentifier(), configShade);
+        });
+    LOG.info("Load config shade spi: {}", CONFIG_SHADES.keySet());
+  }
+
+  @VisibleForTesting
+  public static String decryptOption(String identifier, String content) {
+    ConfigShade configShade = CONFIG_SHADES.getOrDefault(identifier, 
DEFAULT_SHADE);
+    return configShade.decrypt(content);
+  }
+
+  public static Map<String, Object> decryptConfig(Map<String, Object> 
configMap) throws Exception {
+    Configurations serviceConfig = Configurations.fromObjectMap(configMap);
+    String identifier = serviceConfig.get(SHADE_IDENTIFIER_OPTION);
+    List<String> sensitiveOptions = 
serviceConfig.get(SHADE_SENSITIVE_KEYWORDS);
+    return decryptConfig(identifier, configMap, sensitiveOptions, 
serviceConfig);
+  }
+
+  @SuppressWarnings("unchecked")
+  public static Map<String, Object> decryptConfig(
+      String identifier,
+      Map<String, Object> configMap,
+      List<String> sensitiveOptions,
+      Configurations serviceConfig)
+      throws Exception {
+    ConfigShade configShade = CONFIG_SHADES.get(identifier);
+    if (configShade == null) {
+      LOG.error("Can not find config shade: {}", identifier);
+      throw new IllegalStateException("Can not find config shade: " + 
identifier);
+    }
+    configShade.initialize(serviceConfig);
+
+    if (DEFAULT_SHADE.getIdentifier().equals(configShade.getIdentifier())) {
+      return configMap;
+    }
+
+    LOG.info("Use config shade: {}", identifier);
+    BiFunction<String, Object, String> processFunction =
+        (key, value) -> configShade.decrypt(value.toString());
+
+    Preconditions.checkArgument(

Review Comment:
   why do we need to check this here



##########
amoro-ams/src/test/java/org/apache/amoro/server/config/TestConfigShade.java:
##########
@@ -0,0 +1,122 @@
+/*
+ * 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.amoro.server.config;
+
+import static org.apache.amoro.server.AmoroServiceContainer.expandConfigMap;
+
+import org.apache.amoro.config.shade.utils.ConfigShadeUtils;
+import org.apache.amoro.server.AmoroManagementConf;
+import org.apache.amoro.shade.guava32.com.google.common.collect.ImmutableMap;
+import org.apache.amoro.shade.guava32.com.google.common.collect.Maps;
+import org.apache.amoro.shade.guava32.com.google.common.io.Resources;
+import 
org.apache.amoro.shade.jackson2.com.fasterxml.jackson.core.type.TypeReference;
+import org.apache.amoro.shade.jackson2.com.fasterxml.jackson.databind.JsonNode;
+import org.apache.amoro.utils.JacksonUtil;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.yaml.snakeyaml.Yaml;
+
+import java.net.URL;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.Map;
+
+public class TestConfigShade {
+  private static final String USERNAME = "admin";
+  private static final String PASSWORD = "password";
+
+  @Test
+  public void testDecryptOptions() {
+    String encryptUsername = "YWRtaW4=";
+    String encryptPassword = "cGFzc3dvcmQ=";
+    String decryptUsername = ConfigShadeUtils.decryptOption("base64", 
encryptUsername);

Review Comment:
   do we need to change the identifier to `Base64ConfigShade.getIdentifier()` 
here,



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