|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.iceberg.hashicorp; |
| 20 | + |
| 21 | +import com.fasterxml.jackson.databind.JsonNode; |
| 22 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 23 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 24 | +import java.io.Closeable; |
| 25 | +import java.io.IOException; |
| 26 | +import java.io.UncheckedIOException; |
| 27 | +import java.util.OptionalLong; |
| 28 | +import org.apache.hc.client5.http.classic.methods.HttpPost; |
| 29 | +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; |
| 30 | +import org.apache.hc.client5.http.impl.classic.HttpClients; |
| 31 | +import org.apache.hc.client5.http.protocol.HttpClientContext; |
| 32 | +import org.apache.hc.core5.http.ClassicHttpResponse; |
| 33 | +import org.apache.hc.core5.http.ContentType; |
| 34 | +import org.apache.hc.core5.http.ParseException; |
| 35 | +import org.apache.hc.core5.http.io.HttpClientResponseHandler; |
| 36 | +import org.apache.hc.core5.http.io.entity.EntityUtils; |
| 37 | +import org.apache.hc.core5.http.io.entity.StringEntity; |
| 38 | +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; |
| 39 | + |
| 40 | +/** |
| 41 | + * HTTP client for interacting with HashiCorp Vault REST API. |
| 42 | + * |
| 43 | + * @see <a href=https://developer.hashicorp.com/vault/api-docs>HashiCorp Vault HTTP API</a> |
| 44 | + */ |
| 45 | +class VaultClient implements Closeable { |
| 46 | + private static final ObjectMapper MAPPER = new ObjectMapper(); |
| 47 | + private static final String VAULT_TOKEN_HEADER = "X-Vault-Token"; |
| 48 | + |
| 49 | + private final String address; |
| 50 | + private final String transitMount; |
| 51 | + private final String appRolePath; |
| 52 | + |
| 53 | + private transient volatile CloseableHttpClient httpClient; |
| 54 | + |
| 55 | + VaultClient(String address, String transitMount, String appRolePath) { |
| 56 | + this.address = address; |
| 57 | + this.transitMount = transitMount; |
| 58 | + this.appRolePath = appRolePath; |
| 59 | + } |
| 60 | + |
| 61 | + AuthResult authenticate(String roleId, String secretId) { |
| 62 | + ObjectNode requestBody = MAPPER.createObjectNode(); |
| 63 | + requestBody.put("role_id", roleId); |
| 64 | + requestBody.put("secret_id", secretId); |
| 65 | + |
| 66 | + JsonNode response = post("/v1/auth/" + appRolePath + "/login", null, requestBody); |
| 67 | + JsonNode authNode = response.get("auth"); |
| 68 | + if (authNode == null) { |
| 69 | + throw new RuntimeException("Failed to authenticate: no auth section in response"); |
| 70 | + } |
| 71 | + |
| 72 | + String clientToken = authNode.get("client_token").asText(); |
| 73 | + OptionalLong leaseDuration = |
| 74 | + authNode.has("lease_duration") |
| 75 | + ? OptionalLong.of(authNode.get("lease_duration").asLong()) |
| 76 | + : OptionalLong.empty(); |
| 77 | + return new AuthResult(clientToken, leaseDuration); |
| 78 | + } |
| 79 | + |
| 80 | + String encrypt(String vaultToken, String wrappingKeyId, String plaintext) { |
| 81 | + ObjectNode requestBody = MAPPER.createObjectNode(); |
| 82 | + requestBody.put("plaintext", plaintext); |
| 83 | + |
| 84 | + JsonNode response = |
| 85 | + post("/v1/" + transitMount + "/encrypt/" + wrappingKeyId, vaultToken, requestBody); |
| 86 | + |
| 87 | + JsonNode dataNode = response.get("data"); |
| 88 | + if (dataNode == null || !dataNode.has("ciphertext")) { |
| 89 | + throw new RuntimeException("Failed to wrap key: no ciphertext returned"); |
| 90 | + } |
| 91 | + |
| 92 | + return dataNode.get("ciphertext").asText(); |
| 93 | + } |
| 94 | + |
| 95 | + String decrypt(String vaultToken, String wrappingKeyId, String ciphertext) { |
| 96 | + ObjectNode requestBody = MAPPER.createObjectNode(); |
| 97 | + requestBody.put("ciphertext", ciphertext); |
| 98 | + |
| 99 | + JsonNode response = |
| 100 | + post("/v1/" + transitMount + "/decrypt/" + wrappingKeyId, vaultToken, requestBody); |
| 101 | + |
| 102 | + JsonNode dataNode = response.get("data"); |
| 103 | + if (dataNode == null || !dataNode.has("plaintext")) { |
| 104 | + throw new RuntimeException("Failed to unwrap key: no plaintext returned"); |
| 105 | + } |
| 106 | + |
| 107 | + return dataNode.get("plaintext").asText(); |
| 108 | + } |
| 109 | + |
| 110 | + DataKey generateKey(String vaultToken, String wrappingKeyId) { |
| 111 | + ObjectNode requestBody = MAPPER.createObjectNode(); |
| 112 | + |
| 113 | + JsonNode response = |
| 114 | + post( |
| 115 | + "/v1/" + transitMount + "/datakey/plaintext/" + wrappingKeyId, vaultToken, requestBody); |
| 116 | + |
| 117 | + JsonNode dataNode = response.get("data"); |
| 118 | + if (dataNode == null || !dataNode.has("plaintext") || !dataNode.has("ciphertext")) { |
| 119 | + throw new RuntimeException("Failed to generate key: missing plaintext or ciphertext"); |
| 120 | + } |
| 121 | + |
| 122 | + String plaintext = dataNode.get("plaintext").asText(); |
| 123 | + String ciphertext = dataNode.get("ciphertext").asText(); |
| 124 | + return new DataKey(plaintext, ciphertext); |
| 125 | + } |
| 126 | + |
| 127 | + private JsonNode post(String path, String token, ObjectNode requestBody) { |
| 128 | + HttpPost request = new HttpPost(address + path); |
| 129 | + if (token != null) { |
| 130 | + request.setHeader(VAULT_TOKEN_HEADER, token); |
| 131 | + } |
| 132 | + |
| 133 | + try { |
| 134 | + request.setEntity( |
| 135 | + new StringEntity(MAPPER.writeValueAsString(requestBody), ContentType.APPLICATION_JSON)); |
| 136 | + } catch (IOException e) { |
| 137 | + throw new UncheckedIOException("Failed to serialize request body", e); |
| 138 | + } |
| 139 | + |
| 140 | + try { |
| 141 | + return httpClient().execute(request, HttpClientContext.create(), new VaultResponseHandler()); |
| 142 | + } catch (IOException e) { |
| 143 | + throw new UncheckedIOException("Failed to execute Vault request to " + path, e); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + private static class VaultResponseHandler implements HttpClientResponseHandler<JsonNode> { |
| 148 | + @Override |
| 149 | + public JsonNode handleResponse(ClassicHttpResponse response) { |
| 150 | + int statusCode = response.getCode(); |
| 151 | + Preconditions.checkState(statusCode == 200, "Status must be 200: %d", statusCode); |
| 152 | + |
| 153 | + try { |
| 154 | + String responseBody = EntityUtils.toString(response.getEntity()); |
| 155 | + return MAPPER.readTree(responseBody); |
| 156 | + } catch (ParseException e) { |
| 157 | + throw new RuntimeException("Failed to parse Vault error response", e); |
| 158 | + } catch (IOException e) { |
| 159 | + throw new UncheckedIOException("Failed to read response", e); |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + private CloseableHttpClient httpClient() { |
| 165 | + if (httpClient == null) { |
| 166 | + synchronized (this) { |
| 167 | + if (httpClient == null) { |
| 168 | + httpClient = HttpClients.createDefault(); |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + return httpClient; |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public void close() { |
| 177 | + if (httpClient != null) { |
| 178 | + try { |
| 179 | + httpClient.close(); |
| 180 | + } catch (IOException e) { |
| 181 | + throw new UncheckedIOException("Failed to close HTTP client", e); |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + static class AuthResult { |
| 187 | + private final String clientToken; |
| 188 | + private final OptionalLong leaseDuration; |
| 189 | + |
| 190 | + AuthResult(String clientToken, OptionalLong leaseDuration) { |
| 191 | + this.clientToken = clientToken; |
| 192 | + this.leaseDuration = leaseDuration; |
| 193 | + } |
| 194 | + |
| 195 | + public String clientToken() { |
| 196 | + return clientToken; |
| 197 | + } |
| 198 | + |
| 199 | + public OptionalLong leaseDuration() { |
| 200 | + return leaseDuration; |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + static class DataKey { |
| 205 | + private final String plaintext; |
| 206 | + private final String ciphertext; |
| 207 | + |
| 208 | + DataKey(String plaintext, String ciphertext) { |
| 209 | + this.plaintext = plaintext; |
| 210 | + this.ciphertext = ciphertext; |
| 211 | + } |
| 212 | + |
| 213 | + public String plaintext() { |
| 214 | + return plaintext; |
| 215 | + } |
| 216 | + |
| 217 | + public String ciphertext() { |
| 218 | + return ciphertext; |
| 219 | + } |
| 220 | + } |
| 221 | +} |
0 commit comments