provider.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 util
  14. import (
  15. "encoding/json"
  16. "errors"
  17. "fmt"
  18. awssm "github.com/aws/aws-sdk-go-v2/service/secretsmanager/types"
  19. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  20. )
  21. const (
  22. errNilStore = "found nil store"
  23. errMissingStoreSpec = "store is missing spec"
  24. errMissingProvider = "storeSpec is missing provider"
  25. errInvalidProvider = "invalid provider spec. Missing AWS field in store %s"
  26. )
  27. // GetAWSProvider does the necessary nil checks on the generic store
  28. // it returns the aws provider or an error.
  29. func GetAWSProvider(store esv1.GenericStore) (*esv1.AWSProvider, error) {
  30. if store == nil {
  31. return nil, errors.New(errNilStore)
  32. }
  33. spc := store.GetSpec()
  34. if spc == nil {
  35. return nil, errors.New(errMissingStoreSpec)
  36. }
  37. if spc.Provider == nil {
  38. return nil, errors.New(errMissingProvider)
  39. }
  40. prov := spc.Provider.AWS
  41. if prov == nil {
  42. return nil, fmt.Errorf(errInvalidProvider, store.GetObjectMeta().String())
  43. }
  44. return prov, nil
  45. }
  46. func IsReferentSpec(prov esv1.AWSAuth) bool {
  47. if prov.JWTAuth != nil && prov.JWTAuth.ServiceAccountRef != nil && prov.JWTAuth.ServiceAccountRef.Namespace == nil {
  48. return true
  49. }
  50. if prov.SecretRef != nil &&
  51. (prov.SecretRef.AccessKeyID.Namespace == nil ||
  52. prov.SecretRef.SecretAccessKey.Namespace == nil ||
  53. (prov.SecretRef.SessionToken != nil && prov.SecretRef.SessionToken.Namespace == nil)) {
  54. return true
  55. }
  56. return false
  57. }
  58. func SecretTagsToJSONString(tags []awssm.Tag) (string, error) {
  59. tagMap := make(map[string]string, len(tags))
  60. for _, tag := range tags {
  61. tagMap[*tag.Key] = *tag.Value
  62. }
  63. byteArr, err := json.Marshal(tagMap)
  64. if err != nil {
  65. return "", err
  66. }
  67. return string(byteArr), nil
  68. }
  69. func ParameterTagsToJSONString(tags map[string]string) (string, error) {
  70. byteArr, err := json.Marshal(tags)
  71. if err != nil {
  72. return "", err
  73. }
  74. return string(byteArr), nil
  75. }
  76. // FindTagKeysToRemove returns a slice of tag keys that exist in the current tags
  77. // but are not present in the desired metaTags. These keys should be removed to
  78. // synchronize the tags with the desired state.
  79. func FindTagKeysToRemove(tags, metaTags map[string]string) []string {
  80. var diff []string
  81. for key, _ := range tags {
  82. if _, ok := metaTags[key]; !ok {
  83. diff = append(diff, key)
  84. }
  85. }
  86. return diff
  87. }