|
| 1 | +/* |
| 2 | +Copyright 2018 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package windows |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "strings" |
| 22 | + "time" |
| 23 | + |
| 24 | + corev1 "k8s.io/api/core/v1" |
| 25 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + "k8s.io/kubernetes/test/e2e/framework" |
| 27 | + imageutils "k8s.io/kubernetes/test/utils/image" |
| 28 | + |
| 29 | + "github.com/onsi/ginkgo" |
| 30 | + "github.com/onsi/gomega" |
| 31 | +) |
| 32 | + |
| 33 | +var _ = SIGDescribe("[Feature:Windows] [Feature:WindowsGMSA] GMSA [Slow]", func() { |
| 34 | + f := framework.NewDefaultFramework("gmsa-test-windows") |
| 35 | + |
| 36 | + ginkgo.Describe("kubelet GMSA support", func() { |
| 37 | + ginkgo.Context("when creating a pod with correct GMSA credential specs", func() { |
| 38 | + ginkgo.It("passes the credential specs down to the Pod's containers", func() { |
| 39 | + defer ginkgo.GinkgoRecover() |
| 40 | + |
| 41 | + podName := "with-correct-gmsa-annotations" |
| 42 | + |
| 43 | + container1Name := "container1" |
| 44 | + podDomain := "acme.com" |
| 45 | + |
| 46 | + container2Name := "container2" |
| 47 | + container2Domain := "contoso.org" |
| 48 | + |
| 49 | + containers := make([]corev1.Container, 2) |
| 50 | + for i, name := range []string{container1Name, container2Name} { |
| 51 | + containers[i] = corev1.Container{ |
| 52 | + Name: name, |
| 53 | + Image: imageutils.GetPauseImageName(), |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + pod := &corev1.Pod{ |
| 58 | + ObjectMeta: metav1.ObjectMeta{ |
| 59 | + Name: podName, |
| 60 | + Annotations: map[string]string{ |
| 61 | + "pod.alpha.windows.kubernetes.io/gmsa-credential-spec": generateDummyCredSpecs(podDomain), |
| 62 | + container2Name + ".container.alpha.windows.kubernetes.io/gmsa-credential-sp
6DAE
ec": generateDummyCredSpecs(container2Domain), |
| 63 | + }, |
| 64 | + }, |
| 65 | + Spec: corev1.PodSpec{ |
| 66 | + Containers: containers, |
| 67 | + }, |
| 68 | + } |
| 69 | + |
| 70 | + ginkgo.By("creating a pod with correct GMSA annotations") |
| 71 | + f.PodClient().Create(pod) |
| 72 | + |
| 73 | + ginkgo.By("waiting for the pod and its containers to be running") |
| 74 | + gomega.Eventually(func() bool { |
| 75 | + pod, err := f.PodClient().Get(podName, metav1.GetOptions{}) |
| 76 | + if err != nil && pod.Status.Phase != corev1.PodRunning { |
| 77 | + return false |
| 78 | + } |
| 79 | + |
| 80 | + for _, containerStatus := range pod.Status.ContainerStatuses { |
| 81 | <
9E12
code class="diff-text syntax-highlighted-line addition">+ if containerStatus.State.Running == nil { |
| 82 | + return false |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return true |
| 87 | + }, 5*time.Minute, 1*time.Second).Should(gomega.BeTrue()) |
| 88 | + |
| 89 | + ginkgo.By("checking the domain reported by nltest in the containers") |
| 90 | + namespaceOption := fmt.Sprintf("--namespace=%s", f.Namespace.Name) |
| 91 | + for containerName, domain := range map[string]string{ |
| 92 | + container1Name: podDomain, |
| 93 | + container2Name: container2Domain, |
| 94 | + } { |
| 95 | + var ( |
| 96 | + output string |
| 97 | + err error |
| 98 | + ) |
| 99 | + |
| 100 | + containerOption := fmt.Sprintf("--container=%s", containerName) |
| 101 | + // even for bogus creds, `nltest /PARENTDOMAIN` simply returns the AD domain, which is enough for our purpose here. |
| 102 | + // note that the "eventually" part seems to be needed to account for the fact that powershell containers |
| 103 | + // are a bit slow to become responsive, even when docker reports them as running. |
| 104 | + gomega.Eventually(func() bool { |
| 105 | + output, err = framework.RunKubectl("exec", namespaceOption, podName, containerOption, "--", "nltest", "/PARENTDOMAIN") |
| 106 | + return err == nil |
| 107 | + }, 1*time.Minute, 1*time.Second).Should(gomega.BeTrue()) |
| 108 | + |
| 109 | + if !strings.HasPrefix(output, domain) { |
| 110 | + framework.Failf("Expected %q to start with %q", output, domain) |
| 111 | + } |
| 112 | + |
| 113 | + expectedSubstr := "The command completed successfully" |
| 114 | + if !strings.Contains(output, expectedSubstr) { |
| 115 | + framework.Failf("Expected %q to contain %q", output, expectedSubstr) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + // If this was an e2e_node test, we could also check that the registry keys used to pass down the cred specs to Docker |
| 120 | + // have been properly cleaned up - but as of right now, e2e_node tests don't support Windows. We should migrate this |
| 121 | + // test to an e2e_node test when they start supporting Windows. |
| 122 | + }) |
| 123 | + }) |
| 124 | + }) |
| 125 | +}) |
| 126 | + |
| 127 | +func generateDummyCredSpecs(domain string) string { |
| 128 | + shortName := strings.ToUpper(strings.Split(domain, ".")[0]) |
| 129 | + |
| 130 | + return fmt.Sprintf(`{ |
| 131 | + "ActiveDirectoryConfig":{ |
| 132 | + "GroupManagedServiceAccounts":[ |
| 133 | + { |
| 134 | + "Name":"WebApplication", |
| 135 | + "Scope":"%s" |
| 136 | + }, |
| 137 | + { |
| 138 | + "Name":"WebApplication", |
| 139 | + "Scope":"%s" |
| 140 | + } |
| 141 | + ] |
| 142 | + }, |
| 143 | + "CmsPlugins":[ |
| 144 | + "ActiveDirectory" |
| 145 | + ], |
| 146 | + "DomainJoinConfig":{ |
| 147 | + "DnsName":"%s", |
| 148 | + "DnsTreeName":"%s", |
| 149 | + "Guid":"244818ae-87ca-4fcd-92ec-e79e5252348a", |
| 150 | + "MachineAccountName":"WebApplication", |
| 151 | + "NetBiosName":"%s", |
| 152 | + "Sid":"S-1-5-21-2126729477-2524175714-3194792973" |
| 153 | + } |
| 154 | + }`, shortName, domain, domain, domain, shortName) |
| 155 | +} |
0 commit comments