Skip to content

Commit f6aa6fd

Browse files
gargipanatulayue9944882
authored andcommitted
remove nil ptr dereference
1 parent 35afa98 commit f6aa6fd

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed

pkg/providers/v1/aws_fakes.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/aws/aws-sdk-go-v2/service/ec2"
3333
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
3434
elb "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing"
35+
elbtypes "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing/types"
3536
elbv2 "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2"
3637
"github.com/aws/aws-sdk-go-v2/service/kms"
3738
"k8s.io/klog/v2"
@@ -592,8 +593,8 @@ func (elb *FakeELB) ConfigureHealthCheck(ctx context.Context, input *elb.Configu
592593

593594
// CreateLoadBalancerPolicy is not implemented but is required for interface
594595
// conformance
595-
func (elb *FakeELB) CreateLoadBalancerPolicy(ctx context.Context, input *elb.CreateLoadBalancerPolicyInput, opts ...func(*elb.Options)) (*elb.CreateLoadBalancerPolicyOutput, error) {
596-
panic("Not implemented")
596+
func (e *FakeELB) CreateLoadBalancerPolicy(ctx context.Context, input *elb.CreateLoadBalancerPolicyInput, opts ...func(*elb.Options)) (*elb.CreateLoadBalancerPolicyOutput, error) {
597+
return &elb.CreateLoadBalancerPolicyOutput{}, nil
597598
}
598599

599600
// SetLoadBalancerPoliciesForBackendServer is not implemented but is required
@@ -610,8 +611,14 @@ func (elb *FakeELB) SetLoadBalancerPoliciesOfListener(ctx context.Context, input
610611

611612
// DescribeLoadBalancerPolicies is not implemented but is required for
612613
// interface conformance
613-
func (elb *FakeELB) DescribeLoadBalancerPolicies(ctx context.Context, input *elb.DescribeLoadBalancerPoliciesInput, opts ...func(*elb.Options)) (*elb.DescribeLoadBalancerPoliciesOutput, error) {
614-
panic("Not implemented")
614+
func (e *FakeELB) DescribeLoadBalancerPolicies(ctx context.Context, input *elb.DescribeLoadBalancerPoliciesInput, opts ...func(*elb.Options)) (*elb.DescribeLoadBalancerPoliciesOutput, error) {
615+
if aws.ToString(input.LoadBalancerName) == "" {
616+
return nil, &elbtypes.LoadBalancerAttributeNotFoundException{}
617+
}
618+
if len(input.PolicyNames) == 0 || input.PolicyNames[0] == "k8s-SSLNegotiationPolicy-" {
619+
return nil, &elbtypes.PolicyNotFoundException{}
620+
}
621+
return &elb.DescribeLoadBalancerPoliciesOutput{}, nil
615622
}
616623

617624
// DescribeLoadBalancerAttributes is not implemented but is required for

pkg/providers/v1/aws_loadbalancer.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,9 @@ func (c *Cloud) updateInstanceSecurityGroupsForNLB(ctx context.Context, lbName s
816816
if err != nil {
817817
return fmt.Errorf("error finding instance group: %q", err)
818818
}
819+
if sg == nil {
820+
return fmt.Errorf("error finding security group: %s", sgID)
821+
}
819822
clusterSGs[sgID] = sg
820823
}
821824
}
@@ -1505,13 +1508,16 @@ func (c *Cloud) ensureSSLNegotiationPolicy(ctx context.Context, loadBalancer *el
15051508
},
15061509
})
15071510
if err != nil {
1511+
// If DescribeLoadBalancerPolicies returns a PolicyNotFoundException, we must proceed and create the policy.
15081512
var notFoundErr *elbtypes.PolicyNotFoundException
15091513
if !errors.As(err, &notFoundErr) {
15101514
return fmt.Errorf("error describing security policies on load balancer: %q", err)
15111515
}
15121516
}
15131517

1514-
if len(result.PolicyDescriptions) > 0 {
1518+
// If DescribeLoadBalancerPolicies yielded a PolicyNotFoundException, result will be nil,
1519+
// so we must check before dereferencing
1520+
if result != nil && len(result.PolicyDescriptions) > 0 {
15151521
return nil
15161522
}
15171523

pkg/providers/v1/aws_loadbalancer_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,3 +1071,50 @@ func TestCloud_computeTargetGroupExpectedTargets(t *testing.T) {
10711071
})
10721072
}
10731073
}
1074+
1075+
// Make sure that errors returned by DescribeLoadBalancerPolicies are
1076+
// handled gracefully, and don't progress further into the function
1077+
func TestEnsureSSLNegotiationPolicyErrorHandling(t *testing.T) {
1078+
awsServices := NewFakeAWSServices(TestClusterID)
1079+
c, err := newAWSCloud(config.CloudConfig{}, awsServices)
1080+
if err != nil {
1081+
t.Errorf("Error building aws cloud: %v", err)
1082+
return
1083+
}
1084+
1085+
tests := []struct {
1086+
name string
1087+
loadBalancer *elbtypes.LoadBalancerDescription
1088+
policyName string
1089+
expectError bool
1090+
}{
1091+
{
1092+
name: "Expect LoadBalancerAttributeNotFoundException, error",
1093+
loadBalancer: &elbtypes.LoadBalancerDescription{
1094+
LoadBalancerName: aws.String(""),
1095+
},
1096+
policyName: "",
1097+
expectError: true,
1098+
},
1099+
{
1100+
name: "Expect PolicyNotFoundException, nil error",
1101+
loadBalancer: &elbtypes.LoadBalancerDescription{
1102+
LoadBalancerName: aws.String("test-lb"),
1103+
},
1104+
policyName: "",
1105+
expectError: false,
1106+
},
1107+
}
1108+
1109+
for _, test := range tests {
1110+
t.Run(test.name, func(t *testing.T) {
1111+
err := c.ensureSSLNegotiationPolicy(context.TODO(), test.loadBalancer, test.policyName)
1112+
if test.expectError && err == nil {
1113+
t.Errorf("Expected error but got none")
1114+
}
1115+
if !test.expectError && err != nil {
1116+
t.Errorf("Expected no error but got: %v", err)
1117+
}
1118+
})
1119+
}
1120+
}

0 commit comments

Comments
 (0)