client.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 impliec.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package infisical
  13. import (
  14. "context"
  15. "encoding/json"
  16. "errors"
  17. "fmt"
  18. "strings"
  19. "github.com/tidwall/gjson"
  20. corev1 "k8s.io/api/core/v1"
  21. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  22. "github.com/external-secrets/external-secrets/pkg/find"
  23. "github.com/external-secrets/external-secrets/pkg/provider/infisical/api"
  24. )
  25. var (
  26. errNotImplemented = errors.New("not implemented")
  27. errPropertyNotFound = "property %s does not exist in secret %s"
  28. errTagsNotImplemented = errors.New("find by tags not supported")
  29. )
  30. func getPropertyValue(jsonData, propertyName, keyName string) ([]byte, error) {
  31. result := gjson.Get(jsonData, propertyName)
  32. if !result.Exists() {
  33. return nil, fmt.Errorf(errPropertyNotFound, propertyName, keyName)
  34. }
  35. return []byte(result.Str), nil
  36. }
  37. // if GetSecret returns an error with type NoSecretError.
  38. // then the secret entry will be deleted depending on the deletionPolicy.
  39. func (p *Provider) GetSecret(ctx context.Context, ref esv1.ExternalSecretDataRemoteRef) ([]byte, error) {
  40. secret, err := p.apiClient.GetSecretByKeyV3(api.GetSecretByKeyV3Request{
  41. EnvironmentSlug: p.apiScope.EnvironmentSlug,
  42. ProjectSlug: p.apiScope.ProjectSlug,
  43. SecretKey: ref.Key,
  44. SecretPath: p.apiScope.SecretPath,
  45. ExpandSecretReferences: p.apiScope.ExpandSecretReferences,
  46. })
  47. if err != nil {
  48. return nil, err
  49. }
  50. if ref.Property != "" {
  51. propertyValue, err := getPropertyValue(secret, ref.Property, ref.Key)
  52. if err != nil {
  53. return nil, err
  54. }
  55. return propertyValue, nil
  56. }
  57. return []byte(secret), nil
  58. }
  59. // GetSecretMap returns multiple k/v pairs from the provider.
  60. func (p *Provider) GetSecretMap(ctx context.Context, ref esv1.ExternalSecretDataRemoteRef) (map[string][]byte, error) {
  61. secret, err := p.GetSecret(ctx, ref)
  62. if err != nil {
  63. return nil, err
  64. }
  65. kv := make(map[string]json.RawMessage)
  66. err = json.Unmarshal(secret, &kv)
  67. if err != nil {
  68. return nil, fmt.Errorf("unable to unmarshal secret %s: %w", ref.Key, err)
  69. }
  70. secretData := make(map[string][]byte)
  71. for k, v := range kv {
  72. var strVal string
  73. err = json.Unmarshal(v, &strVal)
  74. if err == nil {
  75. secretData[k] = []byte(strVal)
  76. } else {
  77. secretData[k] = v
  78. }
  79. }
  80. return secretData, nil
  81. }
  82. // GetAllSecrets returns multiple k/v pairs from the provider.
  83. func (p *Provider) GetAllSecrets(ctx context.Context, ref esv1.ExternalSecretFind) (map[string][]byte, error) {
  84. if ref.Tags != nil {
  85. return nil, errTagsNotImplemented
  86. }
  87. secrets, err := p.apiClient.GetSecretsV3(api.GetSecretsV3Request{
  88. EnvironmentSlug: p.apiScope.EnvironmentSlug,
  89. ProjectSlug: p.apiScope.ProjectSlug,
  90. SecretPath: p.apiScope.SecretPath,
  91. Recursive: p.apiScope.Recursive,
  92. ExpandSecretReferences: p.apiScope.ExpandSecretReferences,
  93. })
  94. if err != nil {
  95. return nil, err
  96. }
  97. secretMap := make(map[string][]byte)
  98. for key, value := range secrets {
  99. secretMap[key] = []byte(value)
  100. }
  101. if ref.Name == nil && ref.Path == nil {
  102. return secretMap, nil
  103. }
  104. var matcher *find.Matcher
  105. if ref.Name != nil {
  106. m, err := find.New(*ref.Name)
  107. if err != nil {
  108. return nil, err
  109. }
  110. matcher = m
  111. }
  112. selected := map[string][]byte{}
  113. for key, value := range secrets {
  114. if (matcher != nil && !matcher.MatchName(key)) || (ref.Path != nil && !strings.HasPrefix(key, *ref.Path)) {
  115. continue
  116. }
  117. selected[key] = []byte(value)
  118. }
  119. return selected, nil
  120. }
  121. // Validate checks if the client is configured correctly.
  122. // and is able to retrieve secrets from the provider.
  123. // If the validation result is unknown it will be ignored.
  124. func (p *Provider) Validate() (esv1.ValidationResult, error) {
  125. // try to fetch the secrets to ensure provided credentials has access to read secrets
  126. _, err := p.apiClient.GetSecretsV3(api.GetSecretsV3Request{
  127. EnvironmentSlug: p.apiScope.EnvironmentSlug,
  128. ProjectSlug: p.apiScope.ProjectSlug,
  129. Recursive: p.apiScope.Recursive,
  130. SecretPath: p.apiScope.SecretPath,
  131. ExpandSecretReferences: p.apiScope.ExpandSecretReferences,
  132. })
  133. if err != nil {
  134. return esv1.ValidationResultError, fmt.Errorf("cannot read secrets with provided project scope project:%s environment:%s secret-path:%s recursive:%t, %w", p.apiScope.ProjectSlug, p.apiScope.EnvironmentSlug, p.apiScope.SecretPath, p.apiScope.Recursive, err)
  135. }
  136. return esv1.ValidationResultReady, nil
  137. }
  138. // PushSecret will write a single secret into the provider.
  139. func (p *Provider) PushSecret(ctx context.Context, secret *corev1.Secret, data esv1.PushSecretData) error {
  140. return errNotImplemented
  141. }
  142. // DeleteSecret will delete the secret from a provider.
  143. func (p *Provider) DeleteSecret(ctx context.Context, remoteRef esv1.PushSecretRemoteRef) error {
  144. return errNotImplemented
  145. }
  146. // SecretExists checks if a secret is already present in the provider at the given location.
  147. func (p *Provider) SecretExists(ctx context.Context, remoteRef esv1.PushSecretRemoteRef) (bool, error) {
  148. return false, errNotImplemented
  149. }