webhook.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  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 webhook provides functionality for generating secrets through webhook calls.
  14. package webhook
  15. import (
  16. "context"
  17. "encoding/json"
  18. "fmt"
  19. apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  20. "sigs.k8s.io/controller-runtime/pkg/client"
  21. genv1alpha1 "github.com/external-secrets/external-secrets/apis/generators/v1alpha1"
  22. "github.com/external-secrets/external-secrets/providers/v1/webhook/pkg/webhook"
  23. )
  24. // Webhook represents a generator that calls external webhooks to generate secrets.
  25. type Webhook struct {
  26. wh webhook.Webhook
  27. url string
  28. }
  29. // Generate creates secrets by making webhook calls to external services.
  30. func (w *Webhook) Generate(ctx context.Context, jsonSpec *apiextensions.JSON, kclient client.Client, ns string) (map[string][]byte, genv1alpha1.GeneratorProviderState, error) {
  31. w.wh = webhook.Webhook{}
  32. w.wh.EnforceLabels = true
  33. w.wh.ClusterScoped = false
  34. provider, err := parseSpec(jsonSpec.Raw)
  35. if err != nil {
  36. return nil, nil, fmt.Errorf("failed to parse provider spec: %w", err)
  37. }
  38. w.wh.Namespace = ns
  39. w.url = provider.URL
  40. w.wh.Kube = kclient
  41. w.wh.HTTP, err = w.wh.GetHTTPClient(ctx, provider)
  42. if err != nil {
  43. return nil, nil, fmt.Errorf("failed to prepare provider http client: %w", err)
  44. }
  45. data, err := w.wh.GetSecretMap(ctx, provider, nil)
  46. return data, nil, err
  47. }
  48. // Cleanup performs any necessary cleanup operations after secret generation.
  49. func (w *Webhook) Cleanup(_ context.Context, _ *apiextensions.JSON, _ genv1alpha1.GeneratorProviderState, _ client.Client, _ string) error {
  50. return nil
  51. }
  52. func parseSpec(data []byte) (*webhook.Spec, error) {
  53. var spec genv1alpha1.Webhook
  54. err := json.Unmarshal(data, &spec)
  55. if err != nil {
  56. return nil, err
  57. }
  58. out := webhook.Spec{}
  59. d, err := json.Marshal(spec.Spec)
  60. if err != nil {
  61. return nil, err
  62. }
  63. err = json.Unmarshal(d, &out)
  64. return &out, err
  65. }
  66. // NewGenerator creates a new Generator instance.
  67. func NewGenerator() genv1alpha1.Generator {
  68. return &Webhook{}
  69. }
  70. // Kind returns the generator kind.
  71. func Kind() string {
  72. return string(genv1alpha1.GeneratorKindWebhook)
  73. }