forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add e2e test for cacert rotation (istio#47641)
* add e2e test for cacert rotation * lint & gen * add log * more fix * unmarshal * nit * address comment * update * wait a little longer
- Loading branch information
Showing
5 changed files
with
258 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIID7TCCAtWgAwIBAgIJAOIRDhOcxsx6MA0GCSqGSIb3DQEBCwUAMIGLMQswCQYD | ||
VQQGEwJVUzETMBEGA1UECAwKQ2FsaWZvcm5pYTESMBAGA1UEBwwJU3Vubnl2YWxl | ||
MQ4wDAYDVQQKDAVJc3RpbzENMAsGA1UECwwEVGVzdDEQMA4GA1UEAwwHUm9vdCBD | ||
QTEiMCAGCSqGSIb3DQEJARYTdGVzdHJvb3RjYUBpc3Rpby5pbzAgFw0xODAxMjQx | ||
OTE1NTFaGA8yMTE3MTIzMTE5MTU1MVowgYsxCzAJBgNVBAYTAlVTMRMwEQYDVQQI | ||
DApDYWxpZm9ybmlhMRIwEAYDVQQHDAlTdW5ueXZhbGUxDjAMBgNVBAoMBUlzdGlv | ||
MQ0wCwYDVQQLDARUZXN0MRAwDgYDVQQDDAdSb290IENBMSIwIAYJKoZIhvcNAQkB | ||
FhN0ZXN0cm9vdGNhQGlzdGlvLmlvMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB | ||
CgKCAQEA38uEfAat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CFDgjuSfzlrKukcRj7yd2rzs+MQP3amOuWIj5gq7lUWAPNMp0bhU1lIS2eAk7 | ||
Ew8s4BkgMI2fW8z22uEw49j+720n9q3xIpmfGA7SISe32Z3bu10fXrsIii++Zo9p | ||
ItX7B5pKaLCwANjUejzC3NZ5HdL+yV9dXWXrVZrTvdFxRiqhNJtV64YTsSH+kVp8 | ||
cCYyNA90CBVQU8ZyomNnOxqOsrBL1NvZllBmX1f3SnVa2Kw+RxJLtEx9lmKT3aiX | ||
v0kPPhoU+qpA3eOVvbT0CbSBEl56wclPMd7xYMykkNU/AVc3lZ7YFcjb19YNxzff | ||
AKktnJrFx3FHf03aeAnvJ35FxitrcpV4NvLvq5ViCHVw6IMO5h274Z/HoGHVvcle | ||
OPtLRiI5Fkaexa6Y/+SPMDFsCfQJzL4ZWmnGSA+Z6YMD5atviQGEbSP4bCwIgOax | ||
dsSyRo1cTRaOVCGVxed/s+ChbRnvljPd35zl79o+1zOOxJ2ttswYJRkjSsvn+BCL | ||
GZj8c/5MkLtTwvL62wZVhCIHtZoS4LNUqdkyYvRJMZTV72YuEPTfl8NKhmMislqR | ||
5LPKUi9adm4= | ||
-----END CERTIFICATE----- |
192 changes: 192 additions & 0 deletions
192
tests/integration/security/cacert_rotation/main_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
//go:build integ | ||
// +build integ | ||
|
||
// Copyright Istio Authors | ||
// | ||
// 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 cacertrotation | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
admin "github.com/envoyproxy/go-control-plane/envoy/admin/v3" | ||
|
||
"istio.io/istio/pkg/security" | ||
"istio.io/istio/pkg/test/framework" | ||
"istio.io/istio/pkg/test/framework/components/echo" | ||
"istio.io/istio/pkg/test/framework/components/echo/check" | ||
"istio.io/istio/pkg/test/framework/components/echo/common/deployment" | ||
"istio.io/istio/pkg/test/framework/components/echo/echotest" | ||
"istio.io/istio/pkg/test/framework/components/echo/match" | ||
"istio.io/istio/pkg/test/framework/components/istio" | ||
"istio.io/istio/pkg/test/framework/components/istioctl" | ||
"istio.io/istio/pkg/test/framework/components/namespace" | ||
"istio.io/istio/pkg/test/framework/label" | ||
"istio.io/istio/pkg/test/framework/resource" | ||
"istio.io/istio/pkg/test/util/retry" | ||
"istio.io/istio/pkg/util/protomarshal" | ||
"istio.io/istio/tests/integration/security/util/cert" | ||
"istio.io/istio/tests/integration/security/util/reachability" | ||
) | ||
|
||
var apps deployment.SingleNamespaceView | ||
|
||
func TestMain(m *testing.M) { | ||
framework. | ||
NewSuite(m). | ||
Label(label.CustomSetup). | ||
Setup(istio.Setup(nil, setupConfig, cert.CreateCASecret)). | ||
Setup(deployment.SetupSingleNamespace(&apps, deployment.Config{})). | ||
Setup(func(ctx resource.Context) error { | ||
return reachability.CreateCustomInstances(&apps) | ||
}). | ||
Run() | ||
} | ||
|
||
func setupConfig(_ resource.Context, cfg *istio.Config) { | ||
if cfg == nil { | ||
return | ||
} | ||
cfgYaml := ` | ||
values: | ||
pilot: | ||
env: | ||
ISTIO_MULTIROOT_MESH: true | ||
meshConfig: | ||
defaultConfig: | ||
proxyMetadata: | ||
PROXY_CONFIG_XDS_AGENT: "true" | ||
` | ||
cfg.ControlPlaneValues = cfgYaml | ||
} | ||
|
||
func TestReachability(t *testing.T) { | ||
framework.NewTest(t). | ||
Features("security.peer.cacert-rotation"). | ||
Run(func(t framework.TestContext) { | ||
istioCfg := istio.DefaultConfigOrFail(t, t) | ||
istioCtl := istioctl.NewOrFail(t, t, istioctl.Config{}) | ||
namespace.ClaimOrFail(t, t, istioCfg.SystemNamespace) | ||
|
||
from := apps.EchoNamespace.A | ||
to := apps.EchoNamespace.B | ||
fromAndTo := from.Append(to) | ||
|
||
lastUpdateTime, err := getWorkloadCertLastUpdateTime(t, from[0], istioCtl) | ||
if err != nil { | ||
t.Errorf("failed to get workload cert last update time: %v", err) | ||
} | ||
|
||
// Verify traffic works between a and b | ||
echotest.New(t, fromAndTo). | ||
WithDefaultFilters(1, 1). | ||
FromMatch(match.ServiceName(from.NamespacedName())). | ||
ToMatch(match.ServiceName(to.NamespacedName())). | ||
Run(func(t framework.TestContext, from echo.Instance, to echo.Target) { | ||
// Verify mTLS works between a and b | ||
opts := echo.CallOptions{ | ||
To: to, | ||
Port: echo.Port{ | ||
Name: "http", | ||
}, | ||
} | ||
opts.Check = check.And(check.OK(), check.ReachedTargetClusters(t)) | ||
|
||
from.CallOrFail(t, opts) | ||
}) | ||
|
||
// Rotate CA cert | ||
if err := cert.CreateCustomCASecret(t, | ||
"ca-cert.pem", "ca-key.pem", | ||
"cert-chain.pem", "root-cert-combined.pem"); err != nil { | ||
t.Errorf("failed to update combined CA secret: %v", err) | ||
} | ||
|
||
// Wait for workload cert to be updated | ||
retry.UntilOrFail(t, func() bool { | ||
updateTime, err := getWorkloadCertLastUpdateTime(t, from[0], istioCtl) | ||
if err != nil { | ||
t.Logf("failed to get workload cert last update time: %v", err) | ||
return false | ||
} | ||
|
||
// retry when workload cert is not updated | ||
if updateTime.After(lastUpdateTime) { | ||
t.Logf("workload cert is updated, last update time: %v", updateTime) | ||
return true | ||
} | ||
|
||
return false | ||
}, retry.Timeout(time.Minute), retry.Delay(1*time.Second)) | ||
|
||
// Verify traffic works between a and b after cert rotation | ||
echotest.New(t, fromAndTo). | ||
WithDefaultFilters(1, 1). | ||
FromMatch(match.ServiceName(from.NamespacedName())). | ||
ToMatch(match.ServiceName(to.NamespacedName())). | ||
Run(func(t framework.TestContext, from echo.Instance, to echo.Target) { | ||
// Verify mTLS works between a and b | ||
opts := echo.CallOptions{ | ||
To: to, | ||
Port: echo.Port{ | ||
Name: "http", | ||
}, | ||
} | ||
opts.Check = check.And(check.OK(), check.ReachedTargetClusters(t)) | ||
|
||
from.CallOrFail(t, opts) | ||
}) | ||
}) | ||
} | ||
|
||
func getWorkloadCertLastUpdateTime(t framework.TestContext, i echo.Instance, ctl istioctl.Instance) (time.Time, error) { | ||
podID, err := getPodID(i) | ||
if err != nil { | ||
t.Fatalf("Could not get Pod ID: %v", err) | ||
} | ||
podName := fmt.Sprintf("%s.%s", podID, i.NamespaceName()) | ||
out, errOut, err := ctl.Invoke([]string{"pc", "s", podName, "-o", "json"}) | ||
if err != nil || errOut != "" { | ||
t.Errorf("failed to retrieve pod secret from %s, err: %v errOut: %s", podName, err, errOut) | ||
} | ||
|
||
dump := &admin.SecretsConfigDump{} | ||
if err := protomarshal.Unmarshal([]byte(out), dump); err != nil { | ||
t.Errorf("failed to unmarshal secret dump: %v", err) | ||
} | ||
|
||
for _, s := range dump.DynamicActiveSecrets { | ||
if s.Name == security.WorkloadKeyCertResourceName { | ||
return s.LastUpdated.AsTime(), nil | ||
} | ||
} | ||
|
||
return time.Now(), errors.New("failed to find workload cert") | ||
} | ||
|
||
func getPodID(i echo.Instance) (string, error) { | ||
wls, err := i.Workloads() | ||
if err != nil { | ||
return "", nil | ||
} | ||
|
||
for _, wl := range wls { | ||
return wl.PodName(), nil | ||
} | ||
|
||
return "", fmt.Errorf("no workloads") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters