Skip to content

Commit 1a96f93

Browse files
committed
fix(account): validate environment name when loading a group
Adds a check to ensure each environment in a group has a name. Updates tests to cover the case where the environment name is missing and adds a corresponding test YAML file.
1 parent 02810f9 commit 1a96f93

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

pkg/account/persistence/loader/load_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,4 +408,10 @@ func TestLoad(t *testing.T) {
408408
assert.Error(t, err)
409409
assert.Contains(t, err.Error(), "policy definition is ambiguous")
410410
})
411+
412+
t.Run("Loading a group with a missing environment name produces an error", func(t *testing.T) {
413+
_, err := Load(afero.NewOsFs(), "testdata/group-missing-env-name.yaml")
414+
assert.Error(t, err)
415+
assert.Contains(t, err.Error(), "missing required field 'environment'")
416+
})
411417
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
groups:
2+
- name: My Group
3+
id: my-group
4+
5+
environments:
6+
# Missing environment name
7+
- permissions:
8+
- View environment
9+
policies:
10+
- View environment
11+
- type: reference
12+
id: my-policy

pkg/account/persistence/loader/validate.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,8 @@ func validateGroup(g types.Group) error {
202202
}
203203

204204
for _, env := range g.Environment {
205-
for _, policyRef := range env.Policies {
206-
if err := validatePolicyBinding(policyRef); err != nil {
207-
return err
208-
}
205+
if err := validateGroupEnvironment(env, g.ID); err != nil {
206+
return err
209207
}
210208
}
211209

@@ -221,6 +219,18 @@ func validateGroup(g types.Group) error {
221219
return nil
222220
}
223221

222+
func validateGroupEnvironment(env types.Environment, groupID string) error {
223+
if env.Name == "" {
224+
return fmt.Errorf("missing required field 'environment' for 'environments' in group '%s'", groupID)
225+
}
226+
for _, policyRef := range env.Policies {
227+
if err := validatePolicyBinding(policyRef); err != nil {
228+
return err
229+
}
230+
}
231+
return nil
232+
}
233+
224234
func validateBoundary(b types.Boundary) error {
225235
if b.ID == "" {
226236
return errors.New("missing required field 'id' for boundary")

0 commit comments

Comments
 (0)