|
| 1 | +package oracle.jdbc.provider.pkl.configuration.parser; |
| 2 | + |
| 3 | +import oracle.jdbc.provider.pkl.configuration.parser.generated.OjdbcConfig; |
| 4 | +import oracle.jdbc.spi.OracleConfigurationParser; |
| 5 | +import oracle.jdbc.spi.OracleConfigurationSecretProvider; |
| 6 | +import org.pkl.config.java.Config; |
| 7 | +import org.pkl.config.java.ConfigEvaluator; |
| 8 | +import org.pkl.config.java.NoSuchChildException; |
| 9 | +import org.pkl.core.Duration; |
| 10 | +import org.pkl.core.ModuleSource; |
| 11 | +import org.pkl.core.PNull; |
| 12 | + |
| 13 | +import java.io.IOException; |
| 14 | +import java.io.InputStream; |
| 15 | +import java.lang.reflect.Field; |
| 16 | +import java.nio.charset.StandardCharsets; |
| 17 | +import java.sql.SQLException; |
| 18 | +import java.util.Base64; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.Properties; |
| 22 | + |
| 23 | +import static oracle.jdbc.OracleConnection.CONNECTION_PROPERTY_PASSWORD; |
| 24 | +import static oracle.jdbc.OracleConnection.CONNECTION_PROPERTY_WALLET_LOCATION; |
| 25 | + |
| 26 | +public class PklParser implements OracleConfigurationParser { |
| 27 | + @Override |
| 28 | + public Properties parse( |
| 29 | + InputStream inputStream, Map<String, String> options) throws SQLException { |
| 30 | + Properties properties = new Properties(); |
| 31 | + Config pklConfig; |
| 32 | + |
| 33 | + // Parse Pkl config from String |
| 34 | + try (ConfigEvaluator evaluator = ConfigEvaluator.preconfigured()) { |
| 35 | + String fileString = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8); |
| 36 | + pklConfig = evaluator.evaluate(ModuleSource.text(fileString)); |
| 37 | + } catch (IOException e) { |
| 38 | + throw new SQLException(e); |
| 39 | + } |
| 40 | + |
| 41 | + // If key is present, we parse that pkl class |
| 42 | + String key = options.get("key"); |
| 43 | + if (key != null) { |
| 44 | + try { |
| 45 | + pklConfig = pklConfig.get(key); |
| 46 | + } catch (NoSuchChildException e) { |
| 47 | + throw new IllegalArgumentException( |
| 48 | + key + " key appears in URL but is missing in module."); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + // User common connect_descriptor as the URL |
| 53 | + OjdbcConfig ojdbcConfig = pklConfig.as(OjdbcConfig.class); |
| 54 | + |
| 55 | + if (ojdbcConfig.connect_descriptor != null) |
| 56 | + properties.put("URL", |
| 57 | + "jdbc:oracle:thin:@" + ojdbcConfig.connect_descriptor); |
| 58 | + |
| 59 | + if (ojdbcConfig.user != null) |
| 60 | + properties.put("user", ojdbcConfig.user); |
| 61 | + |
| 62 | + if (ojdbcConfig.password != null) { |
| 63 | + // Get password from the provider |
| 64 | + String passwordProviderType = ojdbcConfig.password.type; |
| 65 | + OracleConfigurationSecretProvider passwordSecretProvider = |
| 66 | + OracleConfigurationSecretProvider.find(passwordProviderType); |
| 67 | + |
| 68 | + byte[] decodedPassword = Base64 |
| 69 | + .getDecoder() |
| 70 | + .decode(new String(passwordSecretProvider.getSecret( |
| 71 | + toSecretMap(ojdbcConfig.password)))); |
| 72 | + properties.setProperty(CONNECTION_PROPERTY_PASSWORD, |
| 73 | + new String(decodedPassword)); |
| 74 | + } |
| 75 | + |
| 76 | + if (ojdbcConfig.wallet_location != null) { |
| 77 | + // Get wallet from the provider |
| 78 | + String walletProviderType = ojdbcConfig.wallet_location.type; |
| 79 | + OracleConfigurationSecretProvider walletSecretProvider = |
| 80 | + OracleConfigurationSecretProvider.find(walletProviderType); |
| 81 | + |
| 82 | + char[] walletCharArray = |
| 83 | + walletSecretProvider.getSecret( |
| 84 | + toSecretMap(ojdbcConfig.wallet_location)); |
| 85 | + |
| 86 | + // The value returned from the provider is a base64 encoded char array of |
| 87 | + // a wallet in base64 format. So after the fist decoding, the value is |
| 88 | + // still base64 encoded. |
| 89 | + String wallet = new String( |
| 90 | + Base64.getDecoder() |
| 91 | + .decode(new String(walletCharArray))); |
| 92 | + properties.setProperty(CONNECTION_PROPERTY_WALLET_LOCATION, |
| 93 | + "data:;base64," + wallet); |
| 94 | + } |
| 95 | + |
| 96 | + // Retrieve config time to live |
| 97 | + if (ojdbcConfig.config_time_to_live != null) { |
| 98 | + properties.setProperty("config_time_to_live", |
| 99 | + String.valueOf( |
| 100 | + durationToInt(ojdbcConfig.config_time_to_live))); |
| 101 | + } |
| 102 | + |
| 103 | + // Retrieve Jdbc connection properties |
| 104 | + try { |
| 105 | + properties.putAll(toJdbcProperties(ojdbcConfig.jdbc)); |
| 106 | + } catch (IllegalAccessException e) { |
| 107 | + throw new RuntimeException(e); |
| 108 | + } |
| 109 | + |
| 110 | + return properties; |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public String getType() { |
| 115 | + return "pkl"; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * @return value of the element under node, if presents. Otherwise, return null. |
| 120 | + */ |
| 121 | + private Config getOptional(Config node, String s) { |
| 122 | + try { |
| 123 | + Config child = node.get(s); |
| 124 | + if (!(child.getRawValue() instanceof PNull)) // not a leaf node |
| 125 | + return child; |
| 126 | + } catch (NoSuchChildException e) { |
| 127 | + // no-op |
| 128 | + } |
| 129 | + |
| 130 | + return null; |
| 131 | + } |
| 132 | + |
| 133 | + private Map<String, String> toSecretMap(OjdbcConfig.Secret secretConfig) { |
| 134 | + // Convert Pkl config to map |
| 135 | + Map<String, String> options = new HashMap<>(); |
| 136 | + options.put("value", secretConfig.value); |
| 137 | + |
| 138 | + if (secretConfig.authentication != null) { |
| 139 | + // Rename the key from "method" to "authentication" and put into the password options |
| 140 | + options.put("authentication", secretConfig.authentication.remove("method")); |
| 141 | + // Put the rest into authentication options |
| 142 | + options.putAll(secretConfig.authentication); |
| 143 | + } |
| 144 | + |
| 145 | + return options; |
| 146 | + } |
| 147 | + |
| 148 | + private Properties toJdbcProperties(OjdbcConfig.Jdbc ojdbcConfig) |
| 149 | + throws IllegalAccessException { |
| 150 | + |
| 151 | + Properties properties = new Properties(); |
| 152 | + |
| 153 | + Field[] fields = ojdbcConfig.getClass().getDeclaredFields(); |
| 154 | + for (Field field : fields) { |
| 155 | + Object value = field.get(ojdbcConfig); |
| 156 | + if (value == null) continue; |
| 157 | + |
| 158 | + Class<?> type = field.getType(); |
| 159 | + if (type == Duration.class) { |
| 160 | + value = durationToInt((Duration)value); |
| 161 | + } |
| 162 | + |
| 163 | + // Replace $$ with . |
| 164 | + String name = field.getName().replace("$$", "."); |
| 165 | + properties.setProperty(name, value.toString()); |
| 166 | + } |
| 167 | + |
| 168 | + return properties; |
| 169 | + } |
| 170 | + |
| 171 | + private int durationToInt(Duration duration) { |
| 172 | + return Double.valueOf(duration.getValue()).intValue(); |
| 173 | + } |
| 174 | +} |
0 commit comments