2022-03-01 19:28:39 +00:00
|
|
|
package secretsencryption
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/tests/e2e"
|
2022-03-01 19:28:39 +00:00
|
|
|
. "github.com/onsi/ginkgo/v2"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
)
|
|
|
|
|
2023-08-01 18:16:00 +00:00
|
|
|
// This test is desigened for the new secrets-encrypt rotate-keys command,
|
|
|
|
// Added in v1.28.0+k3s1
|
|
|
|
|
2023-01-27 17:01:13 +00:00
|
|
|
// Valid nodeOS: generic/ubuntu2004, opensuse/Leap-15.3.x86_64
|
2022-03-01 19:28:39 +00:00
|
|
|
var nodeOS = flag.String("nodeOS", "generic/ubuntu2004", "VM operating system")
|
|
|
|
var serverCount = flag.Int("serverCount", 3, "number of server nodes")
|
2022-08-05 16:16:10 +00:00
|
|
|
var hardened = flag.Bool("hardened", false, "true or false")
|
2023-01-12 00:09:45 +00:00
|
|
|
var ci = flag.Bool("ci", false, "running on CI")
|
2023-03-11 03:53:41 +00:00
|
|
|
var local = flag.Bool("local", false, "deploy a locally built K3s binary")
|
2022-03-01 19:28:39 +00:00
|
|
|
|
2022-03-15 17:29:56 +00:00
|
|
|
// Environment Variables Info:
|
2023-08-01 18:16:00 +00:00
|
|
|
// E2E_RELEASE_VERSION=v1.28.0+k3s1 or nil for latest commit from master
|
2022-03-01 19:28:39 +00:00
|
|
|
|
2022-08-05 16:16:10 +00:00
|
|
|
func Test_E2ESecretsEncryption(t *testing.T) {
|
2022-03-01 19:28:39 +00:00
|
|
|
RegisterFailHandler(Fail)
|
|
|
|
flag.Parse()
|
2022-11-04 23:07:12 +00:00
|
|
|
suiteConfig, reporterConfig := GinkgoConfiguration()
|
|
|
|
RunSpecs(t, "Secrets Encryption Test Suite", suiteConfig, reporterConfig)
|
2022-03-01 19:28:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
kubeConfigFile string
|
2022-03-02 20:42:55 +00:00
|
|
|
serverNodeNames []string
|
2022-03-01 19:28:39 +00:00
|
|
|
)
|
|
|
|
|
2022-11-04 23:07:12 +00:00
|
|
|
var _ = ReportAfterEach(e2e.GenReport)
|
|
|
|
|
2022-09-06 19:56:37 +00:00
|
|
|
var _ = Describe("Verify Secrets Encryption Rotation", Ordered, func() {
|
2023-01-27 17:01:13 +00:00
|
|
|
Context("Secrets Keys are rotated:", func() {
|
2023-08-01 18:16:00 +00:00
|
|
|
FIt("Starts up with no issues", func() {
|
2022-03-01 19:28:39 +00:00
|
|
|
var err error
|
2023-03-11 03:53:41 +00:00
|
|
|
if *local {
|
|
|
|
serverNodeNames, _, err = e2e.CreateLocalCluster(*nodeOS, *serverCount, 0)
|
|
|
|
} else {
|
|
|
|
serverNodeNames, _, err = e2e.CreateCluster(*nodeOS, *serverCount, 0)
|
|
|
|
}
|
2023-01-27 20:40:29 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred(), e2e.GetVagrantLog(err))
|
2022-03-01 19:28:39 +00:00
|
|
|
fmt.Println("CLUSTER CONFIG")
|
|
|
|
fmt.Println("OS:", *nodeOS)
|
2022-03-02 20:42:55 +00:00
|
|
|
fmt.Println("Server Nodes:", serverNodeNames)
|
|
|
|
kubeConfigFile, err = e2e.GenKubeConfigFile(serverNodeNames[0])
|
2022-03-01 19:28:39 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
2023-08-01 18:16:00 +00:00
|
|
|
FIt("Checks node and pod status", func() {
|
2022-03-01 19:28:39 +00:00
|
|
|
fmt.Printf("\nFetching node status\n")
|
|
|
|
Eventually(func(g Gomega) {
|
|
|
|
nodes, err := e2e.ParseNodes(kubeConfigFile, false)
|
|
|
|
g.Expect(err).NotTo(HaveOccurred())
|
|
|
|
for _, node := range nodes {
|
|
|
|
g.Expect(node.Status).Should(Equal("Ready"))
|
|
|
|
}
|
2023-01-12 00:09:45 +00:00
|
|
|
}, "620s", "5s").Should(Succeed())
|
2022-03-01 19:28:39 +00:00
|
|
|
_, _ = e2e.ParseNodes(kubeConfigFile, true)
|
|
|
|
|
|
|
|
fmt.Printf("\nFetching pods status\n")
|
|
|
|
Eventually(func(g Gomega) {
|
|
|
|
pods, err := e2e.ParsePods(kubeConfigFile, false)
|
|
|
|
g.Expect(err).NotTo(HaveOccurred())
|
|
|
|
for _, pod := range pods {
|
|
|
|
if strings.Contains(pod.Name, "helm-install") {
|
|
|
|
g.Expect(pod.Status).Should(Equal("Completed"), pod.Name)
|
|
|
|
} else {
|
|
|
|
g.Expect(pod.Status).Should(Equal("Running"), pod.Name)
|
|
|
|
}
|
|
|
|
}
|
2023-01-12 00:09:45 +00:00
|
|
|
}, "620s", "5s").Should(Succeed())
|
2022-03-01 19:28:39 +00:00
|
|
|
_, _ = e2e.ParsePods(kubeConfigFile, true)
|
|
|
|
})
|
|
|
|
|
2023-08-01 18:16:00 +00:00
|
|
|
FIt("Deploys several secrets", func() {
|
2022-08-05 16:16:10 +00:00
|
|
|
_, err := e2e.DeployWorkload("secrets.yaml", kubeConfigFile, *hardened)
|
2022-03-01 19:28:39 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred(), "Secrets not deployed")
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Verifies encryption start stage", func() {
|
2023-06-07 23:03:36 +00:00
|
|
|
cmd := "k3s secrets-encrypt status"
|
2022-03-02 20:42:55 +00:00
|
|
|
for _, nodeName := range serverNodeNames {
|
2022-03-01 19:28:39 +00:00
|
|
|
res, err := e2e.RunCmdOnNode(cmd, nodeName)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(res).Should(ContainSubstring("Encryption Status: Enabled"))
|
|
|
|
Expect(res).Should(ContainSubstring("Current Rotation Stage: start"))
|
|
|
|
Expect(res).Should(ContainSubstring("Server Encryption Hashes: All hashes match"))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Rotates the Secrets-Encryption Keys", func() {
|
2023-08-01 18:16:00 +00:00
|
|
|
cmd := "k3s secrets-encrypt rotate-keys"
|
2022-03-02 20:42:55 +00:00
|
|
|
res, err := e2e.RunCmdOnNode(cmd, serverNodeNames[0])
|
2022-03-01 19:28:39 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred(), res)
|
2022-03-02 20:42:55 +00:00
|
|
|
for i, nodeName := range serverNodeNames {
|
2022-08-05 16:16:10 +00:00
|
|
|
Eventually(func(g Gomega) {
|
2023-06-07 23:03:36 +00:00
|
|
|
cmd := "k3s secrets-encrypt status"
|
2022-08-05 16:16:10 +00:00
|
|
|
res, err := e2e.RunCmdOnNode(cmd, nodeName)
|
|
|
|
g.Expect(err).NotTo(HaveOccurred(), res)
|
|
|
|
g.Expect(res).Should(ContainSubstring("Server Encryption Hashes: hash does not match"))
|
|
|
|
if i == 0 {
|
2023-08-01 18:16:00 +00:00
|
|
|
g.Expect(res).Should(ContainSubstring("Current Rotation Stage: reencrypt_finished"))
|
2022-08-05 16:16:10 +00:00
|
|
|
} else {
|
2023-08-01 18:16:00 +00:00
|
|
|
g.Expect(res).Should(ContainSubstring("Current Rotation Stage: start"))
|
2022-08-05 16:16:10 +00:00
|
|
|
}
|
|
|
|
}, "420s", "2s").Should(Succeed())
|
2022-03-01 19:28:39 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Restarts K3s servers", func() {
|
2023-03-11 03:53:41 +00:00
|
|
|
Expect(e2e.RestartCluster(serverNodeNames)).To(Succeed(), e2e.GetVagrantLog(nil))
|
2022-03-01 19:28:39 +00:00
|
|
|
})
|
|
|
|
|
2023-08-01 18:16:00 +00:00
|
|
|
It("Verifies reencryption_finished stage", func() {
|
2023-06-07 23:03:36 +00:00
|
|
|
cmd := "k3s secrets-encrypt status"
|
2022-03-02 20:42:55 +00:00
|
|
|
for _, nodeName := range serverNodeNames {
|
2022-08-05 16:16:10 +00:00
|
|
|
Eventually(func(g Gomega) {
|
|
|
|
res, err := e2e.RunCmdOnNode(cmd, nodeName)
|
|
|
|
g.Expect(err).NotTo(HaveOccurred())
|
|
|
|
g.Expect(res).Should(ContainSubstring("Encryption Status: Enabled"))
|
2023-08-01 18:16:00 +00:00
|
|
|
g.Expect(res).Should(ContainSubstring("Current Rotation Stage: reencrypt_finished"))
|
2022-08-05 16:16:10 +00:00
|
|
|
g.Expect(res).Should(ContainSubstring("Server Encryption Hashes: All hashes match"))
|
|
|
|
}, "420s", "2s").Should(Succeed())
|
2022-03-01 19:28:39 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2023-01-27 17:01:13 +00:00
|
|
|
Context("Disabling Secrets-Encryption", func() {
|
|
|
|
It("Disables encryption", func() {
|
2023-06-07 23:03:36 +00:00
|
|
|
cmd := "k3s secrets-encrypt disable"
|
2023-01-27 17:01:13 +00:00
|
|
|
res, err := e2e.RunCmdOnNode(cmd, serverNodeNames[0])
|
|
|
|
Expect(err).NotTo(HaveOccurred(), res)
|
2022-03-01 19:28:39 +00:00
|
|
|
|
2023-06-07 23:03:36 +00:00
|
|
|
cmd = "k3s secrets-encrypt status"
|
2023-01-27 17:01:13 +00:00
|
|
|
Eventually(func() (string, error) {
|
|
|
|
return e2e.RunCmdOnNode(cmd, serverNodeNames[0])
|
2023-07-12 15:49:05 +00:00
|
|
|
}, "240s", "10s").Should(ContainSubstring("Current Rotation Stage: reencrypt_finished"))
|
2022-03-01 19:28:39 +00:00
|
|
|
|
2023-01-27 17:01:13 +00:00
|
|
|
for i, nodeName := range serverNodeNames {
|
|
|
|
Eventually(func(g Gomega) {
|
|
|
|
res, err := e2e.RunCmdOnNode(cmd, nodeName)
|
|
|
|
g.Expect(err).NotTo(HaveOccurred(), res)
|
|
|
|
if i == 0 {
|
|
|
|
g.Expect(res).Should(ContainSubstring("Encryption Status: Disabled"))
|
|
|
|
} else {
|
|
|
|
g.Expect(res).Should(ContainSubstring("Encryption Status: Enabled"))
|
|
|
|
}
|
|
|
|
}, "420s", "2s").Should(Succeed())
|
|
|
|
}
|
|
|
|
})
|
2022-03-01 19:28:39 +00:00
|
|
|
|
2023-01-27 17:01:13 +00:00
|
|
|
It("Restarts K3s servers", func() {
|
|
|
|
Expect(e2e.RestartCluster(serverNodeNames)).To(Succeed())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Verifies encryption disabled on all nodes", func() {
|
2023-06-07 23:03:36 +00:00
|
|
|
cmd := "k3s secrets-encrypt status"
|
2023-01-27 17:01:13 +00:00
|
|
|
for _, nodeName := range serverNodeNames {
|
|
|
|
Eventually(func(g Gomega) {
|
|
|
|
g.Expect(e2e.RunCmdOnNode(cmd, nodeName)).Should(ContainSubstring("Encryption Status: Disabled"))
|
|
|
|
}, "420s", "2s").Should(Succeed())
|
|
|
|
}
|
|
|
|
})
|
2022-03-01 19:28:39 +00:00
|
|
|
|
|
|
|
})
|
|
|
|
|
2023-01-27 17:01:13 +00:00
|
|
|
Context("Enabling Secrets-Encryption", func() {
|
|
|
|
It("Enables encryption", func() {
|
2023-06-07 23:03:36 +00:00
|
|
|
cmd := "k3s secrets-encrypt enable"
|
2023-01-27 17:01:13 +00:00
|
|
|
res, err := e2e.RunCmdOnNode(cmd, serverNodeNames[0])
|
|
|
|
Expect(err).NotTo(HaveOccurred(), res)
|
2022-03-01 19:28:39 +00:00
|
|
|
|
2023-06-07 23:03:36 +00:00
|
|
|
cmd = "k3s secrets-encrypt status"
|
2023-01-27 17:01:13 +00:00
|
|
|
Eventually(func() (string, error) {
|
|
|
|
return e2e.RunCmdOnNode(cmd, serverNodeNames[0])
|
|
|
|
}, "180s", "5s").Should(ContainSubstring("Current Rotation Stage: reencrypt_finished"))
|
|
|
|
})
|
2022-03-01 19:28:39 +00:00
|
|
|
|
2023-01-27 17:01:13 +00:00
|
|
|
It("Restarts K3s servers", func() {
|
|
|
|
Expect(e2e.RestartCluster(serverNodeNames)).To(Succeed())
|
|
|
|
})
|
2022-03-01 19:28:39 +00:00
|
|
|
|
2023-01-27 17:01:13 +00:00
|
|
|
It("Verifies encryption enabled on all nodes", func() {
|
2023-06-07 23:03:36 +00:00
|
|
|
cmd := "k3s secrets-encrypt status"
|
2023-01-27 17:01:13 +00:00
|
|
|
for _, nodeName := range serverNodeNames {
|
|
|
|
Eventually(func(g Gomega) {
|
|
|
|
g.Expect(e2e.RunCmdOnNode(cmd, nodeName)).Should(ContainSubstring("Encryption Status: Enabled"))
|
|
|
|
}, "420s", "2s").Should(Succeed())
|
|
|
|
}
|
2022-08-05 16:16:10 +00:00
|
|
|
|
2023-01-27 17:01:13 +00:00
|
|
|
})
|
2022-03-01 19:28:39 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2022-11-04 23:07:12 +00:00
|
|
|
var failed bool
|
2022-03-01 19:28:39 +00:00
|
|
|
var _ = AfterEach(func() {
|
|
|
|
failed = failed || CurrentSpecReport().Failed()
|
|
|
|
})
|
|
|
|
|
|
|
|
var _ = AfterSuite(func() {
|
2023-01-12 00:09:45 +00:00
|
|
|
if failed && !*ci {
|
2022-03-01 19:28:39 +00:00
|
|
|
fmt.Println("FAILED!")
|
|
|
|
} else {
|
2023-08-01 18:16:00 +00:00
|
|
|
// Expect(e2e.GetCoverageReport(serverNodeNames)).To(Succeed())
|
|
|
|
// Expect(e2e.DestroyCluster()).To(Succeed())
|
|
|
|
// Expect(os.Remove(kubeConfigFile)).To(Succeed())
|
2022-03-01 19:28:39 +00:00
|
|
|
}
|
|
|
|
})
|