Skip to content

Commit

Permalink
Allow to inject data from externally crafted secrets #1261 (camel-k b…
Browse files Browse the repository at this point in the history
…its)
  • Loading branch information
lburgazzoli committed Feb 11, 2020
1 parent 0c6d0ab commit ddcc1b6
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 11 deletions.
11 changes: 11 additions & 0 deletions pkg/trait/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ const (
envVarPodName = "POD_NAME"
envVarCamelKVersion = "CAMEL_K_VERSION"
envVarCamelKRuntimeVersion = "CAMEL_K_RUNTIME_VERSION"
envVarMountPathConfigMaps = "CAMEL_K_MOUNT_PATH_CONFIGMAPS"

// Disabling gosec linter as it may triggers:
//
// pkg/trait/environment.go:41: G101: Potential hardcoded credentials (gosec)
// envVarMountPathSecrets = "CAMEL_K_MOUNT_PATH_SECRETS"
//
// nolint: gosec
envVarMountPathSecrets = "CAMEL_K_MOUNT_PATH_SECRETS"
)

func newEnvironmentTrait() *environmentTrait {
Expand All @@ -58,6 +67,8 @@ func (t *environmentTrait) Configure(e *Environment) (bool, error) {
func (t *environmentTrait) Apply(e *Environment) error {
envvar.SetVal(&e.EnvVars, envVarCamelKVersion, defaults.Version)
envvar.SetVal(&e.EnvVars, envVarCamelKRuntimeVersion, e.RuntimeVersion)
envvar.SetVal(&e.EnvVars, envVarMountPathConfigMaps, ConfigMapsMountPath)
envvar.SetVal(&e.EnvVars, envVarMountPathSecrets, SecretsMountPath)

if t.ContainerMeta {
envvar.SetValFrom(&e.EnvVars, envVarNamespace, "metadata.namespace")
Expand Down
10 changes: 10 additions & 0 deletions pkg/trait/environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ func TestDefaultEnvironment(t *testing.T) {
ns := false
name := false
ck := false
cms := false
secrets := false

env.Resources.VisitDeployment(func(deployment *appsv1.Deployment) {
for _, e := range deployment.Spec.Template.Spec.Containers[0].Env {
Expand All @@ -85,12 +87,20 @@ func TestDefaultEnvironment(t *testing.T) {
if e.Name == envVarCamelKVersion {
ck = true
}
if e.Name == envVarMountPathConfigMaps {
cms = true
}
if e.Name == envVarMountPathSecrets {
secrets = true
}
}
})

assert.True(t, ns)
assert.True(t, name)
assert.True(t, ck)
assert.True(t, cms)
assert.True(t, secrets)
}

func TestEnabledContainerMetaDataEnvVars(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions pkg/trait/trait_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package trait

import (
"context"
"path"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -339,7 +340,7 @@ func TestConfigureVolumesAndMounts(t *testing.T) {

m = findVVolumeMount(mnts, func(m corev1.VolumeMount) bool { return m.Name == "test-configmap" })
assert.NotNil(t, m)
assert.Equal(t, "/etc/camel/conf.d/integration-cm-test-configmap", m.MountPath)
assert.Equal(t, path.Join(ConfigMapsMountPath, "test-configmap"), m.MountPath)

v = findVolume(vols, func(v corev1.Volume) bool { return v.Name == "test-secret" })
assert.NotNil(t, v)
Expand All @@ -348,7 +349,7 @@ func TestConfigureVolumesAndMounts(t *testing.T) {

m = findVVolumeMount(mnts, func(m corev1.VolumeMount) bool { return m.Name == "test-secret" })
assert.NotNil(t, m)
assert.Equal(t, "/etc/camel/conf.d/integration-secret-test-secret", m.MountPath)
assert.Equal(t, path.Join(SecretsMountPath, "test-secret"), m.MountPath)

v = findVolume(vols, func(v corev1.Volume) bool { return v.Name == "testvolume-data" })
assert.NotNil(t, v)
Expand Down
38 changes: 29 additions & 9 deletions pkg/trait/trait_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,29 @@ import (
// True --
const True = "true"

var (
// BasePath --
BasePath = "/etc/camel"

// ConfPath --
ConfPath = path.Join(BasePath, "conf")

// ConfdPath --
ConfdPath = path.Join(BasePath, "conf.d")

// SourcesMountPath --
SourcesMountPath = path.Join(BasePath, "sources")

// ResourcesMountPath --
ResourcesMountPath = path.Join(BasePath, "resources")

// ConfigMapsMountPath --
ConfigMapsMountPath = path.Join(ConfdPath, "_configmaps")

// SecretsMountPath --
SecretsMountPath = path.Join(ConfdPath, "_secrets")
)

// Identifiable represent an identifiable type
type Identifiable interface {
ID() ID
Expand Down Expand Up @@ -389,8 +412,7 @@ func (e *Environment) ComputeSourcesURI() []string {
paths := make([]string, 0, len(sources))

for i, s := range sources {
root := "/etc/camel/sources"
root = path.Join(root, fmt.Sprintf("i-source-%03d", i))
root := path.Join(SourcesMountPath, fmt.Sprintf("i-source-%03d", i))

srcName := strings.TrimPrefix(s.Name, "/")
src := path.Join(root, srcName)
Expand Down Expand Up @@ -427,7 +449,7 @@ func (e *Environment) ConfigureVolumesAndMounts(vols *[]corev1.Volume, mnts *[]c
cmName := fmt.Sprintf("%s-source-%03d", e.Integration.Name, i)
refName := fmt.Sprintf("i-source-%03d", i)
resName := strings.TrimPrefix(s.Name, "/")
resPath := path.Join("/etc/camel/sources", refName)
resPath := path.Join(SourcesMountPath, refName)

if s.ContentRef != "" {
cmName = s.ContentRef
Expand Down Expand Up @@ -465,7 +487,7 @@ func (e *Environment) ConfigureVolumesAndMounts(vols *[]corev1.Volume, mnts *[]c
refName := fmt.Sprintf("i-resource-%03d", i)
resName := strings.TrimPrefix(r.Name, "/")
cmKey := "content"
resPath := path.Join("/etc/camel/resources", refName)
resPath := path.Join(ResourcesMountPath, refName)

if r.ContentRef != "" {
cmName = r.ContentRef
Expand Down Expand Up @@ -523,7 +545,7 @@ func (e *Environment) ConfigureVolumesAndMounts(vols *[]corev1.Volume, mnts *[]c

*mnts = append(*mnts, corev1.VolumeMount{
Name: "integration-properties",
MountPath: "/etc/camel/conf",
MountPath: ConfPath,
})

//
Expand All @@ -532,7 +554,6 @@ func (e *Environment) ConfigureVolumesAndMounts(vols *[]corev1.Volume, mnts *[]c

for _, cmName := range e.CollectConfigurationValues("configmap") {
refName := kubernetes.SanitizeLabel(cmName)
fileName := "integration-cm-" + strings.ToLower(cmName)

*vols = append(*vols, corev1.Volume{
Name: refName,
Expand All @@ -547,7 +568,7 @@ func (e *Environment) ConfigureVolumesAndMounts(vols *[]corev1.Volume, mnts *[]c

*mnts = append(*mnts, corev1.VolumeMount{
Name: refName,
MountPath: path.Join("/etc/camel/conf.d", fileName),
MountPath: path.Join(ConfigMapsMountPath, strings.ToLower(cmName)),
})
}

Expand All @@ -557,7 +578,6 @@ func (e *Environment) ConfigureVolumesAndMounts(vols *[]corev1.Volume, mnts *[]c

for _, secretName := range e.CollectConfigurationValues("secret") {
refName := kubernetes.SanitizeLabel(secretName)
fileName := "integration-secret-" + strings.ToLower(secretName)

*vols = append(*vols, corev1.Volume{
Name: refName,
Expand All @@ -570,7 +590,7 @@ func (e *Environment) ConfigureVolumesAndMounts(vols *[]corev1.Volume, mnts *[]c

*mnts = append(*mnts, corev1.VolumeMount{
Name: refName,
MountPath: path.Join("/etc/camel/conf.d", fileName),
MountPath: path.Join(SecretsMountPath, strings.ToLower(secretName)),
})
}

Expand Down

0 comments on commit ddcc1b6

Please sign in to comment.