Skip to content

Commit f86bc3f

Browse files
authored
fix: corrects vault yaml config (#175)
1 parent 9deb8f0 commit f86bc3f

File tree

2 files changed

+144
-2
lines changed

2 files changed

+144
-2
lines changed

agent/secretsmgr/vault_auth.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ func (a *AuthToken) vaultAuthenticate(_ context.Context, cli *vault.Client) (*va
8888
type AuthAppRole struct {
8989
RoleID string `yaml:"role_id"`
9090
SecretID string `yaml:"secret_id"`
91-
WrappingToken bool `yaml:"wrapping_token,ommitempty"`
92-
MountPath *string `yaml:"mount_path,ommitempty"`
91+
WrappingToken bool `yaml:"wrapping_token,omitempty"`
92+
MountPath *string `yaml:"mount_path,omitempty"`
9393
}
9494

9595
// UnmarshalYAML for AuthAppRole validates required fields after unmarshaling

agent/secretsmgr/vault_auth_test.go

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"testing"
66

77
"github.com/stretchr/testify/assert"
8+
"gopkg.in/yaml.v3"
89
)
910

1011
func TestNewAuthentication(t *testing.T) {
@@ -65,3 +66,144 @@ func TestTokenAuth_Authenticate(t *testing.T) {
6566
assert.Error(t, err)
6667
assert.Nil(t, secret)
6768
}
69+
70+
func TestAuthAppRole_YAMLOmitEmpty(t *testing.T) {
71+
tests := []struct {
72+
name string
73+
authAppRole AuthAppRole
74+
expectedYAML string
75+
description string
76+
}{
77+
{
78+
name: "with all fields",
79+
authAppRole: AuthAppRole{
80+
RoleID: "test-role-id",
81+
SecretID: "test-secret-id",
82+
WrappingToken: true,
83+
MountPath: stringPtr("custom-path"),
84+
},
85+
expectedYAML: `role_id: test-role-id
86+
secret_id: test-secret-id
87+
wrapping_token: true
88+
mount_path: custom-path
89+
`,
90+
description: "All fields should be present in YAML",
91+
},
92+
{
93+
name: "omit empty fields",
94+
authAppRole: AuthAppRole{
95+
RoleID: "test-role-id",
96+
SecretID: "test-secret-id",
97+
WrappingToken: false, // zero value should be omitted
98+
MountPath: nil, // nil pointer should be omitted
99+
},
100+
expectedYAML: `role_id: test-role-id
101+
secret_id: test-secret-id
102+
`,
103+
description: "Zero values should be omitted from YAML due to omitempty tag",
104+
},
105+
{
106+
name: "omit empty mount_path only",
107+
authAppRole: AuthAppRole{
108+
RoleID: "test-role-id",
109+
SecretID: "test-secret-id",
110+
WrappingToken: true,
111+
MountPath: nil, // nil pointer should be omitted
112+
},
113+
expectedYAML: `role_id: test-role-id
114+
secret_id: test-secret-id
115+
wrapping_token: true
116+
`,
117+
description: "Only nil mount_path should be omitted",
118+
},
119+
}
120+
121+
for _, tt := range tests {
122+
t.Run(tt.name, func(t *testing.T) {
123+
// Test marshaling (struct -> YAML)
124+
yamlBytes, err := yaml.Marshal(tt.authAppRole)
125+
assert.NoError(t, err, "Failed to marshal AuthAppRole to YAML")
126+
assert.Equal(t, tt.expectedYAML, string(yamlBytes), tt.description)
127+
128+
// Test unmarshaling (YAML -> struct)
129+
var unmarshaled AuthAppRole
130+
err = yaml.Unmarshal([]byte(tt.expectedYAML), &unmarshaled)
131+
assert.NoError(t, err, "Failed to unmarshal YAML to AuthAppRole")
132+
133+
// Compare the unmarshaled struct with expected values
134+
assert.Equal(t, tt.authAppRole.RoleID, unmarshaled.RoleID, "RoleID should match")
135+
assert.Equal(t, tt.authAppRole.SecretID, unmarshaled.SecretID, "SecretID should match")
136+
assert.Equal(t, tt.authAppRole.WrappingToken, unmarshaled.WrappingToken, "WrappingToken should match")
137+
138+
if tt.authAppRole.MountPath == nil {
139+
assert.Nil(t, unmarshaled.MountPath, "MountPath should be nil")
140+
} else {
141+
assert.NotNil(t, unmarshaled.MountPath, "MountPath should not be nil")
142+
assert.Equal(t, *tt.authAppRole.MountPath, *unmarshaled.MountPath, "MountPath values should match")
143+
}
144+
})
145+
}
146+
}
147+
148+
func TestAuthAppRole_YAMLValidation(t *testing.T) {
149+
tests := []struct {
150+
name string
151+
yamlData string
152+
expectErr bool
153+
errMsg string
154+
}{
155+
{
156+
name: "valid minimal config",
157+
yamlData: `role_id: test-role-id
158+
secret_id: test-secret-id
159+
`,
160+
expectErr: false,
161+
},
162+
{
163+
name: "missing role_id",
164+
yamlData: `secret_id: test-secret-id
165+
`,
166+
expectErr: true,
167+
errMsg: "missing required field 'role_id'",
168+
},
169+
{
170+
name: "missing secret_id",
171+
yamlData: `role_id: test-role-id
172+
`,
173+
expectErr: true,
174+
errMsg: "missing required field 'secret_id'",
175+
},
176+
{
177+
name: "valid with optional fields",
178+
yamlData: `role_id: test-role-id
179+
secret_id: test-secret-id
180+
wrapping_token: true
181+
mount_path: custom-mount
182+
`,
183+
expectErr: false,
184+
},
185+
}
186+
187+
for _, tt := range tests {
188+
t.Run(tt.name, func(t *testing.T) {
189+
var authAppRole AuthAppRole
190+
err := yaml.Unmarshal([]byte(tt.yamlData), &authAppRole)
191+
192+
if tt.expectErr {
193+
assert.Error(t, err)
194+
if tt.errMsg != "" {
195+
assert.Contains(t, err.Error(), tt.errMsg)
196+
}
197+
} else {
198+
assert.NoError(t, err)
199+
assert.NotEmpty(t, authAppRole.RoleID)
200+
assert.NotEmpty(t, authAppRole.SecretID)
201+
}
202+
})
203+
}
204+
}
205+
206+
// Helper function to create a string pointer
207+
func stringPtr(s string) *string {
208+
return &s
209+
}

0 commit comments

Comments
 (0)