|
| 1 | +package io.quarkus.cli.config; |
| 2 | + |
| 3 | +import static io.quarkus.devtools.messagewriter.MessageIcons.SUCCESS_ICON; |
| 4 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 5 | + |
| 6 | +import java.nio.ByteBuffer; |
| 7 | +import java.security.MessageDigest; |
| 8 | +import java.util.Base64; |
| 9 | +import java.util.concurrent.Callable; |
| 10 | + |
| 11 | +import javax.crypto.Cipher; |
| 12 | +import javax.crypto.spec.GCMParameterSpec; |
| 13 | +import javax.crypto.spec.SecretKeySpec; |
| 14 | + |
| 15 | +import io.quarkus.cli.config.Encrypt.KeyFormat; |
| 16 | +import picocli.CommandLine.Command; |
| 17 | +import picocli.CommandLine.Option; |
| 18 | +import picocli.CommandLine.Parameters; |
| 19 | + |
| 20 | +@Command(name = "decrypt", aliases = "dec", header = "Decrypt Secrets", description = "Decrypt a Secret value using the AES/GCM/NoPadding algorithm as a default.") |
| 21 | +public class Decrypt extends BaseConfigCommand implements Callable<Integer> { |
| 22 | + @Parameters(index = "0", paramLabel = "SECRET", description = "The secret value to decrypt") |
| 23 | + String secret; |
| 24 | + |
| 25 | + @Parameters(index = "1", paramLabel = "DECRYPTION KEY", description = "The decryption key") |
| 26 | + String decryptionKey; |
| 27 | + |
| 28 | + @Option(names = { "-f", "--format" }, description = "The decryption key format (base64 / plain)", defaultValue = "base64") |
| 29 | + KeyFormat decryptionKeyFormat; |
| 30 | + |
| 31 | + @Option(hidden = true, names = { "-a", "--algorithm" }, description = "Algorithm", defaultValue = "AES") |
| 32 | + String algorithm; |
| 33 | + |
| 34 | + @Option(hidden = true, names = { "-m", "--mode" }, description = "Mode", defaultValue = "GCM") |
| 35 | + String mode; |
| 36 | + |
| 37 | + @Option(hidden = true, names = { "-p", "--padding" }, description = "Padding", defaultValue = "NoPadding") |
| 38 | + String padding; |
| 39 | + |
| 40 | + @Option(hidden = true, names = { "-q", "--quiet" }, defaultValue = "false") |
| 41 | + boolean quiet; |
| 42 | + |
| 43 | + @Override |
| 44 | + public Integer call() throws Exception { |
| 45 | + if (decryptionKey.startsWith("\\\"") && decryptionKey.endsWith("\"\\")) { |
| 46 | + decryptionKey = decryptionKey.substring(2, decryptionKey.length() - 2); |
| 47 | + } |
| 48 | + |
| 49 | + byte[] decryptionKeyBytes; |
| 50 | + if (decryptionKeyFormat.equals(KeyFormat.base64)) { |
| 51 | + decryptionKeyBytes = Base64.getUrlDecoder().decode(decryptionKey); |
| 52 | + } else { |
| 53 | + decryptionKeyBytes = decryptionKey.getBytes(UTF_8); |
| 54 | + } |
| 55 | + |
| 56 | + Cipher cipher = Cipher.getInstance(algorithm + "/" + mode + "/" + padding); |
| 57 | + ByteBuffer byteBuffer = ByteBuffer.wrap(Base64.getUrlDecoder().decode(secret.getBytes(UTF_8))); |
| 58 | + int ivLength = byteBuffer.get(); |
| 59 | + byte[] iv = new byte[ivLength]; |
| 60 | + byteBuffer.get(iv); |
| 61 | + byte[] encrypted = new byte[byteBuffer.remaining()]; |
| 62 | + byteBuffer.get(encrypted); |
| 63 | + |
| 64 | + MessageDigest sha256 = MessageDigest.getInstance("SHA-256"); |
| 65 | + sha256.update(decryptionKeyBytes); |
| 66 | + cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(sha256.digest(), "AES"), new GCMParameterSpec(128, iv)); |
| 67 | + String decrypted = new String(cipher.doFinal(encrypted), UTF_8); |
| 68 | + |
| 69 | + if (!quiet) { |
| 70 | + String success = SUCCESS_ICON + " The secret @|bold " + secret + "|@ was decrypted to @|bold " + decrypted + "|@"; |
| 71 | + output.info(success); |
| 72 | + } |
| 73 | + |
| 74 | + return 0; |
| 75 | + } |
| 76 | +} |
0 commit comments