|
| 1 | +/** |
| 2 | + * Copyright 2024 Emmanuel Bourg |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package net.jsign.jca; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.io.IOException; |
| 21 | +import java.nio.file.Files; |
| 22 | +import java.security.KeyException; |
| 23 | +import java.security.PrivateKey; |
| 24 | +import java.util.Properties; |
| 25 | + |
| 26 | +import net.jsign.PrivateKeyUtils; |
| 27 | + |
| 28 | +/** |
| 29 | + * Oracle Cloud credentials loaded from the <code>.oci/config</code> file or from the environment variables. |
| 30 | + * |
| 31 | + * @since 6.1 |
| 32 | + */ |
| 33 | +public class OracleCloudCredentials { |
| 34 | + |
| 35 | + private String user; |
| 36 | + private String tenancy; |
| 37 | + private String region; |
| 38 | + private String keyfile; |
| 39 | + private String fingerprint; |
| 40 | + private String passphrase; |
| 41 | + private PrivateKey privateKey; |
| 42 | + |
| 43 | + public String getUser() { |
| 44 | + return user; |
| 45 | + } |
| 46 | + |
| 47 | + public String getTenancy() { |
| 48 | + return tenancy; |
| 49 | + } |
| 50 | + |
| 51 | + public String getRegion() { |
| 52 | + return region; |
| 53 | + } |
| 54 | + |
| 55 | + public String getKeyfile() { |
| 56 | + return keyfile; |
| 57 | + } |
| 58 | + |
| 59 | + public String getFingerprint() { |
| 60 | + return fingerprint; |
| 61 | + } |
| 62 | + |
| 63 | + public String getPassphrase() { |
| 64 | + return passphrase; |
| 65 | + } |
| 66 | + |
| 67 | + public String getKeyId() { |
| 68 | + return getTenancy() + "/" + getUser() + "/" + getFingerprint(); |
| 69 | + } |
| 70 | + |
| 71 | + PrivateKey getPrivateKey() { |
| 72 | + if (privateKey == null) { |
| 73 | + try { |
| 74 | + privateKey = PrivateKeyUtils.load(new File(getKeyfile()), getPassphrase()); |
| 75 | + } catch (KeyException e) { |
| 76 | + throw new RuntimeException("Unable to load the private key", e); |
| 77 | + } |
| 78 | + } |
| 79 | + return privateKey; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Loads the credentials from the specified file. |
| 84 | + * |
| 85 | + * @param file the configuration file (null for the default location) |
| 86 | + * @param profile the name of the profile (null for the default profile) |
| 87 | + */ |
| 88 | + public void load(File file, String profile) throws IOException { |
| 89 | + if (file == null) { |
| 90 | + file = getConfigFile(); |
| 91 | + } |
| 92 | + if (profile == null) { |
| 93 | + profile = getDefaultProfile(); |
| 94 | + } |
| 95 | + |
| 96 | + Properties properties = new Properties(); |
| 97 | + |
| 98 | + // parse le lines of the file |
| 99 | + boolean profileFound = false; |
| 100 | + for (String line : Files.readAllLines(file.toPath())) { |
| 101 | + if (profileFound && line.startsWith("[")) { |
| 102 | + break; // end of the profile |
| 103 | + } |
| 104 | + |
| 105 | + if (line.equals("[" + profile + "]")) { |
| 106 | + profileFound = true; |
| 107 | + continue; |
| 108 | + } |
| 109 | + |
| 110 | + if (profileFound) { |
| 111 | + String[] elements = line.split("=", 2); |
| 112 | + if (elements.length == 2) { |
| 113 | + properties.setProperty(elements[0].trim(), elements[1].trim()); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + if (!profileFound) { |
| 119 | + throw new IOException("Profile '" + profile + "' not found in " + file); |
| 120 | + } |
| 121 | + |
| 122 | + user = properties.getProperty("user"); |
| 123 | + tenancy = properties.getProperty("tenancy"); |
| 124 | + region = properties.getProperty("region"); |
| 125 | + keyfile = properties.getProperty("key_file"); |
| 126 | + fingerprint = properties.getProperty("fingerprint"); |
| 127 | + passphrase = properties.getProperty("pass_phrase"); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Loads the credentials from the environment variables. |
| 132 | + * |
| 133 | + * @see <a href="https://docs.oracle.com/en-us/iaas/Content/API/SDKDocs/clienvironmentvariables.htm">CLI Environment Variables</a> |
| 134 | + */ |
| 135 | + public void loadFromEnvironment() { |
| 136 | + if (getenv("OCI_CLI_USER") != null) { |
| 137 | + user = getenv("OCI_CLI_USER"); |
| 138 | + } |
| 139 | + if (getenv("OCI_CLI_TENANCY") != null) { |
| 140 | + tenancy = getenv("OCI_CLI_TENANCY"); |
| 141 | + } |
| 142 | + if (getenv("OCI_CLI_REGION") != null) { |
| 143 | + region = getenv("OCI_CLI_REGION"); |
| 144 | + } |
| 145 | + if (getenv("OCI_CLI_KEY_FILE") != null) { |
| 146 | + keyfile = getenv("OCI_CLI_KEY_FILE"); |
| 147 | + } |
| 148 | + if (getenv("OCI_CLI_FINGERPRINT") != null) { |
| 149 | + fingerprint = getenv("OCI_CLI_FINGERPRINT"); |
| 150 | + } |
| 151 | + if (getenv("OCI_CLI_PASS_PHRASE") != null) { |
| 152 | + passphrase = getenv("OCI_CLI_PASS_PHRASE"); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Returns the default Oracle Cloud configuration. |
| 158 | + */ |
| 159 | + public static OracleCloudCredentials getDefault() throws IOException { |
| 160 | + OracleCloudCredentials credentials = new OracleCloudCredentials(); |
| 161 | + File config = getConfigFile(); |
| 162 | + if (config.exists()) { |
| 163 | + credentials.load(config, getDefaultProfile()); |
| 164 | + } |
| 165 | + credentials.loadFromEnvironment(); |
| 166 | + return credentials; |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Returns the name of the default profile, either the value of the OCI_CLI_PROFILE environment variable or "DEFAULT". |
| 171 | + */ |
| 172 | + public static String getDefaultProfile() { |
| 173 | + String profile = getenv("OCI_CLI_PROFILE"); |
| 174 | + if (profile == null) { |
| 175 | + profile = "DEFAULT"; |
| 176 | + } |
| 177 | + return profile; |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Returns the location of the configuration file, either the value of the OCI_CLI_CONFIG_FILE environment variable |
| 182 | + * or <code>~/.oci/config</code>. |
| 183 | + */ |
| 184 | + public static File getConfigFile() { |
| 185 | + String config = getenv("OCI_CLI_CONFIG_FILE"); |
| 186 | + if (config != null) { |
| 187 | + return new File(config); |
| 188 | + } else { |
| 189 | + return new File(System.getProperty("user.home"), ".oci/config"); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + static String getenv(String name) { |
| 194 | + return System.getenv(name); |
| 195 | + } |
| 196 | +} |
0 commit comments