From 6c057df8a56bc7ceb52e3d8f9cfc3c30329c777c Mon Sep 17 00:00:00 2001 From: Cameron Bateman Date: Thu, 25 Jul 2024 01:40:53 -0700 Subject: [PATCH 1/3] Improve config file reading by exposing parsed profile names and values. Needed by tools mainly. Signed-off-by: Cameron Bateman --- .../java/com/oracle/bmc/ConfigFileReader.java | 34 +++++++-- .../com/oracle/bmc/ExpectedConfigValues2.java | 24 +++++++ .../com/oracle/bmc/ConfigFileReaderTest2.java | 71 +++++++++++++++++++ bmc-common/src/test/resources/oci.config | 23 ++++++ 4 files changed, 146 insertions(+), 6 deletions(-) create mode 100644 bmc-common/src/main/java/com/oracle/bmc/ExpectedConfigValues2.java create mode 100644 bmc-common/src/test/java/com/oracle/bmc/ConfigFileReaderTest2.java create mode 100644 bmc-common/src/test/resources/oci.config diff --git a/bmc-common/src/main/java/com/oracle/bmc/ConfigFileReader.java b/bmc-common/src/main/java/com/oracle/bmc/ConfigFileReader.java index 5061153c386..7e356a94d0e 100644 --- a/bmc-common/src/main/java/com/oracle/bmc/ConfigFileReader.java +++ b/bmc-common/src/main/java/com/oracle/bmc/ConfigFileReader.java @@ -1,13 +1,15 @@ /** * Copyright (c) 2016, 2024, Oracle and/or its affiliates. All rights reserved. * This software is dual-licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose either license. + + * C.B 2024: copied from OCI SDK, copyright notice unchanged. We will try to patch this change back into + * OCI (https://github.com/oracle/oci-java-sdk). This change exposes the profile names and key/value + * pairs that are already being parsed in. This is needed by tools (IntelliJ) and admin tools. */ package com.oracle.bmc; -import org.slf4j.Logger; +import static com.oracle.bmc.util.internal.FileUtils.expandUserHome; -import jakarta.annotation.Nonnull; -import jakarta.annotation.Nullable; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; @@ -16,14 +18,18 @@ import java.io.InputStreamReader; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; +import java.util.Collections; import java.util.HashMap; import java.util.Map; +import java.util.Optional; +import java.util.Set; -import jakarta.annotation.Nonnull; -import jakarta.annotation.Nullable; +import org.slf4j.Logger; import com.oracle.bmc.util.internal.StringUtils; -import static com.oracle.bmc.util.internal.FileUtils.expandUserHome; + +import jakarta.annotation.Nonnull; +import jakarta.annotation.Nullable; /** * Simple implementation to read OCI configuration files. @@ -197,6 +203,22 @@ public String get(String key) { : null; } + public Set getProfileNames() { + return Collections.unmodifiableSet(accumulator.configurationsByProfile.keySet()); + } + + public Map getProfileKeys(String profileName) { + Map keyValues = new HashMap<>(); + if (accumulator.foundDefaultProfile) { + keyValues.putAll(accumulator.configurationsByProfile.get(DEFAULT_PROFILE_NAME)); + } + Map overrideKeyValues = + Optional.ofNullable(accumulator.configurationsByProfile.get(profileName)) + .orElse(Collections.emptyMap()); + keyValues.putAll(overrideKeyValues); + return Collections.unmodifiableMap(keyValues); + } + @Override public String toString() { StringBuilder sb = new StringBuilder(); diff --git a/bmc-common/src/main/java/com/oracle/bmc/ExpectedConfigValues2.java b/bmc-common/src/main/java/com/oracle/bmc/ExpectedConfigValues2.java new file mode 100644 index 00000000000..603c0127e11 --- /dev/null +++ b/bmc-common/src/main/java/com/oracle/bmc/ExpectedConfigValues2.java @@ -0,0 +1,24 @@ +package com.oracle.bmc; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class ExpectedConfigValues2 { + + public static List getExpectedProfileNames() { + return Arrays.asList("DEFAULT", "ALWAYSFREE", "ONPREMJAVA", "ASHBURN"); + } + + public static Map createDefaultExpectedMap() { + Map expectedMap = new HashMap<>(); + expectedMap.put("user", "ocid1.user.oc1..aaaaaaaarqnt5lacwpgxa3wqsmec5w22wv3teohuflvdcrcd74agvsiq7upq"); + expectedMap.put("fingerprint", "53:35:b4:2d:c7:ca:ca:2b:86:53:3f:e5:56:cf:eb:21"); + expectedMap.put("tenancy", "ocid1.tenancy.oc1..aaaaaaaafqtdpajmhdh5drcxtiv3gadg6v56p4g4g7svx4vmf7k7srl6zwwa"); + expectedMap.put("region", "us-phoenix-1"); + expectedMap.put("key_file", "/Users/cbateman/.oci/cameron.bateman-06-16-16-40.pem"); + return expectedMap; + } + +} diff --git a/bmc-common/src/test/java/com/oracle/bmc/ConfigFileReaderTest2.java b/bmc-common/src/test/java/com/oracle/bmc/ConfigFileReaderTest2.java new file mode 100644 index 00000000000..753590123a5 --- /dev/null +++ b/bmc-common/src/test/java/com/oracle/bmc/ConfigFileReaderTest2.java @@ -0,0 +1,71 @@ +package com.oracle.bmc; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.oracle.bmc.ConfigFileReader.ConfigFile; + +public class ConfigFileReaderTest2 { + + private static File CONFIG_FILE; + + @BeforeClass + public static void beforeClass() { + CONFIG_FILE = new File("./src/test/resources/oci.config"); + assertTrue(CONFIG_FILE.isFile()); + } + + private ConfigFile configFile; + @Before + public void before() throws IOException { + this.configFile = ConfigFileReader.parse(CONFIG_FILE.getAbsolutePath()); + assertNotNull(configFile); + } + + @Test + public void testProfileNames() { + Set actualProfileNames = configFile.getProfileNames(); + List expectedProfileNames = ExpectedConfigValues2.getExpectedProfileNames();; + assertEquals(expectedProfileNames.size(), configFile.getProfileNames().size()); + actualProfileNames.forEach(pname -> assertTrue(expectedProfileNames.contains(pname))); + } + @Test + public void testDefaultConfig() throws IOException { + assertDefaultProfile(configFile); + } + + @Test + public void testAshburnOverrideConfig() throws IOException { + assertAshburnOverridsDefault(configFile); + } + + private void assertAshburnOverridsDefault(ConfigFile configFileProfiles) { + Map profileKeys = configFileProfiles.getProfileKeys("ASHBURN"); + Map expectedMap = ExpectedConfigValues2.createDefaultExpectedMap(); + + // override region + expectedMap.put("region", "us-ashburn-1"); + assertEquals(expectedMap, profileKeys); + } + + private void assertDefaultProfile(ConfigFile configFileProfiles) { + Map profileKeys = configFileProfiles.getProfileKeys("DEFAULT"); + Map expectedMap = ExpectedConfigValues2.createDefaultExpectedMap(); + assertEquals(expectedMap, profileKeys); + } + + + + +} diff --git a/bmc-common/src/test/resources/oci.config b/bmc-common/src/test/resources/oci.config new file mode 100644 index 00000000000..6eff7712c91 --- /dev/null +++ b/bmc-common/src/test/resources/oci.config @@ -0,0 +1,23 @@ +[DEFAULT] +user=ocid1.user.oc1..aaaaaaaarqnt5lacwpgxa3wqsmec5w22wv3teohuflvdcrcd74agvsiq7upq +fingerprint=53:35:b4:2d:c7:ca:ca:2b:86:53:3f:e5:56:cf:eb:21 +tenancy=ocid1.tenancy.oc1..aaaaaaaafqtdpajmhdh5drcxtiv3gadg6v56p4g4g7svx4vmf7k7srl6zwwa +region=us-phoenix-1 +key_file=/Users/cbateman/.oci/cameron.bateman-06-16-16-40.pem + +[ALWAYSFREE] +user=ocid1.user.oc1..aaaaaaaarqnt5lacwpgxa3wqsmec5w22wv3teohuflvdcrcd74agvsiq7upq +fingerprint=53:35:b4:2d:c7:ca:ca:2b:86:53:3f:e5:56:cf:eb:21 +tenancy=ocid1.tenancy.oc1..aaaaaaaafqtdpajmhdh5drcxtiv3gadg6v56p4g4g7svx4vmf7k7srl6zwwa +region=us-ashburn-1 +key_file=/Users/cbateman/.oci/cameron.bateman-06-16-16-40.pem + +[ONPREMJAVA] +user=ocid1.user.oc1..aaaaaaaakk4xu76nxwfosexpzpap3c7qd7hjeiuursbj46cnmsrgcwveelua +fingerprint=ad:06:d2:26:52:d5:de:c0:6f:51:37:85:f5:f8:a8:f7 +tenancy=ocid1.tenancy.oc1..aaaaaaaagojvam7c7hthdm7h2pgshjmiqntcvei4skgysz3galuejn3rioia +region=us-phoenix-1 +key_file=/Users/cbateman/.oci/cameron.bateman-11-16-08-01.pem + +[ASHBURN] +region=us-ashburn-1 \ No newline at end of file From 326db6d1fb7e4c5c8cb9f9b84c4588989995f800 Mon Sep 17 00:00:00 2001 From: Cameron Bateman Date: Thu, 25 Jul 2024 01:46:19 -0700 Subject: [PATCH 2/3] Fixed location of test data class and removed additional header info. Signed-off-by: Cameron Bateman --- bmc-common/src/main/java/com/oracle/bmc/ConfigFileReader.java | 4 ---- .../java/com/oracle/bmc/ExpectedConfigValues2.java | 0 2 files changed, 4 deletions(-) rename bmc-common/src/{main => test}/java/com/oracle/bmc/ExpectedConfigValues2.java (100%) diff --git a/bmc-common/src/main/java/com/oracle/bmc/ConfigFileReader.java b/bmc-common/src/main/java/com/oracle/bmc/ConfigFileReader.java index 7e356a94d0e..375c65736bc 100644 --- a/bmc-common/src/main/java/com/oracle/bmc/ConfigFileReader.java +++ b/bmc-common/src/main/java/com/oracle/bmc/ConfigFileReader.java @@ -1,10 +1,6 @@ /** * Copyright (c) 2016, 2024, Oracle and/or its affiliates. All rights reserved. * This software is dual-licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose either license. - - * C.B 2024: copied from OCI SDK, copyright notice unchanged. We will try to patch this change back into - * OCI (https://github.com/oracle/oci-java-sdk). This change exposes the profile names and key/value - * pairs that are already being parsed in. This is needed by tools (IntelliJ) and admin tools. */ package com.oracle.bmc; diff --git a/bmc-common/src/main/java/com/oracle/bmc/ExpectedConfigValues2.java b/bmc-common/src/test/java/com/oracle/bmc/ExpectedConfigValues2.java similarity index 100% rename from bmc-common/src/main/java/com/oracle/bmc/ExpectedConfigValues2.java rename to bmc-common/src/test/java/com/oracle/bmc/ExpectedConfigValues2.java From 1a685e974e6d5067b88f8cce1251d5a92bf09838 Mon Sep 17 00:00:00 2001 From: Cameron Bateman Date: Thu, 25 Jul 2024 10:21:57 -0700 Subject: [PATCH 3/3] Fix tests. Signed-off-by: Cameron Bateman --- ...rTest2.java => ConfigFileReader2Test.java} | 2 +- .../com/oracle/bmc/ExpectedConfigValues2.java | 8 +++---- bmc-common/src/test/resources/oci.config | 24 +++++++++---------- 3 files changed, 17 insertions(+), 17 deletions(-) rename bmc-common/src/test/java/com/oracle/bmc/{ConfigFileReaderTest2.java => ConfigFileReader2Test.java} (98%) diff --git a/bmc-common/src/test/java/com/oracle/bmc/ConfigFileReaderTest2.java b/bmc-common/src/test/java/com/oracle/bmc/ConfigFileReader2Test.java similarity index 98% rename from bmc-common/src/test/java/com/oracle/bmc/ConfigFileReaderTest2.java rename to bmc-common/src/test/java/com/oracle/bmc/ConfigFileReader2Test.java index 753590123a5..ad25fd002f8 100644 --- a/bmc-common/src/test/java/com/oracle/bmc/ConfigFileReaderTest2.java +++ b/bmc-common/src/test/java/com/oracle/bmc/ConfigFileReader2Test.java @@ -16,7 +16,7 @@ import com.oracle.bmc.ConfigFileReader.ConfigFile; -public class ConfigFileReaderTest2 { +public class ConfigFileReader2Test { private static File CONFIG_FILE; diff --git a/bmc-common/src/test/java/com/oracle/bmc/ExpectedConfigValues2.java b/bmc-common/src/test/java/com/oracle/bmc/ExpectedConfigValues2.java index 603c0127e11..510e0f0d87e 100644 --- a/bmc-common/src/test/java/com/oracle/bmc/ExpectedConfigValues2.java +++ b/bmc-common/src/test/java/com/oracle/bmc/ExpectedConfigValues2.java @@ -13,11 +13,11 @@ public static List getExpectedProfileNames() { public static Map createDefaultExpectedMap() { Map expectedMap = new HashMap<>(); - expectedMap.put("user", "ocid1.user.oc1..aaaaaaaarqnt5lacwpgxa3wqsmec5w22wv3teohuflvdcrcd74agvsiq7upq"); - expectedMap.put("fingerprint", "53:35:b4:2d:c7:ca:ca:2b:86:53:3f:e5:56:cf:eb:21"); - expectedMap.put("tenancy", "ocid1.tenancy.oc1..aaaaaaaafqtdpajmhdh5drcxtiv3gadg6v56p4g4g7svx4vmf7k7srl6zwwa"); + expectedMap.put("user", "ocid1.user.oc1..aaaaaaaarqntblargh"); + expectedMap.put("fingerprint", "xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"); + expectedMap.put("tenancy", "ocid1.tenancy.oc1.aaaaablah"); expectedMap.put("region", "us-phoenix-1"); - expectedMap.put("key_file", "/Users/cbateman/.oci/cameron.bateman-06-16-16-40.pem"); + expectedMap.put("key_file", "/Users/homer/.oci/hsimpson/homer-simpson-06-16-12.pem"); return expectedMap; } diff --git a/bmc-common/src/test/resources/oci.config b/bmc-common/src/test/resources/oci.config index 6eff7712c91..785e32573bf 100644 --- a/bmc-common/src/test/resources/oci.config +++ b/bmc-common/src/test/resources/oci.config @@ -1,23 +1,23 @@ [DEFAULT] -user=ocid1.user.oc1..aaaaaaaarqnt5lacwpgxa3wqsmec5w22wv3teohuflvdcrcd74agvsiq7upq -fingerprint=53:35:b4:2d:c7:ca:ca:2b:86:53:3f:e5:56:cf:eb:21 -tenancy=ocid1.tenancy.oc1..aaaaaaaafqtdpajmhdh5drcxtiv3gadg6v56p4g4g7svx4vmf7k7srl6zwwa +user=ocid1.user.oc1..aaaaaaaarqntblargh +fingerprint=xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx +tenancy=ocid1.tenancy.oc1.aaaaablah region=us-phoenix-1 -key_file=/Users/cbateman/.oci/cameron.bateman-06-16-16-40.pem +key_file=/Users/homer/.oci/hsimpson/homer-simpson-06-16-12.pem [ALWAYSFREE] -user=ocid1.user.oc1..aaaaaaaarqnt5lacwpgxa3wqsmec5w22wv3teohuflvdcrcd74agvsiq7upq -fingerprint=53:35:b4:2d:c7:ca:ca:2b:86:53:3f:e5:56:cf:eb:21 -tenancy=ocid1.tenancy.oc1..aaaaaaaafqtdpajmhdh5drcxtiv3gadg6v56p4g4g7svx4vmf7k7srl6zwwa +user=ocid1.user.oc1..aaaaaaaarqntblargh +fingerprint=xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx +tenancy=ocid1.tenancy.oc1.aaaaablah region=us-ashburn-1 -key_file=/Users/cbateman/.oci/cameron.bateman-06-16-16-40.pem +key_file=/Users/homer/.oci/hsimpson/homer-simpson-06-16-12.pem [ONPREMJAVA] -user=ocid1.user.oc1..aaaaaaaakk4xu76nxwfosexpzpap3c7qd7hjeiuursbj46cnmsrgcwveelua -fingerprint=ad:06:d2:26:52:d5:de:c0:6f:51:37:85:f5:f8:a8:f7 -tenancy=ocid1.tenancy.oc1..aaaaaaaagojvam7c7hthdm7h2pgshjmiqntcvei4skgysz3galuejn3rioia +user=ocid1.user.oc1..aaaaaaaarqntblargh22 +fingerprint=xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx +tenancy=ocid1.tenancy.oc1.aaaaablah2 region=us-phoenix-1 -key_file=/Users/cbateman/.oci/cameron.bateman-11-16-08-01.pem +key_file=/Users/lisa/.oci/lsimpson/lisa-simpson-04-06-12.pem [ASHBURN] region=us-ashburn-1 \ No newline at end of file