server.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. Copyright © The ESO Authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Package adapter provides a unified server that wraps v1 providers and generators for v2 gRPC services.
  14. package adapter
  15. import (
  16. "context"
  17. "k8s.io/apimachinery/pkg/runtime"
  18. "sigs.k8s.io/controller-runtime/pkg/client"
  19. genpb "github.com/external-secrets/external-secrets/proto/generator"
  20. pb "github.com/external-secrets/external-secrets/proto/provider"
  21. "github.com/external-secrets/external-secrets/providers/v2/adapter/generator"
  22. "github.com/external-secrets/external-secrets/providers/v2/adapter/store"
  23. )
  24. // Server is a unified gRPC server that implements both SecretStoreProvider and GeneratorProvider.
  25. // It embeds both the store and generator servers to provide a single implementation.
  26. type Server struct {
  27. pb.UnimplementedSecretStoreProviderServer
  28. genpb.UnimplementedGeneratorProviderServer
  29. storeServer *store.Server
  30. generatorServer *generator.Server
  31. }
  32. // NewServer creates a new unified adapter server that wraps v1 providers and generators.
  33. // It combines both store and generator functionality into a single gRPC server.
  34. func NewServer(
  35. kubeClient client.Client,
  36. scheme *runtime.Scheme,
  37. providerMapping store.ProviderMapping,
  38. specMapper store.SpecMapper,
  39. generatorMapping generator.Mapping,
  40. ) *Server {
  41. return &Server{
  42. storeServer: store.NewServer(kubeClient, providerMapping, specMapper),
  43. generatorServer: generator.NewServer(kubeClient, scheme, generatorMapping),
  44. }
  45. }
  46. // Ensure Server implements both interfaces.
  47. var _ pb.SecretStoreProviderServer = (*Server)(nil)
  48. var _ genpb.GeneratorProviderServer = (*Server)(nil)
  49. // Store methods - delegated to store.Server
  50. // GetSecret retrieves a single secret from the provider.
  51. func (s *Server) GetSecret(ctx context.Context, req *pb.GetSecretRequest) (*pb.GetSecretResponse, error) {
  52. return s.storeServer.GetSecret(ctx, req)
  53. }
  54. // GetSecretMap retrieves multiple key/value pairs from a single secret object.
  55. func (s *Server) GetSecretMap(ctx context.Context, req *pb.GetSecretMapRequest) (*pb.GetSecretMapResponse, error) {
  56. return s.storeServer.GetSecretMap(ctx, req)
  57. }
  58. // PushSecret pushes a secret to the provider.
  59. func (s *Server) PushSecret(ctx context.Context, req *pb.PushSecretRequest) (*pb.PushSecretResponse, error) {
  60. return s.storeServer.PushSecret(ctx, req)
  61. }
  62. // DeleteSecret deletes a secret from the provider.
  63. func (s *Server) DeleteSecret(ctx context.Context, req *pb.DeleteSecretRequest) (*pb.DeleteSecretResponse, error) {
  64. return s.storeServer.DeleteSecret(ctx, req)
  65. }
  66. // SecretExists checks if a secret exists in the provider.
  67. func (s *Server) SecretExists(ctx context.Context, req *pb.SecretExistsRequest) (*pb.SecretExistsResponse, error) {
  68. return s.storeServer.SecretExists(ctx, req)
  69. }
  70. // GetAllSecrets retrieves multiple secrets from the provider.
  71. func (s *Server) GetAllSecrets(ctx context.Context, req *pb.GetAllSecretsRequest) (*pb.GetAllSecretsResponse, error) {
  72. return s.storeServer.GetAllSecrets(ctx, req)
  73. }
  74. // Validate validates the provider configuration.
  75. func (s *Server) Validate(ctx context.Context, req *pb.ValidateRequest) (*pb.ValidateResponse, error) {
  76. return s.storeServer.Validate(ctx, req)
  77. }
  78. // Capabilities returns the capabilities of the provider.
  79. func (s *Server) Capabilities(ctx context.Context, req *pb.CapabilitiesRequest) (*pb.CapabilitiesResponse, error) {
  80. return s.storeServer.Capabilities(ctx, req)
  81. }
  82. // Generator methods - delegated to generator.Server
  83. // Generate generates a new secret value.
  84. func (s *Server) Generate(ctx context.Context, req *genpb.GenerateRequest) (*genpb.GenerateResponse, error) {
  85. return s.generatorServer.Generate(ctx, req)
  86. }
  87. // Cleanup performs cleanup operations for the generator.
  88. func (s *Server) Cleanup(ctx context.Context, req *genpb.CleanupRequest) (*genpb.CleanupResponse, error) {
  89. return s.generatorServer.Cleanup(ctx, req)
  90. }