webhook.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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
  14. import (
  15. "context"
  16. "encoding/json"
  17. "fmt"
  18. apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  19. "sigs.k8s.io/controller-runtime/pkg/client"
  20. genv1alpha1 "github.com/external-secrets/external-secrets/apis/generators/v1alpha1"
  21. "github.com/external-secrets/external-secrets/pkg/common/webhook"
  22. )
  23. type Webhook struct {
  24. wh webhook.Webhook
  25. url string
  26. }
  27. func (w *Webhook) Generate(ctx context.Context, jsonSpec *apiextensions.JSON, kclient client.Client, ns string) (map[string][]byte, genv1alpha1.GeneratorProviderState, error) {
  28. w.wh.EnforceLabels = true
  29. w.wh.ClusterScoped = false
  30. provider, err := parseSpec(jsonSpec.Raw)
  31. w.wh = webhook.Webhook{}
  32. if err != nil {
  33. return nil, nil, fmt.Errorf("failed to parse provider spec: %w", err)
  34. }
  35. w.wh.Namespace = ns
  36. w.url = provider.URL
  37. w.wh.Kube = kclient
  38. w.wh.HTTP, err = w.wh.GetHTTPClient(ctx, provider)
  39. if err != nil {
  40. return nil, nil, fmt.Errorf("failed to prepare provider http client: %w", err)
  41. }
  42. data, err := w.wh.GetSecretMap(ctx, provider, nil)
  43. return data, nil, err
  44. }
  45. func (w *Webhook) Cleanup(_ context.Context, jsonSpec *apiextensions.JSON, state genv1alpha1.GeneratorProviderState, _ client.Client, _ string) error {
  46. return nil
  47. }
  48. func parseSpec(data []byte) (*webhook.Spec, error) {
  49. var spec genv1alpha1.Webhook
  50. err := json.Unmarshal(data, &spec)
  51. if err != nil {
  52. return nil, err
  53. }
  54. out := webhook.Spec{}
  55. d, err := json.Marshal(spec.Spec)
  56. if err != nil {
  57. return nil, err
  58. }
  59. err = json.Unmarshal(d, &out)
  60. return &out, err
  61. }
  62. func init() {
  63. genv1alpha1.Register(genv1alpha1.WebhookKind, &Webhook{})
  64. }