server.go 4.3 KB

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