prevent memory leak when using control group factors in a policy (#17532)

* prevent a possible memory leak when using control group factors in a policy

* CL
This commit is contained in:
Hamid Ghaf 2022-10-14 19:15:15 -04:00 committed by GitHub
parent 2e1dc4ed24
commit 8a624c1264
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 1 deletions

3
changelog/17532.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:bug
core: prevent memory leak when using control group factors in a policy
```

View File

@ -260,7 +260,11 @@ func NewACL(ctx context.Context, policies []*Policy) (*ACL, error) {
if pc.Permissions.ControlGroup != nil { if pc.Permissions.ControlGroup != nil {
if len(pc.Permissions.ControlGroup.Factors) > 0 { if len(pc.Permissions.ControlGroup.Factors) > 0 {
if existingPerms.ControlGroup == nil { if existingPerms.ControlGroup == nil {
existingPerms.ControlGroup = pc.Permissions.ControlGroup cg, err := pc.Permissions.ControlGroup.Clone()
if err != nil {
return nil, err
}
existingPerms.ControlGroup = cg
} else { } else {
existingPerms.ControlGroup.Factors = append(existingPerms.ControlGroup.Factors, pc.Permissions.ControlGroup.Factors...) existingPerms.ControlGroup.Factors = append(existingPerms.ControlGroup.Factors, pc.Permissions.ControlGroup.Factors...)
} }

View File

@ -149,6 +149,17 @@ type ControlGroup struct {
Factors []*ControlGroupFactor Factors []*ControlGroupFactor
} }
func (c *ControlGroup) Clone() (*ControlGroup, error) {
clonedControlGroup, err := copystructure.Copy(c)
if err != nil {
return nil, err
}
cg := clonedControlGroup.(*ControlGroup)
return cg, nil
}
type ControlGroupFactor struct { type ControlGroupFactor struct {
Name string Name string
Identity *IdentityFactor `hcl:"identity"` Identity *IdentityFactor `hcl:"identity"`