webhook.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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, 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, 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(provider)
  38. if err != nil {
  39. return nil, err
  40. }
  41. if err != nil {
  42. return nil, fmt.Errorf("failed to get store: %w", err)
  43. }
  44. return w.wh.GetSecretMap(ctx, provider, nil)
  45. }
  46. func parseSpec(data []byte) (*webhook.Spec, error) {
  47. var spec genv1alpha1.Webhook
  48. err := json.Unmarshal(data, &spec)
  49. if err != nil {
  50. return nil, err
  51. }
  52. out := webhook.Spec{}
  53. d, err := json.Marshal(spec.Spec)
  54. if err != nil {
  55. return nil, err
  56. }
  57. err = json.Unmarshal(d, &out)
  58. return &out, err
  59. }
  60. func init() {
  61. genv1alpha1.Register(genv1alpha1.WebhookKind, &Webhook{})
  62. }