Debian Codegraph @

internal/plugins/_mock/plugin.go

raw
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 main
18
19 import (
20 "context"
21 "flag"
22 "os"
23 "os/signal"
24 "syscall"
25 "time"
26
27 "k8s.io/kms/pkg/service"
28 "k8s.io/kms/pkg/util"
29 "k8s.io/kms/plugins/mock/pkcs11"
30 )
31
32 var (
33 listenAddr = flag.String("listen-addr", "unix:///tmp/kms.socket", "gRPC listen address")
34 timeout = flag.Duration("timeout", 5*time.Second, "gRPC timeout")
35 configFilePath = flag.String("config-file-path", "/etc/softhsm-config.json", "SoftHSM config file path")
36 )
37
38 func main() {
39 flag.Parse()
40
41 addr, err := util.ParseEndpoint(*listenAddr)
42 if err != nil {
43 panic("failed to parse endpoint: " + err.Error())
44 }
45
46 remoteKMSService, err := pkcs11.NewPKCS11RemoteService(*configFilePath, "kms-test")
47 if err != nil {
48 panic("failed to create remote service: " + err.Error())
49 }
50
51 ctx := withShutdownSignal(context.Background())
52 grpcService := service.NewGRPCService(
53 addr,
54 *timeout,
55 remoteKMSService,
56 )
57
58 go func() {
59 if err := grpcService.ListenAndServe(); err != nil {
60 panic("failed to serve: " + err.Error())
61 }
62 }()
63
64 <-ctx.Done()
65 grpcService.Shutdown()
66 }
67
68 // withShutdownSignal returns a copy of the parent context that will close if
69 // the process receives termination signals.
70 func withShutdownSignal(ctx context.Context) context.Context {
71 signalChan := make(chan os.Signal, 1)
72 signal.Notify(signalChan, syscall.SIGTERM, syscall.SIGINT, os.Interrupt)
73
74 nctx, cancel := context.WithCancel(ctx)
75
76 go func() {
77 <-signalChan
78 cancel()
79 }()
80 return nctx
81 }