provider.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. Copyright © The ESO Authors
  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 awsutil
  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. // IsReferentSpec checks if the AWS authentication configuration refers to resources in a different namespace.
  47. func IsReferentSpec(prov esv1.AWSAuth) bool {
  48. if prov.JWTAuth != nil && prov.JWTAuth.ServiceAccountRef != nil && prov.JWTAuth.ServiceAccountRef.Namespace == nil {
  49. return true
  50. }
  51. if prov.SecretRef != nil &&
  52. (prov.SecretRef.AccessKeyID.Namespace == nil ||
  53. prov.SecretRef.SecretAccessKey.Namespace == nil ||
  54. (prov.SecretRef.SessionToken != nil && prov.SecretRef.SessionToken.Namespace == nil)) {
  55. return true
  56. }
  57. return false
  58. }
  59. // SecretTagsToJSONString converts AWS Secrets Manager tags to a JSON string.
  60. func SecretTagsToJSONString(tags []awssm.Tag) (string, error) {
  61. tagMap := make(map[string]string, len(tags))
  62. for _, tag := range tags {
  63. tagMap[*tag.Key] = *tag.Value
  64. }
  65. byteArr, err := json.Marshal(tagMap)
  66. if err != nil {
  67. return "", err
  68. }
  69. return string(byteArr), nil
  70. }
  71. // ParameterTagsToJSONString converts parameter tags map to a JSON string.
  72. func ParameterTagsToJSONString(tags map[string]string) (string, error) {
  73. byteArr, err := json.Marshal(tags)
  74. if err != nil {
  75. return "", err
  76. }
  77. return string(byteArr), nil
  78. }
  79. // FindTagKeysToRemove returns a slice of tag keys that exist in the current tags
  80. // but are not present in the desired metaTags. These keys should be removed to
  81. // synchronize the tags with the desired state.
  82. func FindTagKeysToRemove(tags, metaTags map[string]string) []string {
  83. var diff []string
  84. for key := range tags {
  85. if _, ok := metaTags[key]; !ok {
  86. diff = append(diff, key)
  87. }
  88. }
  89. return diff
  90. }