|
| 1 | +/* |
| 2 | + * Copyright 2023-2023 the original author or authors. |
| 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 | + * https://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 | +package org.springframework.vault.core; |
| 17 | + |
| 18 | +import org.springframework.vault.support.Ciphertext; |
| 19 | +import org.springframework.vault.support.Hmac; |
| 20 | +import org.springframework.vault.support.Plaintext; |
| 21 | +import org.springframework.vault.support.RawTransitKey; |
| 22 | +import org.springframework.vault.support.Signature; |
| 23 | +import org.springframework.vault.support.SignatureValidation; |
| 24 | +import org.springframework.vault.support.TransitKeyType; |
| 25 | +import org.springframework.vault.support.VaultDecryptionResult; |
| 26 | +import org.springframework.vault.support.VaultEncryptionResult; |
| 27 | +import org.springframework.vault.support.VaultHmacRequest; |
| 28 | +import org.springframework.vault.support.VaultSignRequest; |
| 29 | +import org.springframework.vault.support.VaultSignatureVerificationRequest; |
| 30 | +import org.springframework.vault.support.VaultTransitContext; |
| 31 | +import org.springframework.vault.support.VaultTransitKey; |
| 32 | +import org.springframework.vault.support.VaultTransitKeyConfiguration; |
| 33 | +import org.springframework.vault.support.VaultTransitKeyCreationRequest; |
| 34 | +import reactor.core.publisher.Flux; |
| 35 | +import reactor.core.publisher.Mono; |
| 36 | + |
| 37 | +import java.util.List; |
| 38 | + |
| 39 | +/** |
| 40 | + * Interface that specifies a set of {@code transit} operations executed on a reactive |
| 41 | + * infrastructure, implemented by |
| 42 | + * {@link org.springframework.vault.core.ReactiveVaultTransitTemplate}. |
| 43 | + * |
| 44 | + * @author James Luke |
| 45 | + */ |
| 46 | +public interface ReactiveVaultTransitOperations { |
| 47 | + |
| 48 | + /** |
| 49 | + * Create a new named encryption key given a {@code name} |
| 50 | + * @param keyName must not be empty or {@literal null} |
| 51 | + */ |
| 52 | + Mono<Void> createKey(String keyName); |
| 53 | + |
| 54 | + /** |
| 55 | + * Create a new named encryption key given a {@code name} and |
| 56 | + * {@link VaultTransitKeyCreationRequest}. The key options set here cannot be changed |
| 57 | + * after key creation. |
| 58 | + * @param keyName must not be empty or {@literal null}. |
| 59 | + * @param createKeyRequest must not be {@literal null}. |
| 60 | + */ |
| 61 | + Mono<Void> createKey(String keyName, VaultTransitKeyCreationRequest createKeyRequest); |
| 62 | + |
| 63 | + /** |
| 64 | + * @return stream of transit key names. |
| 65 | + */ |
| 66 | + Flux<String> getKeys(); |
| 67 | + |
| 68 | + /** |
| 69 | + * Create a new named encryption key given a {@code name}. |
| 70 | + * @param keyName must not be empty or {@literal null}. |
| 71 | + * @param keyConfiguration must not be {@literal null}. |
| 72 | + */ |
| 73 | + Mono<Void> configureKey(String keyName, VaultTransitKeyConfiguration keyConfiguration); |
| 74 | + |
| 75 | + /** |
| 76 | + * Returns the value of the named encryption key. Depending on the type of key, |
| 77 | + * different information may be returned. The key must be exportable to support this |
| 78 | + * operation. |
| 79 | + * @param keyName must not be empty or {@literal null}. |
| 80 | + * @param type must not be {@literal null}. |
| 81 | + * @return the {@link RawTransitKey}. May be empty if key does not exist |
| 82 | + */ |
| 83 | + Mono<RawTransitKey> exportKey(String keyName, TransitKeyType type); |
| 84 | + |
| 85 | + /** |
| 86 | + * Return information about a named encryption key. |
| 87 | + * @param keyName must not be empty or {@literal null}. |
| 88 | + * @return the {@link VaultTransitKey}. May be empty if key does not exist |
| 89 | + */ |
| 90 | + Mono<VaultTransitKey> getKey(String keyName); |
| 91 | + |
| 92 | + /** |
| 93 | + * Deletes a named encryption key. It will no longer be possible to decrypt any data |
| 94 | + * encrypted with the named key. |
| 95 | + * @param keyName must not be empty or {@literal null}. |
| 96 | + */ |
| 97 | + Mono<Void> deleteKey(String keyName); |
| 98 | + |
| 99 | + /** |
| 100 | + * Rotates the version of the named key. After rotation, new plain text requests will |
| 101 | + * be encrypted with the new version of the key. To upgrade ciphertext to be encrypted |
| 102 | + * with the latest version of the key, use {@link #rewrap(String, String)}. |
| 103 | + * @param keyName must not be empty or {@literal null}. |
| 104 | + * @see #rewrap(String, String) |
| 105 | + */ |
| 106 | + Mono<Void> rotate(String keyName); |
| 107 | + |
| 108 | + /** |
| 109 | + * Encrypts the provided plain text using the named key. The given {@code plaintext} |
| 110 | + * is encoded into bytes using the {@link java.nio.charset.Charset#defaultCharset() |
| 111 | + * default charset}. Use |
| 112 | + * {@link #encrypt(String, org.springframework.vault.support.Plaintext)} to construct |
| 113 | + * a {@link org.springframework.vault.support.Plaintext#of(byte[]) Plaintext} object |
| 114 | + * from bytes to avoid {@link java.nio.charset.Charset} mismatches. |
| 115 | + * @param keyName must not be empty or {@literal null}. |
| 116 | + * @param plaintext must not be empty or {@literal null}. |
| 117 | + * @return cipher text. |
| 118 | + */ |
| 119 | + Mono<String> encrypt(String keyName, String plaintext); |
| 120 | + |
| 121 | + /** |
| 122 | + * Encrypts the provided {@code plaintext} using the named key. |
| 123 | + * @param keyName must not be empty or {@literal null}. |
| 124 | + * @param plaintext must not be {@literal null}. |
| 125 | + * @return cipher text. |
| 126 | + */ |
| 127 | + Mono<Ciphertext> encrypt(String keyName, Plaintext plaintext); |
| 128 | + |
| 129 | + /** |
| 130 | + * Encrypts the provided {@code plaintext} using the named key. |
| 131 | + * @param keyName must not be empty or {@literal null}. |
| 132 | + * @param plaintext must not be empty or {@literal null}. |
| 133 | + * @param transitRequest must not be {@literal null}. Use |
| 134 | + * {@link VaultTransitContext#empty()} if no request options provided. |
| 135 | + * @return cipher text. |
| 136 | + */ |
| 137 | + Mono<String> encrypt(String keyName, byte[] plaintext, VaultTransitContext transitRequest); |
| 138 | + |
| 139 | + /** |
| 140 | + * Encrypts the provided batch of {@code plaintext} using the named key and context. |
| 141 | + * The encryption is done using transit backend's batch operation. |
| 142 | + * @param keyName must not be empty or {@literal null}. |
| 143 | + * @param batchRequest a list of {@link Plaintext} which includes plain text and an |
| 144 | + * optional context. |
| 145 | + * @return the encrypted result in the order of {@code batchRequest} plaintexts. |
| 146 | + */ |
| 147 | + Flux<VaultEncryptionResult> encrypt(String keyName, List<Plaintext> batchRequest); |
| 148 | + |
| 149 | + /** |
| 150 | + * Decrypts the provided plain text using the named key. The decoded {@code plaintext} |
| 151 | + * is decoded into {@link String} the {@link java.nio.charset.Charset#defaultCharset() |
| 152 | + * default charset}. Use |
| 153 | + * {@link #decrypt(String, org.springframework.vault.support.Ciphertext)} to obtain a |
| 154 | + * {@link org.springframework.vault.support.Ciphertext} object that allows to control |
| 155 | + * the {@link java.nio.charset.Charset} for later consumption. |
| 156 | + * @param keyName must not be empty or {@literal null}. |
| 157 | + * @param ciphertext must not be empty or {@literal null}. |
| 158 | + * @return plain text. |
| 159 | + */ |
| 160 | + Mono<String> decrypt(String keyName, String ciphertext); |
| 161 | + |
| 162 | + /** |
| 163 | + * Decrypts the provided cipher text using the named key. |
| 164 | + * @param keyName must not be empty or {@literal null}. |
| 165 | + * @param ciphertext must not be {@literal null}. |
| 166 | + * @return plain text. |
| 167 | + */ |
| 168 | + Mono<Plaintext> decrypt(String keyName, Ciphertext ciphertext); |
| 169 | + |
| 170 | + /** |
| 171 | + * Decrypts the provided {@code ciphertext} using the named key. |
| 172 | + * @param keyName must not be empty or {@literal null}. |
| 173 | + * @param ciphertext must not be empty or {@literal null}. |
| 174 | + * @param transitContext must not be {@literal null}. Use |
| 175 | + * {@link VaultTransitContext#empty()} if no request options provided. |
| 176 | + * @return cipher text. |
| 177 | + * @return plain text. |
| 178 | + */ |
| 179 | + Mono<byte[]> decrypt(String keyName, String ciphertext, VaultTransitContext transitContext); |
| 180 | + |
| 181 | + /** |
| 182 | + * Decrypts the provided batch of cipher text using the named key and context. The* |
| 183 | + * decryption is done using transit backend's batch operation. |
| 184 | + * @param keyName must not be empty or {@literal null}. |
| 185 | + * @param batchRequest a list of {@link Ciphertext} which includes plain text and an |
| 186 | + * optional context. |
| 187 | + * @return the decrypted result in the order of {@code batchRequest} ciphertexts. |
| 188 | + */ |
| 189 | + Flux<VaultDecryptionResult> decrypt(String keyName, List<Ciphertext> batchRequest); |
| 190 | + |
| 191 | + /** |
| 192 | + * Rewrap the provided cipher text using the latest version of the named key. Because |
| 193 | + * this never returns plain text, it is possible to delegate this functionality to |
| 194 | + * untrusted users or scripts. |
| 195 | + * @param keyName must not be empty or {@literal null}. |
| 196 | + * @param ciphertext must not be empty or {@literal null}. |
| 197 | + * @return cipher text. |
| 198 | + * @see #rotate(String) |
| 199 | + */ |
| 200 | + Mono<String> rewrap(String keyName, String ciphertext); |
| 201 | + |
| 202 | + /** |
| 203 | + * Rewrap the provided cipher text using the latest version of the named key. Because |
| 204 | + * this never returns plain text, it is possible to delegate this functionality to |
| 205 | + * untrusted users or scripts. |
| 206 | + * @param keyName must not be empty or {@literal null}. |
| 207 | + * @param ciphertext must not be empty or {@literal null}. |
| 208 | + * @param transitContext must not be {@literal null}. Use |
| 209 | + * {@link VaultTransitContext#empty()} if no request options provided. |
| 210 | + * @return cipher text. |
| 211 | + * @see #rotate(String) |
| 212 | + */ |
| 213 | + Mono<String> rewrap(String keyName, String ciphertext, VaultTransitContext transitContext); |
| 214 | + |
| 215 | + /** |
| 216 | + * Create a HMAC using {@code keyName} of given {@link Plaintext} using the default |
| 217 | + * hash algorithm. The key can be of any type supported by transit; the raw key will |
| 218 | + * be marshaled into bytes to be used for the HMAC function. If the key is of a type |
| 219 | + * that supports rotation, the latest (current) version will be used. |
| 220 | + * @param keyName must not be empty or {@literal null}. |
| 221 | + * @param plaintext must not be {@literal null}. |
| 222 | + * @return the digest of given data the default hash algorithm and the named key. |
| 223 | + */ |
| 224 | + Mono<Hmac> getHmac(String keyName, Plaintext plaintext); |
| 225 | + |
| 226 | + /** |
| 227 | + * Create a HMAC using {@code keyName} of given {@link VaultHmacRequest} using the |
| 228 | + * default hash algorithm. The key can be of any type supported by transit; the raw |
| 229 | + * key will be marshaled into bytes to be used for the HMAC function. If the key is of |
| 230 | + * a type that supports rotation, configured {@link VaultHmacRequest#getKeyVersion()} |
| 231 | + * will be used. |
| 232 | + * @param keyName must not be empty or {@literal null}. |
| 233 | + * @param hmacRequest the {@link VaultHmacRequest}, must not be {@literal null}. |
| 234 | + * @return the digest of given data the default hash algorithm and the named key. |
| 235 | + */ |
| 236 | + Mono<Hmac> getHmac(String keyName, VaultHmacRequest hmacRequest); |
| 237 | + |
| 238 | + /** |
| 239 | + * Create a cryptographic signature using {@code keyName} of the given |
| 240 | + * {@link Plaintext} and the default hash algorithm. The key must be of a type that |
| 241 | + * supports signing. |
| 242 | + * @param keyName must not be empty or {@literal null}. |
| 243 | + * @param plaintext must not be empty or {@literal null}. |
| 244 | + * @return Signature for {@link Plaintext}. |
| 245 | + */ |
| 246 | + Mono<Signature> sign(String keyName, Plaintext plaintext); |
| 247 | + |
| 248 | + /** |
| 249 | + * Create a cryptographic signature using {@code keyName} of the given |
| 250 | + * {@link VaultSignRequest} and the specified hash algorithm. The key must be of a |
| 251 | + * type that supports signing. |
| 252 | + * @param keyName must not be empty or {@literal null}. |
| 253 | + * @param signRequest {@link VaultSignRequest} must not be empty or {@literal null}. |
| 254 | + * @return Signature for {@link VaultSignRequest}. |
| 255 | + */ |
| 256 | + Mono<Signature> sign(String keyName, VaultSignRequest signRequest); |
| 257 | + |
| 258 | + /** |
| 259 | + * Verify the cryptographic signature using {@code keyName} of the given |
| 260 | + * {@link Plaintext} and {@link Signature}. |
| 261 | + * @param keyName must not be empty or {@literal null}. |
| 262 | + * @param plaintext must not be {@literal null}. |
| 263 | + * @param signature Signature to be verified, must not be {@literal null}. |
| 264 | + * @return {@literal true} if the signature is valid, {@literal false} otherwise. |
| 265 | + */ |
| 266 | + Mono<Boolean> verify(String keyName, Plaintext plaintext, Signature signature); |
| 267 | + |
| 268 | + /** |
| 269 | + * Verify the cryptographic signature using {@code keyName} of the given |
| 270 | + * {@link VaultSignRequest}. |
| 271 | + * @param keyName must not be empty or {@literal null}. |
| 272 | + * @param verificationRequest {@link VaultSignatureVerificationRequest} must not be |
| 273 | + * {@literal null}. |
| 274 | + * @return the resulting {@link SignatureValidation}. |
| 275 | + */ |
| 276 | + Mono<SignatureValidation> verify(String keyName, VaultSignatureVerificationRequest verificationRequest); |
| 277 | + |
| 278 | +} |
0 commit comments