push_secret.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 secretmanager
  13. import (
  14. "bytes"
  15. "encoding/json"
  16. "errors"
  17. "fmt"
  18. "github.com/tidwall/sjson"
  19. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  20. )
  21. type Metadata struct {
  22. Annotations map[string]string `json:"annotations"`
  23. Labels map[string]string `json:"labels"`
  24. }
  25. func newPushSecretBuilder(payload []byte, data esv1beta1.PushSecretData) (pushSecretBuilder, error) {
  26. if data.GetProperty() == "" {
  27. return &psBuilder{
  28. payload: payload,
  29. pushSecretData: data,
  30. }, nil
  31. }
  32. if data.GetMetadata() != nil {
  33. return nil, errors.New("cannot specify metadata and property at the same time")
  34. }
  35. return &propertyPSBuilder{
  36. payload: payload,
  37. pushSecretData: data,
  38. }, nil
  39. }
  40. type pushSecretBuilder interface {
  41. buildMetadata(annotations, labels map[string]string) (map[string]string, map[string]string, error)
  42. needUpdate(original []byte) bool
  43. buildData(original []byte) ([]byte, error)
  44. }
  45. type psBuilder struct {
  46. payload []byte
  47. pushSecretData esv1beta1.PushSecretData
  48. }
  49. func (b *psBuilder) buildMetadata(_, labels map[string]string) (map[string]string, map[string]string, error) {
  50. if manager, ok := labels[managedByKey]; !ok || manager != managedByValue {
  51. return nil, nil, fmt.Errorf("secret %v is not managed by external secrets", b.pushSecretData.GetRemoteKey())
  52. }
  53. var metadata Metadata
  54. if b.pushSecretData.GetMetadata() != nil {
  55. decoder := json.NewDecoder(bytes.NewReader(b.pushSecretData.GetMetadata().Raw))
  56. // Want to return an error if unknown fields exist
  57. decoder.DisallowUnknownFields()
  58. if err := decoder.Decode(&metadata); err != nil {
  59. return nil, nil, fmt.Errorf("failed to decode PushSecret metadata: %w", err)
  60. }
  61. }
  62. newLabels := map[string]string{}
  63. if metadata.Labels != nil {
  64. newLabels = metadata.Labels
  65. }
  66. newLabels[managedByKey] = managedByValue
  67. return metadata.Annotations, newLabels, nil
  68. }
  69. func (b *psBuilder) needUpdate(original []byte) bool {
  70. if original == nil {
  71. return true
  72. }
  73. return !bytes.Equal(b.payload, original)
  74. }
  75. func (b *psBuilder) buildData(_ []byte) ([]byte, error) {
  76. return b.payload, nil
  77. }
  78. type propertyPSBuilder struct {
  79. payload []byte
  80. pushSecretData esv1beta1.PushSecretData
  81. }
  82. func (b *propertyPSBuilder) buildMetadata(annotations, labels map[string]string) (map[string]string, map[string]string, error) {
  83. newAnnotations := map[string]string{}
  84. newLabels := map[string]string{}
  85. if annotations != nil {
  86. newAnnotations = annotations
  87. }
  88. if labels != nil {
  89. newLabels = labels
  90. }
  91. newLabels[managedByKey] = managedByValue
  92. return newAnnotations, newLabels, nil
  93. }
  94. func (b *propertyPSBuilder) needUpdate(original []byte) bool {
  95. if original == nil {
  96. return true
  97. }
  98. val := getDataByProperty(original, b.pushSecretData.GetProperty())
  99. return !val.Exists() || val.String() != string(b.payload)
  100. }
  101. func (b *propertyPSBuilder) buildData(original []byte) ([]byte, error) {
  102. var base []byte
  103. if original != nil {
  104. base = original
  105. }
  106. return sjson.SetBytes(base, b.pushSecretData.GetProperty(), b.payload)
  107. }