| 109 | |
| 110 | |
| 111 | public static void setCredentials(Exchange instance) throws IllegalArgumentException, IllegalAccessException, IOException { |
| 112 | var basePath = FileSystems.getDefault().getPath("").toAbsolutePath().toString(); |
| 113 | var prefix = (basePath.endsWith("cli")) ? "/../../" : "/../"; |
| 114 | var keysJsonPath = basePath + prefix + "keys.json"; |
| 115 | |
| 116 | // System.out.perintln("Looking for keys.json at: " + keysJsonPath); |
| 117 | |
| 118 | Map<String, Object> keysJsonContent = null; |
| 119 | if (FileSystems.getDefault().getPath(keysJsonPath).toFile().exists()) { |
| 120 | System.out.println("Loading credentials from: " + keysJsonPath); |
| 121 | // var content = FileUtils.readFileAsString(keysJsonPath); |
| 122 | String content = new String(Files.readAllBytes(Paths.get(keysJsonPath))); |
| 123 | |
| 124 | try { |
| 125 | ObjectMapper mapper = new ObjectMapper(); |
| 126 | keysJsonContent = mapper.readValue(content, Map.class); |
| 127 | } catch (Exception e) { |
| 128 | System.out.println("Error parsing keys.json: " + e.getMessage()); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | Map<String, Boolean> credentials = (Map<String, Boolean>)instance.requiredCredentials; |
| 133 | if (noKeys || credentials == null) { |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | for (Map.Entry<String, Boolean> entry : credentials.entrySet()) { |
| 138 | String key = entry.getKey(); |
| 139 | Boolean required = entry.getValue(); |
| 140 | |
| 141 | if (!Boolean.TRUE.equals(required)) { |
| 142 | continue; |
| 143 | } |
| 144 | |
| 145 | String instanceIdKey = instance.id; |
| 146 | String credentialValue = null; |
| 147 | |
| 148 | if (keysJsonContent != null && keysJsonContent.containsKey(instanceIdKey)) { |
| 149 | Map<String, Object> instanceCredentials = (Map<String, Object>)keysJsonContent.get(instanceIdKey); |
| 150 | if (instanceCredentials.containsKey(key)) { |
| 151 | credentialValue = instanceCredentials.get(key).toString(); |
| 152 | System.out.println("Setting credential from keys.json: " + instanceIdKey + "." + key); |
| 153 | setProperty(instance, key, credentialValue); |
| 154 | continue; |
| 155 | } |
| 156 | } |
| 157 | String envKey = instanceIdKey.toUpperCase() + "_" + key.toUpperCase(); |
| 158 | credentialValue = System.getenv(envKey); |
| 159 | |
| 160 | if (credentialValue != null && credentialValue.startsWith("-----BEGIN")) { |
| 161 | credentialValue = credentialValue.replace("\\n", "\n"); |
| 162 | } |
| 163 | |
| 164 | if (credentialValue != null) { |
| 165 | System.out.println("Setting credential from ENV: " + envKey); |
| 166 | setProperty(instance, key, credentialValue); |
| 167 | } |
| 168 | } |