kms_transformation_test.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // +build !windows
  2. /*
  3. Copyright 2017 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package master
  15. import (
  16. "bytes"
  17. "context"
  18. "crypto/aes"
  19. "encoding/binary"
  20. "fmt"
  21. "net/http"
  22. "strings"
  23. "testing"
  24. "time"
  25. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  26. "k8s.io/apimachinery/pkg/util/wait"
  27. "k8s.io/apiserver/pkg/storage/value"
  28. aestransformer "k8s.io/apiserver/pkg/storage/value/encrypt/aes"
  29. mock "k8s.io/apiserver/pkg/storage/value/encrypt/envelope/testing"
  30. kmsapi "k8s.io/apiserver/pkg/storage/value/encrypt/envelope/v1beta1"
  31. "k8s.io/client-go/kubernetes"
  32. "k8s.io/client-go/rest"
  33. )
  34. const (
  35. dekKeySizeLen = 2
  36. kmsAPIVersion = "v1beta1"
  37. )
  38. type envelope struct {
  39. providerName string
  40. rawEnvelope []byte
  41. plainTextDEK []byte
  42. }
  43. func (r envelope) prefix() string {
  44. return fmt.Sprintf("k8s:enc:kms:v1:%s:", r.providerName)
  45. }
  46. func (r envelope) prefixLen() int {
  47. return len(r.prefix())
  48. }
  49. func (r envelope) dekLen() int {
  50. // DEK's length is stored in the two bytes that follow the prefix.
  51. return int(binary.BigEndian.Uint16(r.rawEnvelope[r.prefixLen() : r.prefixLen()+dekKeySizeLen]))
  52. }
  53. func (r envelope) cipherTextDEK() []byte {
  54. return r.rawEnvelope[r.prefixLen()+dekKeySizeLen : r.prefixLen()+dekKeySizeLen+r.dekLen()]
  55. }
  56. func (r envelope) startOfPayload(providerName string) int {
  57. return r.prefixLen() + dekKeySizeLen + r.dekLen()
  58. }
  59. func (r envelope) cipherTextPayload() []byte {
  60. return r.rawEnvelope[r.startOfPayload(r.providerName):]
  61. }
  62. func (r envelope) plainTextPayload(secretETCDPath string) ([]byte, error) {
  63. block, err := aes.NewCipher(r.plainTextDEK)
  64. if err != nil {
  65. return nil, fmt.Errorf("failed to initialize AES Cipher: %v", err)
  66. }
  67. // etcd path of the key is used as the authenticated context - need to pass it to decrypt
  68. ctx := value.DefaultContext([]byte(secretETCDPath))
  69. aescbcTransformer := aestransformer.NewCBCTransformer(block)
  70. plainSecret, _, err := aescbcTransformer.TransformFromStorage(r.cipherTextPayload(), ctx)
  71. if err != nil {
  72. return nil, fmt.Errorf("failed to transform from storage via AESCBC, err: %v", err)
  73. }
  74. return plainSecret, nil
  75. }
  76. // TestKMSProvider is an integration test between KubeAPI, ETCD and KMS Plugin
  77. // Concretely, this test verifies the following integration contracts:
  78. // 1. Raw records in ETCD that were processed by KMS Provider should be prefixed with k8s:enc:kms:v1:grpc-kms-provider-name:
  79. // 2. Data Encryption Key (DEK) should be generated by envelopeTransformer and passed to KMS gRPC Plugin
  80. // 3. KMS gRPC Plugin should encrypt the DEK with a Key Encryption Key (KEK) and pass it back to envelopeTransformer
  81. // 4. The cipherTextPayload (ex. Secret) should be encrypted via AES CBC transform
  82. // 5. Prefix-EncryptedDEK-EncryptedPayload structure should be deposited to ETCD
  83. func TestKMSProvider(t *testing.T) {
  84. encryptionConfig := `
  85. kind: EncryptionConfiguration
  86. apiVersion: apiserver.config.k8s.io/v1
  87. resources:
  88. - resources:
  89. - secrets
  90. providers:
  91. - kms:
  92. name: kms-provider
  93. cachesize: 1000
  94. endpoint: unix:///@kms-provider.sock
  95. `
  96. providerName := "kms-provider"
  97. pluginMock, err := mock.NewBase64Plugin("@kms-provider.sock")
  98. if err != nil {
  99. t.Fatalf("failed to create mock of KMS Plugin: %v", err)
  100. }
  101. go pluginMock.Start()
  102. if err := mock.WaitForBase64PluginToBeUp(pluginMock); err != nil {
  103. t.Fatalf("Failed start plugin, err: %v", err)
  104. }
  105. defer pluginMock.CleanUp()
  106. test, err := newTransformTest(t, encryptionConfig)
  107. if err != nil {
  108. t.Fatalf("failed to start KUBE API Server with encryptionConfig\n %s, error: %v", encryptionConfig, err)
  109. }
  110. defer test.cleanUp()
  111. test.secret, err = test.createSecret(testSecret, testNamespace)
  112. if err != nil {
  113. t.Fatalf("Failed to create test secret, error: %v", err)
  114. }
  115. // Since Data Encryption Key (DEK) is randomly generated (per encryption operation), we need to ask KMS Mock for it.
  116. plainTextDEK := pluginMock.LastEncryptRequest()
  117. secretETCDPath := test.getETCDPath()
  118. rawEnvelope, err := test.getRawSecretFromETCD()
  119. if err != nil {
  120. t.Fatalf("failed to read %s from etcd: %v", secretETCDPath, err)
  121. }
  122. envelope := envelope{
  123. providerName: providerName,
  124. rawEnvelope: rawEnvelope,
  125. plainTextDEK: plainTextDEK,
  126. }
  127. wantPrefix := "k8s:enc:kms:v1:kms-provider:"
  128. if !bytes.HasPrefix(rawEnvelope, []byte(wantPrefix)) {
  129. t.Fatalf("expected secret to be prefixed with %s, but got %s", wantPrefix, rawEnvelope)
  130. }
  131. decryptResponse, err := pluginMock.Decrypt(context.Background(), &kmsapi.DecryptRequest{Version: kmsAPIVersion, Cipher: envelope.cipherTextDEK()})
  132. if err != nil {
  133. t.Fatalf("failed to decrypt DEK, %v", err)
  134. }
  135. dekPlainAsWouldBeSeenByETCD := decryptResponse.Plain
  136. if !bytes.Equal(plainTextDEK, dekPlainAsWouldBeSeenByETCD) {
  137. t.Fatalf("expected plainTextDEK %v to be passed to KMS Plugin, but got %s",
  138. plainTextDEK, dekPlainAsWouldBeSeenByETCD)
  139. }
  140. plainSecret, err := envelope.plainTextPayload(secretETCDPath)
  141. if err != nil {
  142. t.Fatalf("failed to transform from storage via AESCBC, err: %v", err)
  143. }
  144. if !strings.Contains(string(plainSecret), secretVal) {
  145. t.Fatalf("expected %q after decryption, but got %q", secretVal, string(plainSecret))
  146. }
  147. // Secrets should be un-enveloped on direct reads from Kube API Server.
  148. s, err := test.restClient.CoreV1().Secrets(testNamespace).Get(context.TODO(), testSecret, metav1.GetOptions{})
  149. if err != nil {
  150. t.Fatalf("failed to get Secret from %s, err: %v", testNamespace, err)
  151. }
  152. if secretVal != string(s.Data[secretKey]) {
  153. t.Fatalf("expected %s from KubeAPI, but got %s", secretVal, string(s.Data[secretKey]))
  154. }
  155. }
  156. func TestKMSHealthz(t *testing.T) {
  157. encryptionConfig := `
  158. kind: EncryptionConfiguration
  159. apiVersion: apiserver.config.k8s.io/v1
  160. resources:
  161. - resources:
  162. - secrets
  163. providers:
  164. - kms:
  165. name: provider-1
  166. endpoint: unix:///@kms-provider-1.sock
  167. - kms:
  168. name: provider-2
  169. endpoint: unix:///@kms-provider-2.sock
  170. `
  171. pluginMock1, err := mock.NewBase64Plugin("@kms-provider-1.sock")
  172. if err != nil {
  173. t.Fatalf("failed to create mock of KMS Plugin #1: %v", err)
  174. }
  175. if err := pluginMock1.Start(); err != nil {
  176. t.Fatalf("Failed to start kms-plugin, err: %v", err)
  177. }
  178. defer pluginMock1.CleanUp()
  179. if err := mock.WaitForBase64PluginToBeUp(pluginMock1); err != nil {
  180. t.Fatalf("Failed to start plugin #1, err: %v", err)
  181. }
  182. pluginMock2, err := mock.NewBase64Plugin("@kms-provider-2.sock")
  183. if err != nil {
  184. t.Fatalf("Failed to create mock of KMS Plugin #2: err: %v", err)
  185. }
  186. if err := pluginMock2.Start(); err != nil {
  187. t.Fatalf("Failed to start kms-plugin, err: %v", err)
  188. }
  189. defer pluginMock2.CleanUp()
  190. if err := mock.WaitForBase64PluginToBeUp(pluginMock2); err != nil {
  191. t.Fatalf("Failed to start KMS Plugin #2: err: %v", err)
  192. }
  193. test, err := newTransformTest(t, encryptionConfig)
  194. if err != nil {
  195. t.Fatalf("Failed to start kube-apiserver, error: %v", err)
  196. }
  197. defer test.cleanUp()
  198. // Name of the healthz check is calculated based on a constant "kms-provider-" + position of the
  199. // provider in the config.
  200. // Stage 1 - Since all kms-plugins are guaranteed to be up, healthz checks for:
  201. // healthz/kms-provider-0 and /healthz/kms-provider-1 should be OK.
  202. mustBeHealthy(t, "kms-provider-0", test.kubeAPIServer.ClientConfig)
  203. mustBeHealthy(t, "kms-provider-1", test.kubeAPIServer.ClientConfig)
  204. // Stage 2 - kms-plugin for provider-1 is down. Therefore, expect the health check for provider-1
  205. // to fail, but provider-2 should still be OK
  206. pluginMock1.EnterFailedState()
  207. mustBeUnHealthy(t, "kms-provider-0", test.kubeAPIServer.ClientConfig)
  208. mustBeHealthy(t, "kms-provider-1", test.kubeAPIServer.ClientConfig)
  209. pluginMock1.ExitFailedState()
  210. // Stage 3 - kms-plugin for provider-1 is now up. Therefore, expect the health check for provider-1
  211. // to succeed now, but provider-2 is now down.
  212. // Need to sleep since health check chases responses for 3 seconds.
  213. pluginMock2.EnterFailedState()
  214. mustBeHealthy(t, "kms-provider-0", test.kubeAPIServer.ClientConfig)
  215. mustBeUnHealthy(t, "kms-provider-1", test.kubeAPIServer.ClientConfig)
  216. }
  217. func mustBeHealthy(t *testing.T, checkName string, clientConfig *rest.Config) {
  218. t.Helper()
  219. var restErr error
  220. pollErr := wait.PollImmediate(2*time.Second, wait.ForeverTestTimeout, func() (bool, error) {
  221. status, err := getHealthz(checkName, clientConfig)
  222. if err != nil {
  223. return false, err
  224. }
  225. return status == http.StatusOK, nil
  226. })
  227. if pollErr == wait.ErrWaitTimeout {
  228. t.Fatalf("failed to get the expected healthz status of OK for check: %s, error: %v", restErr, checkName)
  229. }
  230. }
  231. func mustBeUnHealthy(t *testing.T, checkName string, clientConfig *rest.Config) {
  232. t.Helper()
  233. var restErr error
  234. pollErr := wait.PollImmediate(2*time.Second, wait.ForeverTestTimeout, func() (bool, error) {
  235. status, err := getHealthz(checkName, clientConfig)
  236. if err != nil {
  237. return false, err
  238. }
  239. return status != http.StatusOK, nil
  240. })
  241. if pollErr == wait.ErrWaitTimeout {
  242. t.Fatalf("failed to get the expected healthz status of !OK for check: %s, error: %v", restErr, checkName)
  243. }
  244. }
  245. func getHealthz(checkName string, clientConfig *rest.Config) (int, error) {
  246. client, err := kubernetes.NewForConfig(clientConfig)
  247. if err != nil {
  248. return 0, fmt.Errorf("failed to create a client: %v", err)
  249. }
  250. result := client.CoreV1().RESTClient().Get().AbsPath(fmt.Sprintf("/healthz/%v", checkName)).Do(context.TODO())
  251. status := 0
  252. result.StatusCode(&status)
  253. return status, nil
  254. }