kms_plugin_mock.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. "context"
  17. "encoding/base64"
  18. "fmt"
  19. "net"
  20. "google.golang.org/grpc"
  21. kmsapi "k8s.io/apiserver/pkg/storage/value/encrypt/envelope/v1beta1"
  22. "k8s.io/klog"
  23. )
  24. const (
  25. kmsAPIVersion = "v1beta1"
  26. sockFile = "@kms-provider.sock"
  27. unixProtocol = "unix"
  28. )
  29. // base64Plugin gRPC sever for a mock KMS provider.
  30. // Uses base64 to simulate encrypt and decrypt.
  31. type base64Plugin struct {
  32. grpcServer *grpc.Server
  33. listener net.Listener
  34. // Allow users of the plugin to sense requests that were passed to KMS.
  35. encryptRequest chan *kmsapi.EncryptRequest
  36. }
  37. func newBase64Plugin() (*base64Plugin, error) {
  38. listener, err := net.Listen(unixProtocol, sockFile)
  39. if err != nil {
  40. return nil, fmt.Errorf("failed to listen on the unix socket, error: %v", err)
  41. }
  42. klog.Infof("Listening on %s", sockFile)
  43. server := grpc.NewServer()
  44. result := &base64Plugin{
  45. grpcServer: server,
  46. listener: listener,
  47. encryptRequest: make(chan *kmsapi.EncryptRequest, 1),
  48. }
  49. kmsapi.RegisterKeyManagementServiceServer(server, result)
  50. return result, nil
  51. }
  52. func (s *base64Plugin) cleanUp() {
  53. s.grpcServer.Stop()
  54. s.listener.Close()
  55. }
  56. var testProviderAPIVersion = kmsAPIVersion
  57. func (s *base64Plugin) Version(ctx context.Context, request *kmsapi.VersionRequest) (*kmsapi.VersionResponse, error) {
  58. return &kmsapi.VersionResponse{Version: testProviderAPIVersion, RuntimeName: "testKMS", RuntimeVersion: "0.0.1"}, nil
  59. }
  60. func (s *base64Plugin) Decrypt(ctx context.Context, request *kmsapi.DecryptRequest) (*kmsapi.DecryptResponse, error) {
  61. klog.Infof("Received Decrypt Request for DEK: %s", string(request.Cipher))
  62. buf := make([]byte, base64.StdEncoding.DecodedLen(len(request.Cipher)))
  63. n, err := base64.StdEncoding.Decode(buf, request.Cipher)
  64. if err != nil {
  65. return nil, err
  66. }
  67. return &kmsapi.DecryptResponse{Plain: buf[:n]}, nil
  68. }
  69. func (s *base64Plugin) Encrypt(ctx context.Context, request *kmsapi.EncryptRequest) (*kmsapi.EncryptResponse, error) {
  70. klog.Infof("Received Encrypt Request for DEK: %x", request.Plain)
  71. s.encryptRequest <- request
  72. buf := make([]byte, base64.StdEncoding.EncodedLen(len(request.Plain)))
  73. base64.StdEncoding.Encode(buf, request.Plain)
  74. return &kmsapi.EncryptResponse{Cipher: buf}, nil
  75. }