Kubernetes Secrets
Since Camel 2.17
Only producer is supported
The Kubernetes Secrets component is one of Kubernetes Components which provides a producer to execute Kubernetes Secrets operations.
Configuring Options
Camel components are configured on two separate levels:
-
component level
-
endpoint level
Configuring Component Options
At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.
For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.
Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.
You can configure components using:
-
the Component DSL.
-
in a configuration file (
application.properties,*.yamlfiles, etc). -
directly in the Java code.
Configuring Endpoint Options
You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.
Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.
A good practice when configuring options is to use Property Placeholders.
Property placeholders provide a few benefits:
-
They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.
-
They allow externalizing the configuration from the code.
-
They help the code to become more flexible and reusable.
The following two sections list all the options, firstly for the component followed by the endpoint.
Component Options
The Kubernetes Secrets component supports the following options which are listed below.
| Name | Description | Default | Type |
|---|---|---|---|
Autowired An existing Kubernetes client for the endpoints of this component that do not set their own kubernetesClient option. The endpoints then do not use the options that configure the client (masterUrl, credentials, certificates, trustCerts, apiVersion, connectionTimeout). | KubernetesClient | ||
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 starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
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 then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. | true | boolean |
Endpoint Options
The Kubernetes Secrets endpoint is configured using URI syntax:
kubernetes-secrets:masterUrl
With the following path and query parameters:
Path Parameters
| Name | Description | Default | Type |
|---|---|---|---|
Required The URL of the Kubernetes API server, such as https://kubernetes.default.svc. Use local or client to create the Kubernetes client from the auto-detected configuration (kubeconfig file, in-cluster service account, system properties and environment variables) without applying the options that configure the client (credentials, certificates, trustCerts, apiVersion, connectionTimeout). With a URL, the client also starts from the auto-detected configuration, and these options override it. Not used when the kubernetesClient option is set. | String |
Query Parameters
| Name | Description | Default | Type |
|---|---|---|---|
The default API version of the Kubernetes client (the client default is v1). The client uses it only for a resource type that does not give its own version, and the resource types of these components all give theirs. | String | ||
Not used. It was the DNS domain for the ServiceCall EIP, which was removed in Camel 4.19. | String | ||
An existing Kubernetes client to use. The options that configure the client (masterUrl, credentials, certificates, trustCerts, apiVersion, connectionTimeout) are then not used, and Camel does not close the client when the endpoint stops. | KubernetesClient | ||
The namespace of the resources. For the producer, the CamelKubernetesNamespaceName header takes precedence. When neither is set, the list operations and the consumer cover all namespaces, and the operations on a single resource fail; openshift-deploymentconfigs then uses the namespace of the Kubernetes client, and kubernetes-custom-resources fails. Not used by kubernetes-namespaces, kubernetes-nodes and kubernetes-persistent-volumes, whose resources are cluster-scoped. When Camel creates the client from a URL, it is also the default namespace of the client. | String | ||
The producer operation to perform, such as listPods or createConfigMap; the page of each component lists its operations. When it is not set, the CamelKubernetesOperation header gives the operation; the option takes precedence over the header. | String | ||
Not used. It was the port name for the ServiceCall EIP, which was removed in Camel 4.19. | String | ||
Not used. It was the port protocol for the ServiceCall EIP, which was removed in Camel 4.19. | tcp | String | |
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 starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
The timeout in milliseconds to establish a connection to the API server. When it is not set, the Kubernetes client default of 10000 applies; 0 or less leaves the timeout to the HTTP client implementation. | Integer | ||
The certificate of the certificate authority (CA) to trust for the API server, as Base64-encoded PEM data. Takes precedence over caCertFile. | String | ||
The path of a PEM file with the certificate of the certificate authority (CA) to trust for the API server. caCertData takes precedence over it. | String | ||
The client certificate for TLS client authentication, as Base64-encoded PEM data. Takes precedence over clientCertFile, and is used only together with clientKeyData or clientKeyFile. | String | ||
The path of a PEM file with the client certificate for TLS client authentication. Used only together with clientKeyData or clientKeyFile; clientCertData takes precedence over it. | String | ||
The algorithm of the client private key: RSA (the default of the Kubernetes client) or EC. | String | ||
The private key for TLS client authentication, as Base64-encoded unencrypted PEM data. Takes precedence over clientKeyFile. | String | ||
The path of a file with the private key for TLS client authentication, as unencrypted PEM. clientKeyData takes precedence over it. | String | ||
The password with which the Kubernetes client protects the client private key in the key store that it builds in memory (the client default is changeit). It does not decrypt the key, which must be unencrypted. | String | ||
The bearer token to authenticate to the API server, such as a service account token or an OAuth access token. When it is set, username and password are not used. | String | ||
The password for HTTP basic authentication to the API server. Used only together with username, and not when oauthToken is set. | String | ||
Whether to trust any certificate of the API server without checking it. This is insecure: set caCertData or caCertFile instead. Setting it to false does not override true from the auto-detected configuration. | Boolean | ||
The username for HTTP basic authentication to the API server. Used only together with password, and not when oauthToken is set. | String |
Message Headers
The Kubernetes Secrets component supports the following message header(s), which is/are listed below:
| Name | Description | Default | Type |
|---|---|---|---|
CamelKubernetesOperation (producer) Constant: | The operation of the producer, such as listPods or createConfigMap. Used only when the operation option of the endpoint is not set; one of the two is required. | String | |
CamelKubernetesNamespaceName (producer) Constant: | The namespace of the resource, which takes precedence over the namespace option of the endpoint. For kubernetes-namespaces, it is instead the name of the namespace to get, create, update or delete, where it is required. When no namespace is set, the list operations cover all namespaces (openshift-deploymentconfigs: the namespace of the Kubernetes client), getConfigMap, getDeployment and getDeploymentConfig use the namespace of the Kubernetes client, and the other operations fail; kubernetes-custom-resources requires a namespace for every operation. | String | |
CamelKubernetesSecretsLabels (producer) Constant: | The labels, as a map of label keys to values: listSecretsByLabels (where they are required) lists only the Secrets that have all of them. createSecret and updateSecret do not use them: they take the labels of the Secret in the CamelKubernetesSecret header. | Map | |
CamelKubernetesSecretName (producer) Constant: | The name of the Secret for getSecret and deleteSecret, where it is required. createSecret and updateSecret take the name of the Secret in the CamelKubernetesSecret header. | String | |
CamelKubernetesSecret (producer) Constant: | The Secret to create or update, with its name, labels and data; required for createSecret and updateSecret. | Secret | |
CamelKubernetesSecretsAnnotations (producer) Constant: | The annotations, as a map of keys to values, for createSecret and updateSecret: when set, they replace the annotations of the Secret in the CamelKubernetesSecret header (on a copy, the Secret of the header is not changed). | Map |
Message Body
The message body at each side of the Kubernetes Secrets component: Consumer is the body of the messages the consumer creates, Consumer reply the bodies a consumer that answers its caller can reply with, Producer input the bodies the producer can send or write, and Producer output the body after the producer.
| Side | Body | Description |
|---|---|---|
Producer input |
| Not used: the operation (the operation option, or else the CamelKubernetesOperation header) takes its input from headers: the namespace in CamelKubernetesNamespaceName, the name in CamelKubernetesSecretName, the labels in CamelKubernetesSecretsLabels and the Secret to create or update in CamelKubernetesSecret. |
Producer output |
| A List of Secret for listSecrets and listSecretsByLabels; the Secret for getSecret (null when not found), createSecret and updateSecret; a Boolean for deleteSecret, false when not found.
|
The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers, the sides and the tools that use them are described in Message Body Types.
Example
Kubernetes Secrets Producer Examples
-
listSecrets: this operation lists the secrets on a kubernetes cluster
-
Java
-
XML
-
YAML
from("direct:list")
.to("kubernetes-secrets:///?kubernetesClient=#kubernetesClient&operation=listSecrets")
.to("mock:result"); <route>
<from uri="direct:list"/>
<to uri="kubernetes-secrets:///?kubernetesClient=#kubernetesClient&operation=listSecrets"/>
<to uri="mock:result"/>
</route> - route:
from:
uri: direct:list
steps:
- to:
uri: kubernetes-secrets:///
parameters:
kubernetesClient: "#kubernetesClient"
operation: listSecrets
- to:
uri: mock:result This operation returns a list of secrets from your cluster
-
listSecretsByLabels: this operation lists the Secrets by labels on a kubernetes cluster
-
Java
-
XML
-
YAML
from("direct:listByLabels")
.setHeader("CamelKubernetesSecretsLabels", constant("key1=value1,key2=value2"))
.to("kubernetes-secrets:///?kubernetesClient=#kubernetesClient&operation=listSecretsByLabels")
.to("mock:result"); <route>
<from uri="direct:listByLabels"/>
<setHeader name="CamelKubernetesSecretsLabels">
<constant>key1=value1,key2=value2</constant>
</setHeader>
<to uri="kubernetes-secrets:///?kubernetesClient=#kubernetesClient&operation=listSecretsByLabels"/>
<to uri="mock:result"/>
</route> - route:
from:
uri: direct:listByLabels
steps:
- setHeader:
name: CamelKubernetesSecretsLabels
expression:
constant:
expression: key1=value1,key2=value2
- to:
uri: kubernetes-secrets:///
parameters:
kubernetesClient: "#kubernetesClient"
operation: listSecretsByLabels
- to:
uri: mock:result This operation returns a list of Secrets from your cluster using a label selector (with key1 and key2, with value value1 and value2)
Using secrets properties function with Kubernetes
The camel-kubernetes component include the following secrets related functions:
-
secret- A function to lookup the string property from Kubernetes Secrets. -
secret-binary- A function to lookup the binary property from Kubernetes Secrets.
Camel reads Secrets from the Kubernetes API Server. And when RBAC is enabled on the cluster, the ServiceAccount that is used to run the application needs to have the proper permissions for such access.
Before the Kubernetes property placeholder functions can be used they need to be configured with either (or both)
-
path - A mount path that must be mounted to the running pod, to load the configmaps or secrets from local disk.
-
kubernetes client - Autowired An
io.fabric8.kubernetes.client.KubernetesClientinstance to use for connecting to the Kubernetes API server.
Camel will first use mount paths (if configured) to lookup, and then fallback to use the KubernetesClient.
A secret named mydb could contain username and passwords to connect to a database such as:
myhost = killroy
myport = 5555
myuser = scott
mypass = tiger This can be used in Camel with for example the Postrgres Sink Kamelet:
-
Java
-
XML
-
YAML
from("direct:rome")
.setBody(constant("{ \"username\":\"oscerd\", \"city\":\"Rome\"}"))
.to("kamelet:postgresql-sink?serverName={{secret:mydb/myhost}}"
+ "&serverPort={{secret:mydb/myport}}"
+ "&username={{secret:mydb/myuser}}"
+ "&password={{secret:mydb/mypass}}"
+ "&databaseName=cities"
+ "&query=INSERT INTO accounts (username,city) VALUES (:#username,:#city)"); <camelContext>
<route>
<from uri="direct:rome"/>
<setBody>
<constant>{ "username":"oscerd", "city":"Rome"}</constant>
</setBody>
<to uri="kamelet:postgresql-sink?serverName={{secret:mydb/myhost}}
&serverPort={{secret:mydb/myport}}
&username={{secret:mydb/myuser}}
&password={{secret:mydb/mypass}}
&databaseName=cities
&query=INSERT INTO accounts (username,city) VALUES (:#username,:#city)"/>
</route>
</camelContext> - route:
from:
uri: direct:rome
steps:
- setBody:
constant: '{ "username":"oscerd", "city":"Rome"}'
- to:
uri: kamelet:postgresql-sink
parameters:
serverName: "{{secret:mydb/myhost}}"
serverPort: "{{secret:mydb/myport}}"
username: "{{secret:mydb/myuser}}"
password: "{{secret:mydb/mypass}}"
databaseName: cities
query: "INSERT INTO accounts (username,city) VALUES (:#username,:#city)" The postgres-sink Kamelet can also be configured in application.properties which reduces the configuration in the route above:
camel.kamelet.postgresql-sink.databaseName={{secret:mydb/myhost}}
camel.kamelet.postgresql-sink.serverPort={{secret:mydb/myport}}
camel.kamelet.postgresql-sink.username={{secret:mydb/myuser}}
camel.kamelet.postgresql-sink.password={{secret:mydb/mypass}} Which reduces the route to:
-
Java
-
XML
-
YAML
from("direct:rome")
.setBody(constant("{ \"username\":\"oscerd\", \"city\":\"Rome\"}"))
.to("kamelet:postgresql-sink?databaseName=cities"
+ "&query=INSERT INTO accounts (username,city) VALUES (:#username,:#city)"); <camelContext>
<route>
<from uri="direct:rome"/>
<setBody>
<constant>{ "username":"oscerd", "city":"Rome"}</constant>
</setBody>
<to uri="kamelet:postgresql-sink?databaseName=cities
&query=INSERT INTO accounts (username,city) VALUES (:#username,:#city)"/>
</route>
</camelContext> - route:
from:
uri: direct:rome
steps:
- setBody:
constant: '{ "username":"oscerd", "city":"Rome"}'
- to:
uri: kamelet:postgresql-sink
parameters:
databaseName: cities
query: "INSERT INTO accounts (username,city) VALUES (:#username,:#city)" Automatic Camel context reloading on Secret Refresh
Being able to reload Camel context on a Secret Refresh could be done by specifying the following properties:
camel.vault.kubernetes.refreshEnabled=true
camel.vault.kubernetes.secrets=Secret
camel.main.context-reload-enabled = true where camel.vault.kubernetes.refreshEnabled will enable the automatic context reload and camel.vault.kubernetes.secrets is a regex representing or a comma separated lists of the secrets we want to track for updates.
Whenever a secrets listed in the property, will be updated in the same namespace of the running application, the Camel context will be reloaded, refreshing the secret value.
Camel lists the Secrets of that namespace and then watches them (with a Kubernetes informer), so when RBAC is enabled the ServiceAccount needs the list and watch permissions on secrets. When the API server closes the watch (for example with 410 Gone because the watch is too old), Camel lists the Secrets again, so an update made in the meantime still reloads the context.