webhook.go 2.1 KB

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