|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "ydbcp/internal/util/tls_setup" |
| 8 | + "ydbcp/internal/util/xlog" |
| 9 | + "ydbcp/pkg/plugins/kms" |
| 10 | + pb "ydbcp/plugins/kms_nebius/proto" |
| 11 | + |
| 12 | + "go.uber.org/zap" |
| 13 | + "google.golang.org/grpc" |
| 14 | + "gopkg.in/yaml.v3" |
| 15 | +) |
| 16 | + |
| 17 | +type kmsProviderNebius struct { |
| 18 | + config pluginConfig |
| 19 | +} |
| 20 | + |
| 21 | +type pluginConfig struct { |
| 22 | + CryptoServiceEndpoint string `yaml:"crypto_service_endpoint"` |
| 23 | + Insecure bool `yaml:"insecure" default:"false"` |
| 24 | + RootCAPath string `yaml:"root_ca_path"` |
| 25 | + ClientKeyPath string `yaml:"client_key_path"` |
| 26 | + ClientCertificatePath string `yaml:"client_certificate_path"` |
| 27 | +} |
| 28 | + |
| 29 | +func (p *kmsProviderNebius) Init(ctx context.Context, rawConfig string) error { |
| 30 | + xlog.Info(ctx, "KmsNebiusProvider initialization started", zap.String("config", rawConfig)) |
| 31 | + if err := yaml.Unmarshal([]byte(rawConfig), &p.config); err != nil { |
| 32 | + xlog.Error(ctx, "Unable to parse configuration", zap.Error(err)) |
| 33 | + return fmt.Errorf("kms: unable to parse configuration: %w", err) |
| 34 | + } |
| 35 | + if len(p.config.CryptoServiceEndpoint) == 0 { |
| 36 | + return fmt.Errorf("kms: crypto service endpoint is required in configuration") |
| 37 | + } |
| 38 | + if len(p.config.RootCAPath) == 0 { |
| 39 | + return fmt.Errorf("kms: root ca path is required in configuration") |
| 40 | + } |
| 41 | + if len(p.config.ClientKeyPath) == 0 { |
| 42 | + return fmt.Errorf("kms: client key path is required in configuration") |
| 43 | + } |
| 44 | + if len(p.config.ClientCertificatePath) == 0 { |
| 45 | + return fmt.Errorf("kms: client certificate path is required in configuration") |
| 46 | + } |
| 47 | + |
| 48 | + xlog.Info(ctx, "KmsNebiusProvider was initialized successfully") |
| 49 | + return nil |
| 50 | +} |
| 51 | + |
| 52 | +func (p *kmsProviderNebius) Close(ctx context.Context) error { |
| 53 | + xlog.Info(ctx, "KmsNebiusProvider was closed") |
| 54 | + return nil |
| 55 | +} |
| 56 | + |
| 57 | +func (p *kmsProviderNebius) Encrypt(ctx context.Context, req *kms.EncryptRequest) (*kms.EncryptResponse, error) { |
| 58 | + if req == nil { |
| 59 | + return nil, fmt.Errorf("kms: encryption request is nil") |
| 60 | + } |
| 61 | + if len(req.KeyID) == 0 { |
| 62 | + return nil, fmt.Errorf("kms: key id is required in encryption request") |
| 63 | + } |
| 64 | + |
| 65 | + tlsOption, err := tls_setup.LoadMTLSCredentials(&p.config.RootCAPath, &p.config.ClientCertificatePath, &p.config.ClientKeyPath, p.config.Insecure) |
| 66 | + if err != nil { |
| 67 | + return nil, err |
| 68 | + } |
| 69 | + |
| 70 | + grpcClient, err := grpc.NewClient("dns:"+p.config.CryptoServiceEndpoint, tlsOption) // TODO: do we need dns prefix? |
| 71 | + if err != nil { |
| 72 | + return nil, err |
| 73 | + } |
| 74 | + defer grpcClient.Close() |
| 75 | + |
| 76 | + client := pb.NewSymmetricCryptoServiceClient(grpcClient) |
| 77 | + resp, err := client.Encrypt(ctx, &pb.SymmetricEncryptRequest{ |
| 78 | + KeyId: req.KeyID, |
| 79 | + Plaintext: req.Plaintext, |
| 80 | + }) |
| 81 | + if err != nil { |
| 82 | + return nil, fmt.Errorf("kms: encryption was failed: %w", err) |
| 83 | + } |
| 84 | + |
| 85 | + return &kms.EncryptResponse{ |
| 86 | + KeyID: resp.GetKeyId(), |
| 87 | + Ciphertext: resp.GetCiphertext(), |
| 88 | + }, nil |
| 89 | +} |
| 90 | + |
| 91 | +func (p *kmsProviderNebius) Decrypt(ctx context.Context, req *kms.DecryptRequest) (*kms.DecryptResponse, error) { |
| 92 | + if req == nil { |
| 93 | + return nil, fmt.Errorf("kms: decryption request is nil") |
| 94 | + } |
| 95 | + if len(req.KeyID) == 0 { |
| 96 | + return nil, fmt.Errorf("kms: key id is required in decryption request") |
| 97 | + } |
| 98 | + |
| 99 | + tlsOption, err := tls_setup.LoadTLSCredentials(&p.config.RootCAPath, p.config.Insecure) |
| 100 | + if err != nil { |
| 101 | + return nil, err |
| 102 | + } |
| 103 | + |
| 104 | + grpcClient, err := grpc.NewClient("dns:"+p.config.CryptoServiceEndpoint, tlsOption) // TODO: do we need dns prefix? |
| 105 | + if err != nil { |
| 106 | + return nil, err |
| 107 | + } |
| 108 | + defer grpcClient.Close() |
| 109 | + |
| 110 | + client := pb.NewSymmetricCryptoServiceClient(grpcClient) |
| 111 | + resp, err := client.Decrypt(ctx, &pb.SymmetricDecryptRequest{ |
| 112 | + KeyId: req.KeyID, |
| 113 | + Ciphertext: req.Ciphertext, |
| 114 | + }) |
| 115 | + if err != nil { |
| 116 | + return nil, fmt.Errorf("kms: decryption was failed: %w", err) |
| 117 | + } |
| 118 | + |
| 119 | + return &kms.DecryptResponse{ |
| 120 | + KeyID: resp.GetKeyId(), |
| 121 | + Plaintext: resp.GetPlaintext(), |
| 122 | + }, nil |
| 123 | +} |
| 124 | + |
| 125 | +func main() {} |
| 126 | + |
| 127 | +var KmsProvider kmsProviderNebius |
0 commit comments