gmsa_kubelet.go 4.9 KB

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