provider.go 2.8 KB

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