Repository: microsoft/kafka-connect-cosmosdb
Pull request #188 (2020-12-14 17:52:17)

Fix the config for the master key being a simple string
## Type of PR

- [ ] Documentation changes
- [X] Code changes
- [X] Test changes
- [ ] CI-CD changes
- [ ] GitHub Template changes

## Purpose of PR
The master key config was treated as a string config, which could display in logs the key. There is a config type that prevents that happening, so we use that.

## Review notes

## Issues Closed or Referenced



Patch:
diff --git a/src/main/java/com/microsoft/azure/cosmosdb/kafka/connect/PasswordSetting.java b/src/main/java/com/microsoft/azure/cosmosdb/kafka/connect/PasswordSetting.java
new file mode 100644
index 0000000..27dd358
--- /dev/null
+++ b/src/main/java/com/microsoft/azure/cosmosdb/kafka/connect/PasswordSetting.java
@@ -0,0 +1,18 @@
+package com.microsoft.azure.cosmosdb.kafka.connect;
+
+import org.apache.kafka.common.config.ConfigDef;
+
+import java.util.function.Consumer;
+import java.util.function.Supplier;
+
+public class PasswordSetting extends Setting {
+
+    public PasswordSetting(String name, String documentation, String displayName, Consumer<String> modifier, Supplier<String> accessor) {
+        super(name, documentation, displayName, modifier, accessor);
+    }
+
+    @Override
+    protected ConfigDef.Type getKafkaConfigType() {
+        return ConfigDef.Type.PASSWORD;
+    }
+}
diff --git a/src/main/java/com/microsoft/azure/cosmosdb/kafka/connect/Settings.java b/src/main/java/com/microsoft/azure/cosmosdb/kafka/connect/Settings.java
index 9a6dbb7..acea94f 100644
--- a/src/main/java/com/microsoft/azure/cosmosdb/kafka/connect/Settings.java
+++ b/src/main/java/com/microsoft/azure/cosmosdb/kafka/connect/Settings.java
@@ -25,7 +25,7 @@ public class Settings {
     private final List<Setting> allSettings = Arrays.asList(
             //Add all common Source and Sink settings here:
             new Setting(PREFIX + ".connection.endpoint", "The Cosmos DB endpoint.", "CosmosDB Endpoint", this::setEndpoint, this::getEndpoint),
-            new Setting(PREFIX + ".master.key", "The connection master key.", "Master Key", this::setKey, this::getKey),
+            new PasswordSetting(PREFIX + ".master.key", "The connection master key.", "Master Key", this::setKey, this::getKey),
             new Setting(PREFIX + ".databasename", "The Cosmos DB target database.", "CosmosDB Database Name", this::setDatabaseName, this::getDatabaseName),
             new Setting(PREFIX + ".containers.topicmap", "A comma delimited list of containers mapped to their topic partitions. Example: topic1#con1,topic2#con2.",
                     "Topic-Container map", value -> this.setTopicContainerMap(TopicContainerMap.deserialize(value)), ()->this.getTopicContainerMap().serialize())


Test patch:
diff --git a/src/test/java/com/microsoft/azure/cosmosdb/kafka/connect/SettingsTest.java b/src/test/java/com/microsoft/azure/cosmosdb/kafka/connect/SettingsTest.java
new file mode 100644
index 0000000..1333c43
--- /dev/null
+++ b/src/test/java/com/microsoft/azure/cosmosdb/kafka/connect/SettingsTest.java
@@ -0,0 +1,28 @@
+package com.microsoft.azure.cosmosdb.kafka.connect;
+
+import org.apache.kafka.common.config.ConfigDef;
+import org.junit.Test;
+
+import java.util.HashMap;
+
+import static org.junit.Assert.assertEquals;
+
+public class SettingsTest {
+
+    @Test
+    public void verifyMasterKeyIsPasswordType() {
+        HashMap<String, String> common = new HashMap<>();
+        common.put(Settings.PREFIX + ".master.key", "foobar");
+
+        Settings settings = new Settings();
+        settings.populate(common);
+
+        assertEquals("foobar", settings.getKey());
+        assertEquals(ConfigDef.Type.PASSWORD, settings.getAllSettings()
+                .stream()
+                .filter(s -> s.getName().equals(Settings.PREFIX + ".master.key"))
+                .map(Setting::getKafkaConfigType)
+                .findAny()
+                .orElse(null));
+    }
+}