Skip to content

Commit 39c571f

Browse files
authored
Merge pull request #1286 from M00nF1sh/fix_1274
bug fix for #1274
2 parents 6cd8324 + 50c565d commit 39c571f

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

internal/alb/ls/rules.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func (c *rulesController) getDesiredRules(ctx context.Context, listener *elbv2.L
154154
}
155155
if createsRedirectLoop(listener, elbRule) {
156156
continue
157-
} else if isUnconditionalRedirect(listener, elbRule) {
157+
} else if isUnconditionalRedirect(listener, elbRule, ingressRule.Host) {
158158
seenUnconditionalRedirect = true
159159
}
160160
output = append(output, elbRule)
@@ -543,25 +543,34 @@ func createsRedirectLoop(listener *elbv2.Listener, r elbv2.Rule) bool {
543543
// isUnconditionalRedirect checks whether specified rule always redirects
544544
// We consider the rule is a unconditional redirect if
545545
// 1) The Path condition is nil, or at least one Path condition is /*
546-
// 2) All other rule conditions are nil (ignoring the Host condition).
547-
// 3) RedirectConfig is not nil.
548-
func isUnconditionalRedirect(listener *elbv2.Listener, r elbv2.Rule) bool {
546+
// 2) The Host condition don't contain any other element than host passed-in
547+
// 3) All other rule conditions are nil.
548+
// 4) RedirectConfig is not nil.
549+
func isUnconditionalRedirect(listener *elbv2.Listener, r elbv2.Rule, ruleHost string) bool {
549550
for _, action := range r.Actions {
550551
rc := action.RedirectConfig
551552
if rc == nil {
552553
continue
553554
}
554555

556+
var hosts []string
555557
var paths []string
556558
for _, c := range r.Conditions {
557559
switch aws.StringValue(c.Field) {
558560
case conditions.FieldPathPattern:
559561
paths = append(paths, aws.StringValueSlice(c.PathPatternConfig.Values)...)
562+
case conditions.FieldHostHeader:
563+
hosts = append(hosts, aws.StringValueSlice(c.HostHeaderConfig.Values)...)
560564
case conditions.FieldHTTPRequestMethod, conditions.FieldSourceIP, conditions.FieldHTTPHeader, conditions.FieldQueryString:
561565
// If there are any conditions, then the redirect is not unconditional
562566
return false
563567
}
564568
}
569+
for _, host := range hosts {
570+
if host != ruleHost {
571+
return false
572+
}
573+
}
565574

566575
if len(paths) != 0 {
567576
// ALB path conditions are ORed, so if any of them are a wildcard, the redirect is unconditional
@@ -573,7 +582,6 @@ func isUnconditionalRedirect(listener *elbv2.Listener, r elbv2.Rule) bool {
573582
// The redirect isn't unconditional if none of the path conditions are a wildcard
574583
return false
575584
}
576-
577585
return true
578586
}
579587
return false

internal/alb/ls/rules_test.go

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2883,6 +2883,7 @@ func Test_isUnconditionalRedirect(t *testing.T) {
28832883
name string
28842884
listener elbv2.Listener
28852885
rule elbv2.Rule
2886+
ruleHost string
28862887

28872888
expected bool
28882889
}{
@@ -2920,11 +2921,12 @@ func Test_isUnconditionalRedirect(t *testing.T) {
29202921
{
29212922
Field: aws.String(conditions.FieldHostHeader),
29222923
HostHeaderConfig: &elbv2.HostHeaderConditionConfig{
2923-
Values: aws.StringSlice([]string{"www.example.com", "anno.example.com"}),
2924+
Values: aws.StringSlice([]string{"www.example.com"}),
29242925
},
29252926
},
29262927
},
29272928
},
2929+
ruleHost: "www.example.com",
29282930
expected: true,
29292931
},
29302932
{
@@ -3012,7 +3014,7 @@ func Test_isUnconditionalRedirect(t *testing.T) {
30123014
expected: false,
30133015
},
30143016
{
3015-
name: "Path condition set to /* and Host condition is set ",
3017+
name: "Path condition set to /* and Host condition is set to same as ruleHost",
30163018
listener: elbv2.Listener{Protocol: aws.String("HTTP"), Port: aws.Int64(80)},
30173019
rule: elbv2.Rule{
30183020
Actions: []*elbv2.Action{
@@ -3031,13 +3033,42 @@ func Test_isUnconditionalRedirect(t *testing.T) {
30313033
{
30323034
Field: aws.String(conditions.FieldHostHeader),
30333035
HostHeaderConfig: &elbv2.HostHeaderConditionConfig{
3034-
Values: aws.StringSlice([]string{"www.example.com", "anno.example.com"}),
3036+
Values: aws.StringSlice([]string{"www.example.com"}),
30353037
},
30363038
},
30373039
},
30383040
},
3041+
ruleHost: "www.example.com",
30393042
expected: true,
30403043
},
3044+
{
3045+
name: "Path condition set to /* and Host condition is set to more than ruleHost",
3046+
listener: elbv2.Listener{Protocol: aws.String("HTTP"), Port: aws.Int64(80)},
3047+
rule: elbv2.Rule{
3048+
Actions: []*elbv2.Action{
3049+
{
3050+
Type: aws.String(elbv2.ActionTypeEnumRedirect),
3051+
RedirectConfig: redirectActionConfig(&elbv2.RedirectActionConfig{Path: aws.String("/#{path}")}),
3052+
},
3053+
},
3054+
Conditions: []*elbv2.RuleCondition{
3055+
{
3056+
Field: aws.String(conditions.FieldPathPattern),
3057+
PathPatternConfig: &elbv2.PathPatternConditionConfig{
3058+
Values: aws.StringSlice([]string{"/*"}),
3059+
},
3060+
},
3061+
{
3062+
Field: aws.String(conditions.FieldHostHeader),
3063+
HostHeaderConfig: &elbv2.HostHeaderConditionConfig{
3064+
Values: aws.StringSlice([]string{"www.example.com", "annos.example.com"}),
3065+
},
3066+
},
3067+
},
3068+
},
3069+
ruleHost: "www.example.com",
3070+
expected: false,
3071+
},
30413072
{
30423073
name: "Path condition set to /* but a SourceIP condition is also set",
30433074
listener: elbv2.Listener{Protocol: aws.String("HTTP"), Port: aws.Int64(80)},
@@ -3067,7 +3098,7 @@ func Test_isUnconditionalRedirect(t *testing.T) {
30673098
},
30683099
} {
30693100
t.Run(tc.name, func(t *testing.T) {
3070-
assert.Equal(t, tc.expected, isUnconditionalRedirect(&tc.listener, tc.rule))
3101+
assert.Equal(t, tc.expected, isUnconditionalRedirect(&tc.listener, tc.rule, tc.ruleHost))
30713102
})
30723103
}
30733104
}

0 commit comments

Comments
 (0)