Skip to content
This repository was archived by the owner on Nov 28, 2022. It is now read-only.

Commit ffa41a0

Browse files
authored
Do not inject sidecars if only job and secrets modes are enabled and secrets type is "static" (#45)
1 parent 2877ebb commit ffa41a0

25 files changed

+323
-70
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog for Vault Sidecar Injector
22

3+
## Release v7.1.1 - 2021-04-02
4+
5+
**Fixed**
6+
7+
- [VSI #45](https://github.com/Talend/vault-sidecar-injector/pull/45) - Do not inject sidecars if only job and secrets modes are enabled and secrets type is "static"
8+
39
## Release v7.1.0 - 2021-02-09
410

511
A new `sidecar.vault.talend.org/vault-image` annotation has been added to override the default injected image. Refer to the [samples](samples) for a [working example](samples/app-dep-10-secrets_custom_image_notify.yaml).

VERSION_CHART

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4.2.0
1+
4.2.1

VERSION_RELEASE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7.1.0
1+
7.1.1

VERSION_VSI

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7.1.0
1+
7.1.1

doc/Usage.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ Depending on the modes you decide to enable and whether you opt for static or dy
113113
<td align="center">X</td><td/><td/><td/><td align="center" bgcolor="grey">X</td><td bgcolor="grey"/>
114114
</tr>
115115
<tr>
116-
<td align="center">X</td><td/><td/><td align="center">X<b><i>¹</i></b></td><td align="center" bgcolor="grey">X (secrets)</td><td align="center" bgcolor="grey">X (job)</td>
116+
<td align="center">X</td><td/><td/><td align="center">X<b><i>¹</i></b></td><td align="center" bgcolor="grey">X (secrets)</td><td align="center" bgcolor="grey"/>
117117
</tr>
118118
<tr>
119119
<td/><td align="center">X</td><td/><td/><td bgcolor="grey"/><td align="center" bgcolor="grey">X</td>
@@ -141,6 +141,6 @@ Depending on the modes you decide to enable and whether you opt for static or dy
141141
</tr>
142142
</table>
143143

144-
> **[1]** *on job mode:* if you only set mode annotation's value to "job", `secrets` mode will be enabled automatically and configured to handle dynamic secrets (unless you set `sidecar.vault.talend.org/secrets-type` to "static" but note that in this situation, there is no need, although we do not prevent it, to enable job mode as no Vault Agent will be injected as sidecar).
144+
> **[1]** *on job mode:* if you only set mode annotation's value to "job", `secrets` mode will be enabled automatically and configured to handle dynamic secrets (unless you set `sidecar.vault.talend.org/secrets-type` to "static" but note that in this situation, there is no need, although we do not prevent it, to enable job mode explicitly as no sidecar will be injected).
145145
146-
> **[2]** *on number of injected sidecars:* for Kubernetes **Deployment** workloads, **only one sidecar container** is added to your pod to handle dynamic secrets and/or proxy. For Kubernetes **Job** workloads, **two sidecars** are injected to achieve the same tasks.
146+
> **[2]** *on number of injected sidecars:* for Kubernetes **Deployment** workloads, **only one sidecar container** is added to your pod to handle dynamic secrets and/or proxy. For Kubernetes **Job** workloads, **two sidecars** are injected to achieve the same tasks (or 0 in case you only enable job mode with static secrets).

doc/announcements/Static-vs-Dynamic-Secrets.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Available with `Vault Sidecar Injector` version **`6.0.0`**, *static secrets* su
66

77
A new annotation, `sidecar.vault.talend.org/secrets-type`, is supported to explicitly define what kind of secrets you intend to fetch, default being *dynamic secrets*.
88

9-
When *static secrets* are set, `Vault Sidecar Injector` will only inject an init container in your workload's pod. Fetched secrets will be stored in a file in a shared memory volume, the same way it is already done for *dynamic secrets*. As a result, if you do not enable other modes (e.g. *proxy*, *job*) no sidecar will be added. It also means that you don't have to leverage hooks or wait for the injected Vault Agent to fetch your secrets: your workload can access the values right after its container is started. The drawback of course is that your secrets **will not be automatically refreshed upon changes**, opt for *dynamic secrets* if this behavior is required.
9+
When *static secrets* are set, `Vault Sidecar Injector` will only inject an init container in your workload's pod. Fetched secrets will be stored in a file in a shared memory volume, the same way it is already done for *dynamic secrets*. As a result, if you do not enable other modes (e.g. *proxy*) no sidecar will be added (Note that enabling *job* mode with static secrets will not incur any sidecar injection either). It also means that you don't have to leverage hooks or wait for the injected Vault Agent to fetch your secrets: your workload can access the values right after its container is started. The drawback of course is that your secrets **will not be automatically refreshed upon changes**, opt for *dynamic secrets* if this behavior is required.
1010

1111
If you enable several modes, you may end up with both init container and sidecar(s) in your workload. A comprehensive table is provided in the main documention in section [Modes and Injection Config Overview](../Usage.md#modes-and-injection-config-overview).
1212

pkg/mode/constants.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright © 2019-2021 Talend - www.talend.com
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package mode
16+
17+
const (
18+
//--- Vault Sidecar Injector supported modes
19+
VaultInjectorModeSecrets = "secrets" // Enable fetching of secrets from Vault store
20+
VaultInjectorModeProxy = "proxy" // Enable local Vault proxy
21+
VaultInjectorModeJob = "job" // Enable handling of Kubernetes Job
22+
)

pkg/mode/job/constants.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,3 @@ const (
2525
jobContainerNameEnv = "VSI_JOB_CNT_NAME" // Env var for name of the app job's container
2626
jobWorkloadEnv = "VSI_JOB_WORKLOAD" // Env var set to "true" if submitted workload is a k8s job
2727
)
28-
29-
const (
30-
//--- Vault Sidecar Injector supported modes
31-
VaultInjectorModeJob = "job" // Enable handling of Kubernetes Job
32-
)

pkg/mode/job/job-func-inject.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright © 2019-2020 Talend - www.talend.com
1+
// Copyright © 2019-2021 Talend - www.talend.com
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -17,6 +17,8 @@ package job
1717
import (
1818
"errors"
1919
ctx "talend/vault-sidecar-injector/pkg/context"
20+
m "talend/vault-sidecar-injector/pkg/mode"
21+
"talend/vault-sidecar-injector/pkg/mode/secrets"
2022

2123
corev1 "k8s.io/api/core/v1"
2224
"k8s.io/klog"
@@ -25,13 +27,21 @@ import (
2527
func jobModeInject(containerBasePath string, podContainers []corev1.Container, containerName string, env []corev1.EnvVar, context *ctx.InjectionContext) (bool, error) {
2628
if (containerBasePath == ctx.JsonPathContainers) && (len(podContainers) != 1) {
2729
err := errors.New("Submitted pod should contain only one container")
28-
klog.Errorf("[%s] %s", VaultInjectorModeJob, err.Error())
30+
klog.Errorf("[%s] %s", m.VaultInjectorModeJob, err.Error())
2931
return false, err
3032
}
3133

34+
// If static secrets and job (+ secrets as it'll be enabled also) are the only enabled modes then do not inject job containers as sidecars (no need for job babysitter nor Vault Agent)
35+
if (containerBasePath == ctx.JsonPathContainers) &&
36+
m.IsEnabledModes(context.ModesStatus, []string{m.VaultInjectorModeSecrets, m.VaultInjectorModeJob}) &&
37+
secrets.IsSecretsStatic(context) {
38+
klog.Infof("[%s] Static secrets in use and only enabled modes are '%s' and '%s': skip injecting job container %s (path: %s)", m.VaultInjectorModeJob, m.VaultInjectorModeJob, m.VaultInjectorModeSecrets, containerName, containerBasePath)
39+
return false, nil
40+
}
41+
3242
for _, cntName := range jobContainerNames[containerBasePath] {
3343
if cntName == containerName {
34-
klog.Infof("[%s] Injecting container %s (path: %s)", VaultInjectorModeJob, containerName, containerBasePath)
44+
klog.Infof("[%s] Injecting container %s (path: %s)", m.VaultInjectorModeJob, containerName, containerBasePath)
3545

3646
// Resolve job env vars
3747
for envIdx := range env {

pkg/mode/job/job.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright © 2019-2020 Talend - www.talend.com
1+
// Copyright © 2019-2021 Talend - www.talend.com
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -22,7 +22,7 @@ func init() {
2222
// Register mode
2323
m.RegisterMode(
2424
m.VaultInjectorModeInfo{
25-
Key: VaultInjectorModeJob,
25+
Key: m.VaultInjectorModeJob,
2626
DefaultMode: false,
2727
EnableDefaultMode: true, // Default mode will also be enabled if job is **the only mode on** (as it does not make sense to have only this mode)
2828
InjectContainerFunc: jobModeInject,

0 commit comments

Comments
 (0)