Skip to content

Commit c0a9852

Browse files
Merge pull request #287 from 13bscsaamjad/sec-eng-mnt-path-opt
feat/fix: make path optional in secret engine mount
2 parents 2cadf68 + c4835be commit c0a9852

File tree

3 files changed

+92
-9
lines changed

3 files changed

+92
-9
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package v1alpha1
2+
3+
import (
4+
"testing"
5+
6+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
7+
)
8+
9+
func TestSecretEngineMountGetPath(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
mount *SecretEngineMount
13+
expectedPath string
14+
}{
15+
{
16+
name: "with path and name specified",
17+
mount: &SecretEngineMount{
18+
ObjectMeta: metav1.ObjectMeta{
19+
Name: "test-mount",
20+
},
21+
Spec: SecretEngineMountSpec{
22+
Path: "custom-path",
23+
Name: "custom-name",
24+
},
25+
},
26+
expectedPath: "sys/mounts/custom-path/custom-name",
27+
},
28+
{
29+
name: "with path but no name specified",
30+
mount: &SecretEngineMount{
31+
ObjectMeta: metav1.ObjectMeta{
32+
Name: "test-mount",
33+
},
34+
Spec: SecretEngineMountSpec{
35+
Path: "custom-path",
36+
},
37+
},
38+
expectedPath: "sys/mounts/custom-path/test-mount",
39+
},
40+
{
41+
name: "with name but no path specified",
42+
mount: &SecretEngineMount{
43+
ObjectMeta: metav1.ObjectMeta{
44+
Name: "test-mount",
45+
},
46+
Spec: SecretEngineMountSpec{
47+
Name: "custom-name",
48+
},
49+
},
50+
expectedPath: "sys/mounts/custom-name",
51+
},
52+
{
53+
name: "with neither path nor name specified",
54+
mount: &SecretEngineMount{
55+
ObjectMeta: metav1.ObjectMeta{
56+
Name: "test-mount",
57+
},
58+
Spec: SecretEngineMountSpec{},
59+
},
60+
expectedPath: "sys/mounts/test-mount",
61+
},
62+
}
63+
64+
for _, tt := range tests {
65+
t.Run(tt.name, func(t *testing.T) {
66+
result := tt.mount.GetPath()
67+
if result != tt.expectedPath {
68+
t.Errorf("GetPath() = %v, expected %v", result, tt.expectedPath)
69+
}
70+
})
71+
}
72+
}

api/v1alpha1/secretenginemount_types.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,21 @@ func (d *SecretEngineMount) IsDeletable() bool {
4141
}
4242

4343
func (d *SecretEngineMount) GetPath() string {
44+
var pathComponent string
45+
if d.Spec.Path != "" {
46+
pathComponent = string(d.Spec.Path)
47+
} else {
48+
// When Path is empty, use the name directly as the mount path
49+
if d.Spec.Name != "" {
50+
return vaultutils.CleansePath(d.GetEngineListPath() + "/" + d.Spec.Name)
51+
}
52+
return vaultutils.CleansePath(d.GetEngineListPath() + "/" + d.Name)
53+
}
54+
4455
if d.Spec.Name != "" {
45-
return vaultutils.CleansePath(d.GetEngineListPath() + "/" + string(d.Spec.Path) + "/" + d.Spec.Name)
56+
return vaultutils.CleansePath(d.GetEngineListPath() + "/" + pathComponent + "/" + d.Spec.Name)
4657
}
47-
return vaultutils.CleansePath(d.GetEngineListPath() + "/" + string(d.Spec.Path) + "/" + d.Name)
58+
return vaultutils.CleansePath(d.GetEngineListPath() + "/" + pathComponent + "/" + d.Name)
4859
}
4960
func (d *SecretEngineMount) GetPayload() map[string]interface{} {
5061
return d.Spec.toMap()
@@ -101,10 +112,10 @@ type SecretEngineMountSpec struct {
101112

102113
Mount `json:",inline"`
103114

104-
// Path at which this secret engine will be available
105-
// The final path in Vault will be {[spec.authentication.namespace]}/{spec.path}/{metadata.name}.
106-
// The authentication role must have the following capabilities = [ "create", "read", "update", "delete"] on that path /sys/mounts/{[spec.authentication.namespace]}/{spec.path}/{metadata.name}.
107-
// +kubebuilder:validation:Required
115+
// Path at which this secret engine will be available. If not specified, defaults to the resource name (/sys/mounts/{[spec.authentication.namespace]}/{metadata.name}).
116+
// The final path in Vault will be {[spec.authentication.namespace]}/{[spec.path]}/{metadata.name}.
117+
// The authentication role must have the following capabilities = [ "create", "read", "update", "delete"] on computed path /sys/mounts/{[spec.authentication.namespace]}/{[spec.path]}/{metadata.name} or /sys/mounts/{[spec.authentication.namespace]}/{metadata.name} if path is empty.
118+
// +kubebuilder:validation:Optional
108119
Path vaultutils.Path `json:"path,omitempty"`
109120

110121
// The name of the obejct created in Vault. If this is specified it takes precedence over {metatada.name}

config/crd/bases/redhatcop.redhat.io_secretenginemounts.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,9 @@ spec:
214214
x-kubernetes-map-type: granular
215215
path:
216216
description: |-
217-
Path at which this secret engine will be available
218-
The final path in Vault will be {[spec.authentication.namespace]}/{spec.path}/{metadata.name}.
219-
The authentication role must have the following capabilities = [ "create", "read", "update", "delete"] on that path /sys/mounts/{[spec.authentication.namespace]}/{spec.path}/{metadata.name}.
217+
Path at which this secret engine will be available. If not specified, defaults to the resource name (/sys/mounts/{[spec.authentication.namespace]}/{metadata.name}).
218+
The final path in Vault will be {[spec.authentication.namespace]}/{[spec.path]}/{metadata.name}.
219+
The authentication role must have the following capabilities = [ "create", "read", "update", "delete"] on computed path /sys/mounts/{[spec.authentication.namespace]}/{[spec.path]}/{metadata.name} or /sys/mounts/{[spec.authentication.namespace]}/{metadata.name} if path is empty.
220220
pattern: ^(?:/?[\w;:@&=\$-\.\+]*)+/?
221221
type: string
222222
sealWrap:

0 commit comments

Comments
 (0)