Skip to content

Commit 9545833

Browse files
committed
SecurityGroup: Tighten adoption filter
ListOSResourcesForAdoption only filtered by name. Resolve ProjectRef when set and include ProjectID and the Stateful field in the list filter to prevent adopting a security group from the wrong project or with the wrong stateful/stateless behavior.
1 parent 71912ee commit 9545833

1 file changed

Lines changed: 24 additions & 2 deletions

File tree

internal/controllers/securitygroup/actuator.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,33 @@ func (actuator securityGroupActuator) GetOSResourceByID(ctx context.Context, id
7777
}
7878

7979
func (actuator securityGroupActuator) ListOSResourcesForAdoption(ctx context.Context, obj *orcv1alpha1.SecurityGroup) (securityGroupIterator, bool) {
80-
if obj.Spec.Resource == nil {
80+
resource := obj.Spec.Resource
81+
if resource == nil {
8182
return nil, false
8283
}
8384

84-
listOpts := groups.ListOpts{Name: getResourceName(obj)}
85+
// Resolve the project ID from ProjectRef if set. Without the project
86+
// ID, adoption with admin-scoped credentials could match a security
87+
// group in the wrong project.
88+
var projectID string
89+
if resource.ProjectRef != nil {
90+
project, rs := dependency.FetchDependency(
91+
ctx, actuator.k8sClient, obj.Namespace, resource.ProjectRef, "Project",
92+
func(dep *orcv1alpha1.Project) bool {
93+
return orcv1alpha1.IsAvailable(dep) && dep.Status.ID != nil
94+
},
95+
)
96+
if needsReschedule, _ := rs.NeedsReschedule(); needsReschedule {
97+
return nil, false
98+
}
99+
projectID = ptr.Deref(project.Status.ID, "")
100+
}
101+
102+
listOpts := groups.ListOpts{
103+
Name: getResourceName(obj),
104+
ProjectID: projectID,
105+
Stateful: resource.Stateful,
106+
}
85107
return actuator.osClient.ListSecGroup(ctx, listOpts), true
86108
}
87109

0 commit comments

Comments
 (0)