You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2022/06/09 09:56:48 UTC

[camel] 03/04: CAMEL-17689 - Create a Camel Hashicorp Vault Component - Read secret operation

This is an automated email from the ASF dual-hosted git repository.

acosentino pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 432f3b8c1b7082dea64a60fbf59365100d032ccc
Author: Andrea Cosentino <an...@gmail.com>
AuthorDate: Thu Jun 9 10:50:54 2022 +0200

    CAMEL-17689 - Create a Camel Hashicorp Vault Component - Read secret operation
---
 .../component/hashicorp/vault/hashicorp-vault.json      |  5 +++--
 .../hashicorp/vault/HashicorpVaultConstants.java        |  6 +++++-
 .../hashicorp/vault/HashicorpVaultOperation.java        |  3 ++-
 .../hashicorp/vault/HashicorpVaultProducer.java         | 17 +++++++++++++++++
 4 files changed, 27 insertions(+), 4 deletions(-)

diff --git a/components/camel-hashicorp-vault/src/generated/resources/org/apache/camel/component/hashicorp/vault/hashicorp-vault.json b/components/camel-hashicorp-vault/src/generated/resources/org/apache/camel/component/hashicorp/vault/hashicorp-vault.json
index 30add6e5bd2..c622121c0ec 100644
--- a/components/camel-hashicorp-vault/src/generated/resources/org/apache/camel/component/hashicorp/vault/hashicorp-vault.json
+++ b/components/camel-hashicorp-vault/src/generated/resources/org/apache/camel/component/hashicorp/vault/hashicorp-vault.json
@@ -26,13 +26,14 @@
     "autowiredEnabled": { "kind": "property", "displayName": "Autowired Enabled", "group": "advanced", "label": "advanced", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "description": "Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which t [...]
   },
   "headers": {
-    "CamelHashicorpVaultProducerOperation": { "kind": "header", "displayName": "", "group": "producer", "label": "producer", "required": false, "javaType": "org.apache.camel.component.azure.key.vault.KeyVaultOperationDefinition", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "description": "Overrides the desired operation to be used in the producer.", "constantName": "org.apache.camel.component.hashicorp.vault.HashicorpVaultConstants#OPERATION" }
+    "CamelHashicorpVaultProducerOperation": { "kind": "header", "displayName": "", "group": "producer", "label": "producer", "required": false, "javaType": "String", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "description": "Overrides the desired operation to be used in the producer.", "constantName": "org.apache.camel.component.hashicorp.vault.HashicorpVaultConstants#OPERATION" },
+    "CamelHashicorpVaultSecretPath": { "kind": "header", "displayName": "", "group": "producer", "label": "producer", "required": false, "javaType": "String", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "description": "Set the desired secret path as header.", "constantName": "org.apache.camel.component.hashicorp.vault.HashicorpVaultConstants#SECRET_PATH" }
   },
   "properties": {
     "secretsEngine": { "kind": "path", "displayName": "Secrets Engine", "group": "producer", "label": "", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.hashicorp.vault.HashicorpVaultConfiguration", "configurationField": "configuration", "description": "Vault Name to be used" },
     "host": { "kind": "parameter", "displayName": "Host", "group": "producer", "label": "", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.hashicorp.vault.HashicorpVaultConfiguration", "configurationField": "configuration", "description": "Hashicorp Vault instance host to be used" },
     "lazyStartProducer": { "kind": "parameter", "displayName": "Lazy Start Producer", "group": "producer", "label": "producer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during sta [...]
-    "operation": { "kind": "parameter", "displayName": "Operation", "group": "producer", "label": "producer", "required": false, "type": "object", "javaType": "org.apache.camel.component.hashicorp.vault.HashicorpVaultOperation", "enum": [ "createSecret" ], "deprecated": false, "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.hashicorp.vault.HashicorpVaultConfiguration", "configurationField": "configuration", "description": "Operation to be performed" },
+    "operation": { "kind": "parameter", "displayName": "Operation", "group": "producer", "label": "producer", "required": false, "type": "object", "javaType": "org.apache.camel.component.hashicorp.vault.HashicorpVaultOperation", "enum": [ "createSecret", "getSecret" ], "deprecated": false, "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.hashicorp.vault.HashicorpVaultConfiguration", "configurationField": "configuration", "description": "Operation to  [...]
     "port": { "kind": "parameter", "displayName": "Port", "group": "producer", "label": "", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "defaultValue": "8200", "configurationClass": "org.apache.camel.component.hashicorp.vault.HashicorpVaultConfiguration", "configurationField": "configuration", "description": "Hashicorp Vault instance port to be used" },
     "scheme": { "kind": "parameter", "displayName": "Scheme", "group": "producer", "label": "", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "defaultValue": "https", "configurationClass": "org.apache.camel.component.hashicorp.vault.HashicorpVaultConfiguration", "configurationField": "configuration", "description": "Hashicorp Vault instance scheme to be used" },
     "secretPath": { "kind": "parameter", "displayName": "Secret Path", "group": "producer", "label": "", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.hashicorp.vault.HashicorpVaultConfiguration", "configurationField": "configuration", "description": "Hashicorp Vault instance secret Path to be used" },
diff --git a/components/camel-hashicorp-vault/src/main/java/org/apache/camel/component/hashicorp/vault/HashicorpVaultConstants.java b/components/camel-hashicorp-vault/src/main/java/org/apache/camel/component/hashicorp/vault/HashicorpVaultConstants.java
index a6c7afc2e6b..3f3cb0d6e6e 100644
--- a/components/camel-hashicorp-vault/src/main/java/org/apache/camel/component/hashicorp/vault/HashicorpVaultConstants.java
+++ b/components/camel-hashicorp-vault/src/main/java/org/apache/camel/component/hashicorp/vault/HashicorpVaultConstants.java
@@ -23,9 +23,13 @@ public final class HashicorpVaultConstants {
 
     // headers set by the producer only
     @Metadata(label = "producer", description = "Overrides the desired operation to be used in the producer.",
-              javaType = "org.apache.camel.component.azure.key.vault.KeyVaultOperationDefinition")
+              javaType = "String")
     public static final String OPERATION = HEADER_PREFIX + "ProducerOperation";
 
+    // headers set by the producer only
+    @Metadata(label = "producer", description = "Set the desired secret path as header.",
+            javaType = "String")
+    public static final String SECRET_PATH = HEADER_PREFIX + "SecretPath";
     private HashicorpVaultConstants() {
     }
 }
diff --git a/components/camel-hashicorp-vault/src/main/java/org/apache/camel/component/hashicorp/vault/HashicorpVaultOperation.java b/components/camel-hashicorp-vault/src/main/java/org/apache/camel/component/hashicorp/vault/HashicorpVaultOperation.java
index 70d314382e5..2023dd8ed52 100644
--- a/components/camel-hashicorp-vault/src/main/java/org/apache/camel/component/hashicorp/vault/HashicorpVaultOperation.java
+++ b/components/camel-hashicorp-vault/src/main/java/org/apache/camel/component/hashicorp/vault/HashicorpVaultOperation.java
@@ -17,5 +17,6 @@
 package org.apache.camel.component.hashicorp.vault;
 
 public enum HashicorpVaultOperation {
-    createSecret
+    createSecret,
+    getSecret
 }
diff --git a/components/camel-hashicorp-vault/src/main/java/org/apache/camel/component/hashicorp/vault/HashicorpVaultProducer.java b/components/camel-hashicorp-vault/src/main/java/org/apache/camel/component/hashicorp/vault/HashicorpVaultProducer.java
index e6e5582840b..7855be2bce1 100644
--- a/components/camel-hashicorp-vault/src/main/java/org/apache/camel/component/hashicorp/vault/HashicorpVaultProducer.java
+++ b/components/camel-hashicorp-vault/src/main/java/org/apache/camel/component/hashicorp/vault/HashicorpVaultProducer.java
@@ -21,10 +21,12 @@ import org.apache.camel.Exchange;
 import org.apache.camel.InvalidPayloadException;
 import org.apache.camel.Message;
 import org.apache.camel.support.DefaultProducer;
+import org.apache.camel.util.ObjectHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.vault.core.VaultKeyValueOperations;
 import org.springframework.vault.core.VaultKeyValueOperationsSupport;
+import org.springframework.vault.support.VaultResponse;
 
 public class HashicorpVaultProducer extends DefaultProducer {
 
@@ -51,6 +53,9 @@ public class HashicorpVaultProducer extends DefaultProducer {
             case createSecret:
                 createSecret(exchange);
                 break;
+            case getSecret:
+                getSecret(exchange);
+                break;
             default:
                 throw new IllegalArgumentException("Unsupported operation");
         }
@@ -63,6 +68,18 @@ public class HashicorpVaultProducer extends DefaultProducer {
         keyValue.put(getEndpoint().getConfiguration().getSecretPath(), exchange.getMessage().getBody());
     }
 
+    private void getSecret(Exchange exchange) throws InvalidPayloadException {
+        String secretPath;
+        if (ObjectHelper.isNotEmpty(exchange.getMessage().getHeader(HashicorpVaultConstants.SECRET_PATH))) {
+            secretPath = exchange.getMessage().getHeader(HashicorpVaultConstants.SECRET_PATH, String.class);
+        } else {
+        throw new IllegalArgumentException("Secret Path must be specified");
+        }
+        String completePath = getEndpoint().getConfiguration().getSecretsEngine() + "/" + "data" + "/" + secretPath;
+        VaultResponse rawSecret = getEndpoint().getVaultTemplate().read(completePath);
+        exchange.getMessage().setBody(rawSecret.getData());
+    }
+
     @Override
     public HashicorpVaultEndpoint getEndpoint() {
         return (HashicorpVaultEndpoint) super.getEndpoint();