-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add support for optionally enforcing NLB security groups on PrivateLi… #3594
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package elbv2 | |
import ( | ||
"context" | ||
"fmt" | ||
|
||
awssdk "github.com/aws/aws-sdk-go/aws" | ||
elbv2sdk "github.com/aws/aws-sdk-go/service/elbv2" | ||
"github.com/go-logr/logr" | ||
|
@@ -75,6 +76,12 @@ func (m *defaultLoadBalancerManager) Create(ctx context.Context, resLB *elbv2mod | |
return elbv2model.LoadBalancerStatus{}, err | ||
} | ||
|
||
if resLB.Spec.Type == elbv2model.LoadBalancerTypeNetwork && resLB.Spec.SecurityGroupsInboundRulesOnPrivateLink != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, we can only set it after LB creation |
||
if err := m.updateSDKLoadBalancerWithSecurityGroups(ctx, resLB, sdkLB); err != nil { | ||
return elbv2model.LoadBalancerStatus{}, err | ||
} | ||
} | ||
|
||
return buildResLoadBalancerStatus(sdkLB), nil | ||
} | ||
|
||
|
@@ -186,27 +193,39 @@ func (m *defaultLoadBalancerManager) updateSDKLoadBalancerWithSecurityGroups(ctx | |
} | ||
desiredSecurityGroups := sets.NewString(awssdk.StringValueSlice(securityGroups)...) | ||
currentSecurityGroups := sets.NewString(awssdk.StringValueSlice(sdkLB.LoadBalancer.SecurityGroups)...) | ||
if desiredSecurityGroups.Equal(currentSecurityGroups) { | ||
|
||
isEnforceSGInboundRulesOnPrivateLinkUpdated, currentEnforceSecurityGroupInboundRulesOnPrivateLinkTraffic, desiredEnforceSecurityGroupInboundRulesOnPrivateLinkTraffic := isEnforceSGInboundRulesOnPrivateLinkUpdated(resLB, sdkLB) | ||
if desiredSecurityGroups.Equal(currentSecurityGroups) && !isEnforceSGInboundRulesOnPrivateLinkUpdated { | ||
return nil | ||
} | ||
|
||
var changeDescriptions []string | ||
|
||
if !desiredSecurityGroups.Equal(currentSecurityGroups) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
if ! isEnforceSGInboundRulesOnPrivateLinkUpdated { There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
changeSecurityGroupsDesc := fmt.Sprintf("%v => %v", currentSecurityGroups.List(), desiredSecurityGroups.List()) | ||
changeDescriptions = append(changeDescriptions, "changeSecurityGroups", changeSecurityGroupsDesc) | ||
} | ||
|
||
req := &elbv2sdk.SetSecurityGroupsInput{ | ||
LoadBalancerArn: sdkLB.LoadBalancer.LoadBalancerArn, | ||
SecurityGroups: securityGroups, | ||
} | ||
changeDesc := fmt.Sprintf("%v => %v", currentSecurityGroups.List(), desiredSecurityGroups.List()) | ||
m.logger.Info("modifying loadBalancer securityGroups", | ||
"stackID", resLB.Stack().StackID(), | ||
"resourceID", resLB.ID(), | ||
"arn", awssdk.StringValue(sdkLB.LoadBalancer.LoadBalancerArn), | ||
"change", changeDesc) | ||
|
||
if isEnforceSGInboundRulesOnPrivateLinkUpdated { | ||
changeEnforceSecurityGroupInboundRulesOnPrivateLinkTrafficDesc := fmt.Sprintf("%v => %v", currentEnforceSecurityGroupInboundRulesOnPrivateLinkTraffic, desiredEnforceSecurityGroupInboundRulesOnPrivateLinkTraffic) | ||
changeDescriptions = append(changeDescriptions, "changeEnforceSecurityGroupInboundRulesOnPrivateLinkTraffic", changeEnforceSecurityGroupInboundRulesOnPrivateLinkTrafficDesc) | ||
req.EnforceSecurityGroupInboundRulesOnPrivateLinkTraffic = &desiredEnforceSecurityGroupInboundRulesOnPrivateLinkTraffic | ||
} | ||
|
||
if _, err := m.elbv2Client.SetSecurityGroupsWithContext(ctx, req); err != nil { | ||
return err | ||
} | ||
m.logger.Info("modified loadBalancer securityGroups", | ||
"stackID", resLB.Stack().StackID(), | ||
"resourceID", resLB.ID(), | ||
"arn", awssdk.StringValue(sdkLB.LoadBalancer.LoadBalancerArn)) | ||
"arn", awssdk.StringValue(sdkLB.LoadBalancer.LoadBalancerArn), | ||
"changeSecurityGroups", changeDescriptions, | ||
) | ||
|
||
return nil | ||
} | ||
|
@@ -298,3 +317,25 @@ func buildResLoadBalancerStatus(sdkLB LoadBalancerWithTags) elbv2model.LoadBalan | |
DNSName: awssdk.StringValue(sdkLB.LoadBalancer.DNSName), | ||
} | ||
} | ||
|
||
func isEnforceSGInboundRulesOnPrivateLinkUpdated(resLB *elbv2model.LoadBalancer, sdkLB LoadBalancerWithTags) (bool, string, string) { | ||
|
||
if resLB.Spec.Type != elbv2model.LoadBalancerTypeNetwork || resLB.Spec.SecurityGroupsInboundRulesOnPrivateLink == nil { | ||
return false, "", "" | ||
} | ||
|
||
desiredEnforceSecurityGroupInboundRulesOnPrivateLinkTraffic := string(*resLB.Spec.SecurityGroupsInboundRulesOnPrivateLink) | ||
|
||
var currentEnforceSecurityGroupInboundRulesOnPrivateLinkTraffic string | ||
|
||
if sdkLB.LoadBalancer.EnforceSecurityGroupInboundRulesOnPrivateLinkTraffic != nil { | ||
currentEnforceSecurityGroupInboundRulesOnPrivateLinkTraffic = awssdk.StringValue(sdkLB.LoadBalancer.EnforceSecurityGroupInboundRulesOnPrivateLinkTraffic) | ||
} | ||
|
||
if desiredEnforceSecurityGroupInboundRulesOnPrivateLinkTraffic == currentEnforceSecurityGroupInboundRulesOnPrivateLinkTraffic { | ||
return false, "", "" | ||
} | ||
|
||
return true, currentEnforceSecurityGroupInboundRulesOnPrivateLinkTraffic, desiredEnforceSecurityGroupInboundRulesOnPrivateLinkTraffic | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package elbv2 | |
|
||
import ( | ||
"context" | ||
|
||
"github.com/pkg/errors" | ||
"sigs.k8s.io/aws-load-balancer-controller/pkg/model/core" | ||
) | ||
|
@@ -86,6 +87,13 @@ const ( | |
IPAddressTypeDualStack IPAddressType = "dualstack" | ||
) | ||
|
||
type SecurityGroupsInboundRulesOnPrivateLinkStatus string | ||
|
||
const ( | ||
SecurityGroupsInboundRulesOnPrivateLinkOn SecurityGroupsInboundRulesOnPrivateLinkStatus = "on" | ||
SecurityGroupsInboundRulesOnPrivateLinkOff SecurityGroupsInboundRulesOnPrivateLinkStatus = "off" | ||
) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we move these to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it is okay to move these to pkg/model/ec2/security_group.go. One concern is this field is actually a load balancer field, even though it is related to security groups |
||
type LoadBalancerScheme string | ||
|
||
const ( | ||
|
@@ -143,6 +151,10 @@ type LoadBalancerSpec struct { | |
// +optional | ||
SecurityGroups []core.StringToken `json:"securityGroups,omitempty"` | ||
|
||
// [Network Load Balancers] The status of the security groups inbound rules on private link. | ||
// +optional | ||
SecurityGroupsInboundRulesOnPrivateLink *SecurityGroupsInboundRulesOnPrivateLinkStatus `json:"securityGroupsInboundRulesOnPrivateLink,omitempty"` | ||
|
||
// [Application Load Balancers on Outposts] The ID of the customer-owned address pool (CoIP pool). | ||
// +optional | ||
CustomerOwnedIPv4Pool *string `json:"customerOwnedIPv4Pool,omitempty"` | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,6 +77,11 @@ func (t *defaultModelBuildTask) buildLoadBalancerSpec(ctx context.Context, schem | |
if err != nil { | ||
return elbv2model.LoadBalancerSpec{}, err | ||
} | ||
securityGroupsInboundRulesOnPrivateLink, err := t.buildSecurityGroupsInboundRulesOnPrivateLink(ctx) | ||
if err != nil { | ||
return elbv2model.LoadBalancerSpec{}, err | ||
} | ||
|
||
spec := elbv2model.LoadBalancerSpec{ | ||
Name: name, | ||
Type: elbv2model.LoadBalancerTypeNetwork, | ||
|
@@ -87,6 +92,11 @@ func (t *defaultModelBuildTask) buildLoadBalancerSpec(ctx context.Context, schem | |
LoadBalancerAttributes: lbAttributes, | ||
Tags: tags, | ||
} | ||
|
||
if securityGroupsInboundRulesOnPrivateLink != nil { | ||
spec.SecurityGroupsInboundRulesOnPrivateLink = securityGroupsInboundRulesOnPrivateLink | ||
} | ||
|
||
return spec, nil | ||
} | ||
|
||
|
@@ -177,6 +187,23 @@ func (t *defaultModelBuildTask) buildLoadBalancerIPAddressType(_ context.Context | |
} | ||
} | ||
|
||
func (t *defaultModelBuildTask) buildSecurityGroupsInboundRulesOnPrivateLink(_ context.Context) (*elbv2model.SecurityGroupsInboundRulesOnPrivateLinkStatus, error) { | ||
var rawSecurityGroupsInboundRulesOnPrivateLink string | ||
if exists := t.annotationParser.ParseStringAnnotation(annotations.SvcLBSuffixEnforceSGInboundRulesOnPrivateLinkTraffic, &rawSecurityGroupsInboundRulesOnPrivateLink, t.service.Annotations); !exists { | ||
return nil, nil | ||
} | ||
securityGroupsInboundRulesOnPrivateLink := elbv2model.SecurityGroupsInboundRulesOnPrivateLinkStatus(rawSecurityGroupsInboundRulesOnPrivateLink) | ||
|
||
switch securityGroupsInboundRulesOnPrivateLink { | ||
case elbv2model.SecurityGroupsInboundRulesOnPrivateLinkOn: | ||
return &securityGroupsInboundRulesOnPrivateLink, nil | ||
case elbv2model.SecurityGroupsInboundRulesOnPrivateLinkOff: | ||
return &securityGroupsInboundRulesOnPrivateLink, nil | ||
default: | ||
return nil, errors.Errorf("Invalid value for securityGroupsInboundRulesOnPrivateLink status: %v, value must be one of [%v, %v]", securityGroupsInboundRulesOnPrivateLink, string(elbv2model.SecurityGroupsInboundRulesOnPrivateLinkOn), string(elbv2model.SecurityGroupsInboundRulesOnPrivateLinkOff)) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it will be confusing that it says only accept "on" and "off", but it actually allows an empty string "". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It won't allow empty string "". An empty string will also fall into the condition of an invalid value. |
||
} | ||
|
||
func (t *defaultModelBuildTask) buildLoadBalancerScheme(ctx context.Context) (elbv2model.LoadBalancerScheme, error) { | ||
scheme, explicitSchemeSpecified, err := t.buildLoadBalancerSchemeViaAnnotation(ctx) | ||
if err != nil { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this be something like
or
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, I like
aws-load-balancer-enforce-sg-inbound-rules-on-private-link-traffic
but annotation must be 63 characters or less, so we decided to useaws-load-balancer-inbound-sg-rules-on-private-link-traffic