|
| 1 | +package lb |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "k8s.io/apimachinery/pkg/util/intstr" |
| 8 | + |
| 9 | + "github.com/aws/aws-sdk-go/service/wafv2" |
| 10 | + "github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/aws" |
| 11 | + "github.com/kubernetes-sigs/aws-alb-ingress-controller/mocks" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + apiv1 "k8s.io/api/core/v1" |
| 14 | + extensions "k8s.io/api/extensions/v1beta1" |
| 15 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 16 | +) |
| 17 | + |
| 18 | +func buildWAFV2TestIngress(wafIngressAnnotations map[string]string) *extensions.Ingress { |
| 19 | + defaultBackend := extensions.IngressBackend{ |
| 20 | + ServiceName: "default-backend", |
| 21 | + ServicePort: intstr.FromInt(80), |
| 22 | + } |
| 23 | + |
| 24 | + return &extensions.Ingress{ |
| 25 | + ObjectMeta: metav1.ObjectMeta{ |
| 26 | + Name: "foo", |
| 27 | + Namespace: apiv1.NamespaceDefault, |
| 28 | + Annotations: wafIngressAnnotations, |
| 29 | + }, |
| 30 | + Spec: extensions.IngressSpec{ |
| 31 | + Backend: &extensions.IngressBackend{ |
| 32 | + ServiceName: "default-backend", |
| 33 | + ServicePort: intstr.FromInt(80), |
| 34 | + }, |
| 35 | + Rules: []extensions.IngressRule{ |
| 36 | + { |
| 37 | + Host: "foo.bar.com", |
| 38 | + IngressRuleValue: extensions.IngressRuleValue{ |
| 39 | + HTTP: &extensions.HTTPIngressRuleValue{ |
| 40 | + Paths: []extensions.HTTPIngressPath{ |
| 41 | + { |
| 42 | + Path: "/foo", |
| 43 | + Backend: defaultBackend, |
| 44 | + }, |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func Test_defaultWAFV2Controller_Reconcile(t *testing.T) { |
| 55 | + for _, tc := range []struct { |
| 56 | + Name string |
| 57 | + GetWAFV2WebACLSummaryResponse *wafv2.WebACL |
| 58 | + GetWAFV2WebACLSummaryError error |
| 59 | + AssociateWAFV2Response *wafv2.AssociateWebACLOutput |
| 60 | + AssociateWAFV2Error error |
| 61 | + DisassociateWAFV2Response *wafv2.DisassociateWebACLOutput |
| 62 | + DisassociateWAFV2Error error |
| 63 | + Expected error |
| 64 | + ExpectedError error |
| 65 | + LoadBalancerARN string |
| 66 | + IngressAnnotations *extensions.Ingress |
| 67 | + DesiredWebACLARN string |
| 68 | + GetWAFV2WebACLSummaryTimesCalled int |
| 69 | + AssociateWAFV2TimesCalled int |
| 70 | + DisassociateWAFV2TimesCalled int |
| 71 | + }{ |
| 72 | + { |
| 73 | + Name: "No annotation, confirm nothing is attached", |
| 74 | + GetWAFV2WebACLSummaryResponse: &wafv2.WebACL{ |
| 75 | + ARN: nil, |
| 76 | + }, |
| 77 | + GetWAFV2WebACLSummaryError: nil, |
| 78 | + AssociateWAFV2Response: &wafv2.AssociateWebACLOutput{}, |
| 79 | + AssociateWAFV2Error: nil, |
| 80 | + Expected: nil, |
| 81 | + LoadBalancerARN: "arn:lb", |
| 82 | + IngressAnnotations: buildWAFV2TestIngress( |
| 83 | + map[string]string{}, |
| 84 | + ), |
| 85 | + DesiredWebACLARN: "", |
| 86 | + GetWAFV2WebACLSummaryTimesCalled: 1, |
| 87 | + AssociateWAFV2TimesCalled: 0, |
| 88 | + DisassociateWAFV2TimesCalled: 0, |
| 89 | + }, |
| 90 | + { |
| 91 | + Name: "Empty WAFv2 annotation", |
| 92 | + GetWAFV2WebACLSummaryResponse: &wafv2.WebACL{ |
| 93 | + ARN: nil, |
| 94 | + }, |
| 95 | + GetWAFV2WebACLSummaryError: nil, |
| 96 | + AssociateWAFV2Response: &wafv2.AssociateWebACLOutput{}, |
| 97 | + AssociateWAFV2Error: nil, |
| 98 | + Expected: nil, |
| 99 | + LoadBalancerARN: "arn:lb", |
| 100 | + IngressAnnotations: buildWAFV2TestIngress( |
| 101 | + map[string]string{ |
| 102 | + "alb.ingress.kubernetes.io/wafv2-acl-arn": "", |
| 103 | + }, |
| 104 | + ), |
| 105 | + DesiredWebACLARN: "", |
| 106 | + GetWAFV2WebACLSummaryTimesCalled: 1, |
| 107 | + AssociateWAFV2TimesCalled: 0, |
| 108 | + DisassociateWAFV2TimesCalled: 0, |
| 109 | + }, |
| 110 | + { |
| 111 | + Name: "No annotation, detach WAFv2", |
| 112 | + GetWAFV2WebACLSummaryResponse: &wafv2.WebACL{ |
| 113 | + ARN: aws.String("arn:aws:wafv2:us-east-1:000000000000:regional/webacl/name/0aa00000-00a0-00a0-a0a0-0a0000a0000a"), |
| 114 | + }, |
| 115 | + GetWAFV2WebACLSummaryError: nil, |
| 116 | + AssociateWAFV2Response: &wafv2.AssociateWebACLOutput{}, |
| 117 | + AssociateWAFV2Error: nil, |
| 118 | + Expected: nil, |
| 119 | + LoadBalancerARN: "arn:lb", |
| 120 | + IngressAnnotations: buildWAFV2TestIngress( |
| 121 | + map[string]string{}, |
| 122 | + ), |
| 123 | + DesiredWebACLARN: "", |
| 124 | + GetWAFV2WebACLSummaryTimesCalled: 1, |
| 125 | + AssociateWAFV2TimesCalled: 0, |
| 126 | + DisassociateWAFV2TimesCalled: 1, |
| 127 | + }, |
| 128 | + { |
| 129 | + Name: "Empty annotation, dissassociate WAFv2", |
| 130 | + GetWAFV2WebACLSummaryResponse: &wafv2.WebACL{ |
| 131 | + ARN: aws.String("arn:aws:wafv2:us-east-1:000000000000:regional/webacl/name/0aa00000-00a0-00a0-a0a0-0a0000a0000a"), |
| 132 | + }, |
| 133 | + GetWAFV2WebACLSummaryError: nil, |
| 134 | + AssociateWAFV2Response: &wafv2.AssociateWebACLOutput{}, |
| 135 | + AssociateWAFV2Error: nil, |
| 136 | + Expected: nil, |
| 137 | + LoadBalancerARN: "arn:lb", |
| 138 | + IngressAnnotations: buildWAFV2TestIngress( |
| 139 | + map[string]string{ |
| 140 | + "alb.ingress.kubernetes.io/wafv2-acl-arn": "", |
| 141 | + }, |
| 142 | + ), |
| 143 | + DesiredWebACLARN: "", |
| 144 | + GetWAFV2WebACLSummaryTimesCalled: 1, |
| 145 | + AssociateWAFV2TimesCalled: 0, |
| 146 | + DisassociateWAFV2TimesCalled: 1, |
| 147 | + }, |
| 148 | + { |
| 149 | + Name: "Annotation, associate WAFv2", |
| 150 | + GetWAFV2WebACLSummaryResponse: &wafv2.WebACL{ |
| 151 | + ARN: nil, |
| 152 | + }, |
| 153 | + GetWAFV2WebACLSummaryError: nil, |
| 154 | + AssociateWAFV2Response: &wafv2.AssociateWebACLOutput{}, |
| 155 | + AssociateWAFV2Error: nil, |
| 156 | + Expected: nil, |
| 157 | + LoadBalancerARN: "arn:lb", |
| 158 | + IngressAnnotations: buildWAFV2TestIngress( |
| 159 | + map[string]string{ |
| 160 | + "alb.ingress.kubernetes.io/wafv2-acl-arn": "arn:aws:wafv2:us-east-1:000000000000:regional/webacl/name/0aa00000-00a0-00a0-a0a0-0a0000a0000a", |
| 161 | + }, |
| 162 | + ), |
| 163 | + DesiredWebACLARN: "arn:aws:wafv2:us-east-1:000000000000:regional/webacl/name/0aa00000-00a0-00a0-a0a0-0a0000a0000a", |
| 164 | + GetWAFV2WebACLSummaryTimesCalled: 1, |
| 165 | + AssociateWAFV2TimesCalled: 1, |
| 166 | + DisassociateWAFV2TimesCalled: 0, |
| 167 | + }, |
| 168 | + { |
| 169 | + Name: "Annotation, change WAFv2", |
| 170 | + GetWAFV2WebACLSummaryResponse: &wafv2.WebACL{ |
| 171 | + ARN: aws.String("arn:aws:wafv2:us-east-1:000000000000:regional/webacl/name/0bb00000-00b0-00b0-b0b0-0b0000a0000b"), |
| 172 | + }, |
| 173 | + GetWAFV2WebACLSummaryError: nil, |
| 174 | + Expected: nil, |
| 175 | + AssociateWAFV2Response: &wafv2.AssociateWebACLOutput{}, |
| 176 | + AssociateWAFV2Error: nil, |
| 177 | + LoadBalancerARN: "arn:lb", |
| 178 | + IngressAnnotations: buildWAFV2TestIngress( |
| 179 | + map[string]string{ |
| 180 | + "alb.ingress.kubernetes.io/wafv2-acl-arn": "arn:aws:wafv2:us-east-1:000000000000:regional/webacl/name/0aa00000-00a0-00a0-a0a0-0a0000a0000a", |
| 181 | + }, |
| 182 | + ), |
| 183 | + DesiredWebACLARN: "arn:aws:wafv2:us-east-1:000000000000:regional/webacl/name/0aa00000-00a0-00a0-a0a0-0a0000a0000a", |
| 184 | + GetWAFV2WebACLSummaryTimesCalled: 1, |
| 185 | + AssociateWAFV2TimesCalled: 1, |
| 186 | + DisassociateWAFV2TimesCalled: 0, |
| 187 | + }, |
| 188 | + } { |
| 189 | + t.Run(tc.Name, func(t *testing.T) { |
| 190 | + ctx := context.Background() |
| 191 | + cloud := &mocks.CloudAPI{} |
| 192 | + |
| 193 | + cloud.On("GetWAFV2WebACLSummary", ctx, aws.String(tc.LoadBalancerARN)).Return(tc.GetWAFV2WebACLSummaryResponse, tc.GetWAFV2WebACLSummaryError) |
| 194 | + cloud.On("AssociateWAFV2", ctx, aws.String(tc.LoadBalancerARN), aws.String(tc.DesiredWebACLARN)).Return(tc.AssociateWAFV2Response, tc.AssociateWAFV2Error) |
| 195 | + cloud.On("DisassociateWAFV2", ctx, aws.String(tc.LoadBalancerARN)).Return(tc.DisassociateWAFV2Response, tc.DisassociateWAFV2Error) |
| 196 | + |
| 197 | + controller := NewWAFV2Controller(cloud) |
| 198 | + err := controller.Reconcile(ctx, tc.LoadBalancerARN, tc.IngressAnnotations) |
| 199 | + assert.Equal(t, tc.Expected, err) |
| 200 | + cloud.AssertNumberOfCalls(t, "GetWAFV2WebACLSummary", tc.GetWAFV2WebACLSummaryTimesCalled) |
| 201 | + cloud.AssertNumberOfCalls(t, "AssociateWAFV2", tc.AssociateWAFV2TimesCalled) |
| 202 | + cloud.AssertNumberOfCalls(t, "DisassociateWAFV2", tc.DisassociateWAFV2TimesCalled) |
| 203 | + }) |
| 204 | + } |
| 205 | +} |
0 commit comments