| 1 |
/* |
| 2 |
Copyright 2023 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 service |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"net" |
| 22 |
"time" |
| 23 |
|
| 24 |
"google.golang.org/grpc" |
| 25 |
|
| 26 |
kmsapi "k8s.io/kms/apis/v2" |
| 27 |
) |
| 28 |
|
| 29 |
// GRPCService is a grpc server that runs the kms v2 alpha1 API. |
| 30 |
type GRPCService struct { |
| 31 |
addr string |
| 32 |
timeout time.Duration |
| 33 |
server *grpc.Server |
| 34 |
|
| 35 |
kmsService Service |
| 36 |
kmsapi.UnsafeKeyManagementServiceServer |
| 37 |
} |
| 38 |
|
| 39 |
var _ kmsapi.KeyManagementServiceServer = (*GRPCService)(nil) |
| 40 |
|
| 41 |
// NewGRPCService creates an instance of GRPCService. |
| 42 |
func NewGRPCService( |
| 43 |
address string, |
| 44 |
timeout time.Duration, |
| 45 |
|
| 46 |
kmsService Service, |
| 47 |
) *GRPCService { |
| 48 |
return &GRPCService{ |
| 49 |
addr: address, |
| 50 |
timeout: timeout, |
| 51 |
kmsService: kmsService, |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
// ListenAndServe accepts incoming connections on a Unix socket. It is a blocking method. |
| 56 |
// Returns non-nil error unless Close or Shutdown is called. |
| 57 |
func (s *GRPCService) ListenAndServe() error { |
| 58 |
ln, err := net.Listen("unix", s.addr) |
| 59 |
if err != nil { |
| 60 |
return err |
| 61 |
} |
| 62 |
defer ln.Close() |
| 63 |
|
| 64 |
gs := grpc.NewServer( |
| 65 |
grpc.ConnectionTimeout(s.timeout), |
| 66 |
) |
| 67 |
s.server = gs |
| 68 |
|
| 69 |
kmsapi.RegisterKeyManagementServiceServer(gs, s) |
| 70 |
|
| 71 |
return gs.Serve(ln) |
| 72 |
} |
| 73 |
|
| 74 |
// Shutdown performs a graceful shutdown. Doesn't accept new connections and |
| 75 |
// blocks until all pending RPCs are finished. |
| 76 |
func (s *GRPCService) Shutdown() { |
| 77 |
if s.server != nil { |
| 78 |
s.server.GracefulStop() |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
// Close stops the server by closing all connections immediately and cancels |
| 83 |
// all active RPCs. |
| 84 |
func (s *GRPCService) Close() { |
| 85 |
if s.server != nil { |
| 86 |
s.server.Stop() |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
// Status sends a status request to specified kms service. |
| 91 |
func (s *GRPCService) Status(ctx context.Context, _ *kmsapi.StatusRequest) (*kmsapi.StatusResponse, error) { |
| 92 |
res, err := s.kmsService.Status(ctx) |
| 93 |
if err != nil { |
| 94 |
return nil, err |
| 95 |
} |
| 96 |
|
| 97 |
return &kmsapi.StatusResponse{ |
| 98 |
Version: res.Version, |
| 99 |
Healthz: res.Healthz, |
| 100 |
KeyId: res.KeyID, |
| 101 |
}, nil |
| 102 |
} |
| 103 |
|
| 104 |
// Decrypt sends a decryption request to specified kms service. |
| 105 |
func (s *GRPCService) Decrypt(ctx context.Context, req *kmsapi.DecryptRequest) (*kmsapi.DecryptResponse, error) { |
| 106 |
plaintext, err := s.kmsService.Decrypt(ctx, req.Uid, &DecryptRequest{ |
| 107 |
Ciphertext: req.Ciphertext, |
| 108 |
KeyID: req.KeyId, |
| 109 |
Annotations: req.Annotations, |
| 110 |
}) |
| 111 |
if err != nil { |
| 112 |
return nil, err |
| 113 |
} |
| 114 |
|
| 115 |
return &kmsapi.DecryptResponse{ |
| 116 |
Plaintext: plaintext, |
| 117 |
}, nil |
| 118 |
} |
| 119 |
|
| 120 |
// Encrypt sends an encryption request to specified kms service. |
| 121 |
func (s *GRPCService) Encrypt(ctx context.Context, req *kmsapi.EncryptRequest) (*kmsapi.EncryptResponse, error) { |
| 122 |
encRes, err := s.kmsService.Encrypt(ctx, req.Uid, req.Plaintext) |
| 123 |
if err != nil { |
| 124 |
return nil, err |
| 125 |
} |
| 126 |
|
| 127 |
return &kmsapi.EncryptResponse{ |
| 128 |
Ciphertext: encRes.Ciphertext, |
| 129 |
KeyId: encRes.KeyID, |
| 130 |
Annotations: encRes.Annotations, |
| 131 |
}, nil |
| 132 |
} |