apiserver_kms_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. Copyright 2017 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. package gci
  14. import (
  15. "bytes"
  16. "encoding/base64"
  17. "fmt"
  18. "io/ioutil"
  19. "os"
  20. "path/filepath"
  21. "reflect"
  22. "strings"
  23. "testing"
  24. v1 "k8s.io/api/core/v1"
  25. )
  26. const (
  27. kubeAPIServerManifestFileName = "kube-apiserver.manifest"
  28. kubeAPIServerConfigScriptName = "configure-kubeapiserver.sh"
  29. kubeAPIServerStartFuncName = "start-kube-apiserver"
  30. )
  31. type kubeAPIServerEnv struct {
  32. KubeHome string
  33. EncryptionProviderConfigPath string
  34. EncryptionProviderConfig string
  35. CloudKMSIntegration bool
  36. }
  37. func TestEncryptionProviderFlag(t *testing.T) {
  38. var (
  39. // command": [
  40. // "/bin/sh", - Index 0
  41. // "-c", - Index 1
  42. // "exec /usr/local/bin/kube-apiserver " - Index 2
  43. execArgsIndex = 2
  44. encryptionConfigFlag = "--encryption-provider-config"
  45. )
  46. testCases := []struct {
  47. desc string
  48. encryptionProviderConfig string
  49. wantFlag bool
  50. }{
  51. {
  52. desc: "ENCRYPTION_PROVIDER_CONFIG is set",
  53. encryptionProviderConfig: base64.StdEncoding.EncodeToString([]byte("foo")),
  54. wantFlag: true,
  55. },
  56. {
  57. desc: "ENCRYPTION_PROVIDER_CONFIG is not set",
  58. encryptionProviderConfig: "",
  59. wantFlag: false,
  60. },
  61. }
  62. for _, tc := range testCases {
  63. t.Run(tc.desc, func(t *testing.T) {
  64. c := newManifestTestCase(t, kubeAPIServerManifestFileName, kubeAPIServerStartFuncName, nil)
  65. defer c.tearDown()
  66. e := kubeAPIServerEnv{
  67. KubeHome: c.kubeHome,
  68. EncryptionProviderConfigPath: filepath.Join(c.kubeHome, "encryption-provider-config.yaml"),
  69. EncryptionProviderConfig: tc.encryptionProviderConfig,
  70. }
  71. c.mustInvokeFunc(
  72. e,
  73. kubeAPIServerConfigScriptName,
  74. "kms.template",
  75. "testdata/kube-apiserver/base.template",
  76. "testdata/kube-apiserver/kms.template")
  77. c.mustLoadPodFromManifest()
  78. execArgs := c.pod.Spec.Containers[0].Command[execArgsIndex]
  79. flagIsInArg := strings.Contains(execArgs, encryptionConfigFlag)
  80. flag := fmt.Sprintf("%s=%s", encryptionConfigFlag, e.EncryptionProviderConfigPath)
  81. switch {
  82. case tc.wantFlag && !flagIsInArg:
  83. t.Fatalf("Got %q,\n want flags to contain %q", execArgs, flag)
  84. case !tc.wantFlag && flagIsInArg:
  85. t.Fatalf("Got %q,\n do not want flags to contain %q", execArgs, encryptionConfigFlag)
  86. case tc.wantFlag && flagIsInArg && !strings.Contains(execArgs, flag):
  87. t.Fatalf("Got flags: %q, want it to contain %q", execArgs, flag)
  88. }
  89. })
  90. }
  91. }
  92. func TestEncryptionProviderConfig(t *testing.T) {
  93. c := newManifestTestCase(t, kubeAPIServerManifestFileName, kubeAPIServerStartFuncName, nil)
  94. defer c.tearDown()
  95. p := filepath.Join(c.kubeHome, "encryption-provider-config.yaml")
  96. e := kubeAPIServerEnv{
  97. KubeHome: c.kubeHome,
  98. EncryptionProviderConfigPath: p,
  99. EncryptionProviderConfig: base64.StdEncoding.EncodeToString([]byte("foo")),
  100. }
  101. c.mustInvokeFunc(
  102. e,
  103. kubeAPIServerConfigScriptName,
  104. "kms.template",
  105. "testdata/kube-apiserver/base.template",
  106. "testdata/kube-apiserver/kms.template",
  107. )
  108. if _, err := os.Stat(p); err != nil {
  109. c.t.Fatalf("Expected encryption provider config to be written to %s, but stat failed with error: %v", p, err)
  110. }
  111. got, err := ioutil.ReadFile(p)
  112. if err != nil {
  113. c.t.Fatalf("Failed to read encryption provider config %s", p)
  114. }
  115. want := []byte("foo")
  116. if !bytes.Equal(got, want) {
  117. c.t.Fatalf("got encryptionConfig:\n%q\n, want encryptionConfig:\n%q", got, want)
  118. }
  119. }
  120. func TestKMSIntegration(t *testing.T) {
  121. var (
  122. socketPath = "/var/run/kmsplugin"
  123. dirOrCreate = v1.HostPathType(v1.HostPathDirectoryOrCreate)
  124. socketName = "kmssocket"
  125. )
  126. testCases := []struct {
  127. desc string
  128. cloudKMSIntegration bool
  129. wantVolume v1.Volume
  130. wantVolMount v1.VolumeMount
  131. }{
  132. {
  133. desc: "CLOUD_KMS_INTEGRATION is set",
  134. cloudKMSIntegration: true,
  135. wantVolume: v1.Volume{
  136. Name: socketName,
  137. VolumeSource: v1.VolumeSource{
  138. HostPath: &v1.HostPathVolumeSource{
  139. Path: socketPath,
  140. Type: &dirOrCreate,
  141. },
  142. },
  143. },
  144. wantVolMount: v1.VolumeMount{
  145. Name: socketName,
  146. MountPath: socketPath,
  147. },
  148. },
  149. {
  150. desc: "CLOUD_KMS_INTEGRATION is not set",
  151. cloudKMSIntegration: false,
  152. },
  153. }
  154. for _, tc := range testCases {
  155. t.Run(tc.desc, func(t *testing.T) {
  156. c := newManifestTestCase(t, kubeAPIServerManifestFileName, kubeAPIServerStartFuncName, nil)
  157. defer c.tearDown()
  158. var e = kubeAPIServerEnv{
  159. KubeHome: c.kubeHome,
  160. EncryptionProviderConfigPath: filepath.Join(c.kubeHome, "encryption-provider-config.yaml"),
  161. EncryptionProviderConfig: base64.StdEncoding.EncodeToString([]byte("foo")),
  162. CloudKMSIntegration: tc.cloudKMSIntegration,
  163. }
  164. c.mustInvokeFunc(
  165. e,
  166. kubeAPIServerConfigScriptName,
  167. "kms.template",
  168. "testdata/kube-apiserver/base.template",
  169. "testdata/kube-apiserver/kms.template",
  170. )
  171. c.mustLoadPodFromManifest()
  172. // By this point, we can be sure that kube-apiserver manifest is a valid POD.
  173. var gotVolume v1.Volume
  174. for _, v := range c.pod.Spec.Volumes {
  175. if v.Name == socketName {
  176. gotVolume = v
  177. break
  178. }
  179. }
  180. if !reflect.DeepEqual(gotVolume, tc.wantVolume) {
  181. t.Errorf("got volume %v, want %v", gotVolume, tc.wantVolume)
  182. }
  183. var gotVolumeMount v1.VolumeMount
  184. for _, v := range c.pod.Spec.Containers[0].VolumeMounts {
  185. if v.Name == socketName {
  186. gotVolumeMount = v
  187. break
  188. }
  189. }
  190. if !reflect.DeepEqual(gotVolumeMount, tc.wantVolMount) {
  191. t.Errorf("got volumeMount %v, want %v", gotVolumeMount, tc.wantVolMount)
  192. }
  193. })
  194. }
  195. }