-
Notifications
You must be signed in to change notification settings - Fork 41
/
trust_domain.go
176 lines (146 loc) · 6.45 KB
/
trust_domain.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright 2021 Red Hat, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package authorizaton
import (
"context"
"fmt"
"strings"
"testing"
"time"
"github.com/maistra/maistra-test-tool/pkg/examples"
"github.com/maistra/maistra-test-tool/pkg/util"
)
func cleanupTrustDomainMigration() {
util.Log.Info("Cleanup")
util.KubeDeleteContents("foo", TrustDomainPolicy)
sleep := examples.Sleep{Namespace: "foo"}
httpbin := examples.Httpbin{Namespace: "foo"}
sleep.Uninstall()
httpbin.Uninstall()
sleep = examples.Sleep{Namespace: "bar"}
sleep.Uninstall()
applyTrustDomain("cluster.local", "", false)
}
func TestTrustDomainMigration(t *testing.T) {
defer cleanupTrustDomainMigration()
defer util.RecoverPanic(t)
util.Log.Info("Trust Domain Migration")
applyTrustDomain("old-td", "", true)
// Deploy workloads
httpbin := examples.Httpbin{Namespace: "foo"}
util.Inspect(httpbin.Install(), "Failed to deploy httpbin", "", t)
sleep := examples.Sleep{Namespace: "foo"}
util.Inspect(sleep.Install(), "Failed to deploy sleep", "", t)
sleep = examples.Sleep{Namespace: "bar"}
util.Inspect(sleep.Install(), "Failed to deploy sleep", "", t)
util.Log.Info("Apply deny all policy except sleep in bar namespace")
util.KubeApplyContents("foo", TrustDomainPolicy)
t.Run("Case 1: Verifying policy works", func(t *testing.T) {
sleepPod, err := util.GetPodName("foo", "app=sleep")
util.Inspect(err, "Failed to get sleep pod name", "", t)
cmd := fmt.Sprintf(`curl http://httpbin.%s:8000/ip -sS -o /dev/null -w "%%{http_code}\n"`, "foo")
if err := checkOutput("foo", sleepPod, "sleep", cmd, "403"); err != nil {
t.Fatal(err)
}
sleepPod, err = util.GetPodName("bar", "app=sleep")
util.Inspect(err, "Failed to get sleep pod name", "", t)
cmd = fmt.Sprintf(`curl http://httpbin.%s:8000/ip -sS -o /dev/null -w "%%{http_code}\n"`, "foo")
if err := checkOutput("bar", sleepPod, "sleep", cmd, "200"); err != nil {
t.Fatal(err)
}
})
t.Run("Case 2: Migrate trust domain without trust domain aliases", func(t *testing.T) {
applyTrustDomain("new-td", "", true)
// Restart workload pods
util.Shell("oc -n foo delete pod --all")
util.Shell("oc -n bar delete pod --all")
util.Shell("oc -n foo wait --for condition=Ready --all pods --timeout 30s")
util.Shell("oc -n bar wait --for condition=Ready --all pods --timeout 30s")
// Both must return 403
sleepPod, err := util.GetPodName("foo", "app=sleep")
util.Inspect(err, "Failed to get sleep pod name", "", t)
cmd := fmt.Sprintf(`curl http://httpbin.%s:8000/ip -sS -o /dev/null -w "%%{http_code}\n"`, "foo")
if err := checkOutput("foo", sleepPod, "sleep", cmd, "403"); err != nil {
t.Fatal(err)
}
sleepPod, err = util.GetPodName("bar", "app=sleep")
util.Inspect(err, "Failed to get sleep pod name", "", t)
cmd = fmt.Sprintf(`curl http://httpbin.%s:8000/ip -sS -o /dev/null -w "%%{http_code}\n"`, "foo")
if err := checkOutput("bar", sleepPod, "sleep", cmd, "403"); err != nil {
t.Fatal(err)
}
})
t.Run("Case 3: Migrate trust domain with trust domain aliases", func(t *testing.T) {
applyTrustDomain("new-td", "old-td", true)
// Restart workload pods
util.Shell("oc -n foo delete pod --all")
util.Shell("oc -n bar delete pod --all")
util.Shell("oc -n foo wait --for condition=Ready --all pods --timeout 30s")
util.Shell("oc -n bar wait --for condition=Ready --all pods --timeout 30s")
sleepPod, err := util.GetPodName("foo", "app=sleep")
util.Inspect(err, "Failed to get sleep pod name", "", t)
cmd := fmt.Sprintf(`curl http://httpbin.%s:8000/ip -sS -o /dev/null -w "%%{http_code}\n"`, "foo")
if err := checkOutput("foo", sleepPod, "sleep", cmd, "403"); err != nil {
t.Fatal(err)
}
// This must return 200, as in the first case
sleepPod, err = util.GetPodName("bar", "app=sleep")
util.Inspect(err, "Failed to get sleep pod name", "", t)
cmd = fmt.Sprintf(`curl http://httpbin.%s:8000/ip -sS -o /dev/null -w "%%{http_code}\n"`, "foo")
if err := checkOutput("bar", sleepPod, "sleep", cmd, "200"); err != nil {
t.Fatal(err)
}
})
}
func checkOutput(namespace, pod, container, cmd, expected string) error {
retry := util.Retrier{
BaseDelay: 5 * time.Second,
MaxDelay: 10 * time.Second,
Retries: 5,
}
util.Log.Infof("Verifying curl output, expecting %s", expected)
retryFn := func(_ context.Context, i int) error {
msg, err := util.PodExec(namespace, pod, container, cmd, true)
if err != nil {
return err
}
if !strings.Contains(msg, expected) {
util.Log.Errorf("Attempt %d/%d - expected: %v; Got: %v", i, retry.Retries, expected, msg)
return fmt.Errorf("expected: %v; Got: %v", expected, msg)
}
util.Log.Infof("Attempt %d/%d - Success, got %s", i, retry.Retries, msg)
return nil
}
ctx := context.Background()
_, err := retry.Retry(ctx, retryFn)
return err
}
func applyTrustDomain(domain, alias string, mtls bool) {
util.Log.Infof("Configuring spec.security.trust.domain to %q and alias %q", domain, alias)
if alias != "" {
alias = fmt.Sprintf("%q", alias)
}
util.Shell(`oc -n %s patch smcp/%s --type merge -p '{"spec":{"security":{"dataPlane":{"mtls":%v}, "trust":{"domain":"%s", "additionalDomains": [%s]}}}}'`, meshNamespace, smcpName, mtls, domain, alias)
// Wait for the operator to reconcile the changes
util.Shell(`oc -n %s wait --for condition=Ready smcp/%s --timeout 180s`, meshNamespace, smcpName)
// Restart istiod so it picks up the new trust domain
util.Shell(`oc -n %s rollout restart deployment istiod-%s`, meshNamespace, smcpName)
// wait 20 seconds and avoid a race condition checking wrong ingressgateway and istiod-basic pods
time.Sleep(time.Duration(20) * time.Second)
util.Shell(`oc -n %s wait --for condition=Ready --all pods --timeout 180s`, meshNamespace)
// Restart ingress gateway since we changed the mtls setting
util.Shell(`oc -n %s rollout restart deployment istio-ingressgateway`, meshNamespace)
time.Sleep(time.Duration(20) * time.Second)
util.Shell(`oc -n %s wait --for condition=Ready --all pods --timeout 180s`, meshNamespace)
}