Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibility to use template and template-file annotations without sec… #505

Merged
merged 8 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 60 additions & 41 deletions agent-inject/agent/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,60 +589,79 @@ func Init(pod *corev1.Pod, cfg AgentConfig) error {
func (a *Agent) secrets() []*Secret {
var secrets []*Secret

for name, path := range a.Annotations {
secretName := fmt.Sprintf("%s-", AnnotationAgentInjectSecret)
if strings.Contains(name, secretName) {
raw := strings.ReplaceAll(name, secretName, "")
name := raw

if ok, _ := a.preserveSecretCase(raw); !ok {
name = strings.ToLower(raw)
}

if name == "" {
for annotationName, annotationValue := range a.Annotations {
saturn4er marked this conversation as resolved.
Show resolved Hide resolved
var (
secret *Secret
raw, name string
ok bool
)
switch {
// parsing vault.hashicorp.com/agent-inject-secret-<name>: <path>
case strings.Contains(annotationName, fmt.Sprintf("%s-", AnnotationAgentInjectSecret)):
raw, name, ok = a.secretName(annotationName, AnnotationAgentInjectSecret)
if !ok {
continue
}

s := &Secret{Name: name, Path: path}

templateName := fmt.Sprintf("%s-%s", AnnotationAgentInjectTemplate, raw)
if val, ok := a.Annotations[templateName]; ok {
s.Template = val
}
if s.Template == "" {
templateFileAnnotation := fmt.Sprintf("%s-%s", AnnotationAgentInjectTemplateFile, raw)
if val, ok := a.Annotations[templateFileAnnotation]; ok {
s.TemplateFile = val
}
secret = &Secret{Name: name, Path: annotationValue}
// parsing vault.hashicorp.com/agent-inject-secret-template-file-<name>: <template-file>
case strings.Contains(annotationName, fmt.Sprintf("%s-", AnnotationAgentInjectTemplateFile)):
raw, name, ok = a.secretName(annotationName, AnnotationAgentInjectTemplateFile)
if !ok {
continue
}

s.MountPath = a.Annotations[AnnotationVaultSecretVolumePath]
mountPathAnnotationName := fmt.Sprintf("%s-%s", AnnotationVaultSecretVolumePath, raw)
if val, ok := a.Annotations[mountPathAnnotationName]; ok {
s.MountPath = val
secret = &Secret{Name: name, TemplateFile: annotationValue}
// parsing vault.hashicorp.com/agent-inject-secret-template-<name>: <template>
case strings.Contains(annotationName, fmt.Sprintf("%s-", AnnotationAgentInjectTemplate)):
raw, name, ok = a.secretName(annotationName, AnnotationAgentInjectTemplate)
if !ok {
continue
}
secret = &Secret{Name: name, Template: annotationValue}
default:
continue
}

commandName := fmt.Sprintf("%s-%s", AnnotationAgentInjectCommand, raw)
if val, ok := a.Annotations[commandName]; ok {
s.Command = val
}
secret.MountPath = a.Annotations[AnnotationVaultSecretVolumePath]
mountPathAnnotationName := fmt.Sprintf("%s-%s", AnnotationVaultSecretVolumePath, raw)
if val, ok := a.Annotations[mountPathAnnotationName]; ok {
secret.MountPath = val
}

file := fmt.Sprintf("%s-%s", AnnotationAgentInjectFile, raw)
if val, ok := a.Annotations[file]; ok {
s.FilePathAndName = val
}
commandName := fmt.Sprintf("%s-%s", AnnotationAgentInjectCommand, raw)
if val, ok := a.Annotations[commandName]; ok {
secret.Command = val
}

filePerm := fmt.Sprintf("%s-%s", AnnotationAgentInjectFilePermission, raw)
if val, ok := a.Annotations[filePerm]; ok {
s.FilePermission = val
}
file := fmt.Sprintf("%s-%s", AnnotationAgentInjectFile, raw)
if val, ok := a.Annotations[file]; ok {
secret.FilePathAndName = val
}

secrets = append(secrets, s)
filePerm := fmt.Sprintf("%s-%s", AnnotationAgentInjectFilePermission, raw)
if val, ok := a.Annotations[filePerm]; ok {
secret.FilePermission = val
}

secrets = append(secrets, secret)
}
return secrets
}

func (a *Agent) secretName(annotationName, annotation string) (raw string, name string, notEmpty bool) {
raw = strings.ReplaceAll(annotationName, fmt.Sprintf("%s-", annotation), "")
name = raw

if ok, _ := a.preserveSecretCase(raw); !ok {
name = strings.ToLower(raw)
}

if name == "" {
return "", "", false
}

return raw, name, true
}

func (a *Agent) inject() (bool, error) {
raw, ok := a.Annotations[AnnotationAgentInject]
if !ok {
Expand Down
49 changes: 16 additions & 33 deletions agent-inject/agent/annotations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import (
"strings"
"testing"

"github.com/hashicorp/vault-k8s/agent-inject/internal"
corev1 "k8s.io/api/core/v1"

"github.com/hashicorp/vault-k8s/agent-inject/internal"

jsonpatch "github.com/evanphx/json-patch"
"github.com/hashicorp/vault/sdk/helper/pointerutil"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -196,7 +197,7 @@ func TestSecretAnnotationsWithPreserveCaseSensitivityFlagOff(t *testing.T) {

if tt.expectedKey != "" {
if len(agent.Secrets) == 0 {
t.Error("Secrets length was zero, it shouldn't have been")
t.Errorf("Secrets length was zero, it shouldn't have been: %s", tt.key)
}
if agent.Secrets[0].Name != tt.expectedKey {
t.Errorf("expected %s, got %s", tt.expectedKey, agent.Secrets[0].Name)
Expand Down Expand Up @@ -348,44 +349,30 @@ func TestSecretLocationFileAnnotations(t *testing.T) {
func TestSecretTemplateAnnotations(t *testing.T) {
tests := []struct {
annotations map[string]string
expectedExists bool
expectedKey string
expectedTemplate string
}{
{
map[string]string{
"vault.hashicorp.com/agent-inject-secret-foobar": "test1",
"vault.hashicorp.com/agent-inject-template-foobar": "foobarTemplate",
}, "foobar", "foobarTemplate",
}, true, "foobar", "foobarTemplate",
},
{
map[string]string{
"vault.hashicorp.com/agent-inject-secret-foobar2": "test2",
"vault.hashicorp.com/agent-inject-template-foobar": "",
}, "foobar2", "",
}, true, "foobar", "",
},
{
map[string]string{
"vault.hashicorp.com/agent-inject-secret-foobar": "test1",
"vault.hashicorp.com/agent-inject-templat-foobar": "foobarTemplate",
}, "foobar", "",
},
{
map[string]string{
"vault.hashicorp.com/agent-inject-secret-foobar": "test1",
"vault.hashicorp.com/agent-inject-template-foobar2": "foobarTemplate",
tomhjp marked this conversation as resolved.
Show resolved Hide resolved
}, "foobar", "",
},
{
map[string]string{
"vault.hashicorp.com/agent-inject-secret-foobar2": "test1",
"vault.hashicorp.com/agent-inject-template-foobar": "foobarTemplate",
}, "foobar2", "",
}, false, "foobar", "",
},
{
map[string]string{
"vault.hashicorp.com/agent-inject-secret-foobar2": "test1",
"vault.hashicorp.com/agent-inject-TEMPLATE-foobar": "foobarTemplate",
}, "foobar2", "",
}, false, "foobar2", "",
},
}

Expand All @@ -404,7 +391,11 @@ func TestSecretTemplateAnnotations(t *testing.T) {
}

if len(agent.Secrets) == 0 {
t.Error("Secrets length was zero, it shouldn't have been")
if tt.expectedExists {
t.Error("Secrets length was zero, it shouldn't have been")
} else {
continue
}
}

if agent.Secrets[0].Name != tt.expectedKey {
Expand All @@ -424,24 +415,20 @@ func TestSecretMixedTemplatesAnnotations(t *testing.T) {
}{
{
map[string]string{
"vault.hashicorp.com/agent-inject-secret-foobar": "test1",
"vault.hashicorp.com/agent-inject-template-foobar": "",
tomhjp marked this conversation as resolved.
Show resolved Hide resolved
"vault.hashicorp.com/agent-inject-template-file-foobar": "/etc/config.tmpl",
"vault.hashicorp.com/agent-inject-secret-test2": "test2",
"vault.hashicorp.com/agent-inject-template-test2": "foobarTemplate",
"vault.hashicorp.com/agent-inject-template-file-test2": "",
},
map[string]Secret{
"foobar": {
Name: "foobar",
Path: "test1",
Path: "",
Template: "",
TemplateFile: "/etc/config.tmpl",
MountPath: secretVolumePath,
},
"test2": {
Name: "test2",
Path: "test2",
Path: "",
Template: "foobarTemplate",
TemplateFile: "",
MountPath: secretVolumePath,
Expand Down Expand Up @@ -492,15 +479,11 @@ func TestSecretTemplateFileAnnotations(t *testing.T) {
}{
{
map[string]string{
"vault.hashicorp.com/agent-inject-secret-foobar": "test1",
"vault.hashicorp.com/agent-inject-template-foobar": "foobarTemplate",
"vault.hashicorp.com/agent-inject-template-file-foobar": "/etc/config.tmpl",
"vault.hashicorp.com/agent-inject-template-foobar": "foobarTemplate",
}, "foobar", "foobarTemplate", "",
},
{
map[string]string{
"vault.hashicorp.com/agent-inject-secret-foobar": "test1",
"vault.hashicorp.com/agent-inject-template-foobar": "",
"vault.hashicorp.com/agent-inject-template-file-foobar": "/etc/config.tmpl",
}, "foobar", "", "/etc/config.tmpl",
},
Expand Down
4 changes: 1 addition & 3 deletions agent-inject/agent/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func TestNewConfig(t *testing.T) {
AnnotationVaultClientKey: "client-key",
AnnotationVaultSecretVolumePath: "/vault/secrets",
AnnotationProxyAddress: "http://proxy:3128",
"vault.hashicorp.com/agent-inject-secret-foo": "db/creds/foo",
tomhjp marked this conversation as resolved.
Show resolved Hide resolved
"vault.hashicorp.com/agent-inject-template-foo": "template foo",
"vault.hashicorp.com/agent-inject-secret-bar": "db/creds/bar",

Expand All @@ -38,7 +37,6 @@ func TestNewConfig(t *testing.T) {
fmt.Sprintf("%s-%s", AnnotationVaultSecretVolumePath, "different-path"): "/etc/container_environment",

// render this secret from a template on disk
"vault.hashicorp.com/agent-inject-secret-with-file-template": "with-file-template",
fmt.Sprintf("%s-%s", AnnotationAgentInjectTemplateFile, "with-file-template"): "/etc/file-template",

"vault.hashicorp.com/agent-inject-command-bar": "pkill -HUP app",
Expand Down Expand Up @@ -809,7 +807,7 @@ func TestConfigTelemetry(t *testing.T) {
"vault.hashicorp.com/agent-telemetry-stackdriver_location": "useast-1",
"vault.hashicorp.com/agent-telemetry-stackdriver_namespace": "foo",
"vault.hashicorp.com/agent-telemetry-stackdriver_debug_logs": "false",
"vault.hashicorp.com/agent-telemetry-prefix_filter": `["+vault.token", "-vault.expire", "+vault.expire.num_leases"]`,
"vault.hashicorp.com/agent-telemetry-prefix_filter": `["+vault.token", "-vault.expire", "+vault.expire.num_leases"]`,
},
&Telemetry{
UsageGaugePeriod: "10m",
Expand Down