client.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 infisical
  14. import (
  15. "context"
  16. "encoding/json"
  17. "errors"
  18. "fmt"
  19. "strings"
  20. infisical "github.com/infisical/go-sdk"
  21. "github.com/tidwall/gjson"
  22. corev1 "k8s.io/api/core/v1"
  23. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  24. "github.com/external-secrets/external-secrets/pkg/find"
  25. "github.com/external-secrets/external-secrets/pkg/metrics"
  26. "github.com/external-secrets/external-secrets/pkg/provider/infisical/constants"
  27. )
  28. var (
  29. errNotImplemented = errors.New("not implemented")
  30. errPropertyNotFound = "property %s does not exist in secret %s"
  31. errTagsNotImplemented = errors.New("find by tags not supported")
  32. )
  33. const (
  34. getSecretsV3 = "GetSecretsV3"
  35. getSecretByKeyV3 = "GetSecretByKeyV3"
  36. )
  37. func getPropertyValue(jsonData, propertyName, keyName string) ([]byte, error) {
  38. result := gjson.Get(jsonData, propertyName)
  39. if !result.Exists() {
  40. return nil, fmt.Errorf(errPropertyNotFound, propertyName, keyName)
  41. }
  42. return []byte(result.Str), nil
  43. }
  44. // getSecretAddress returns the path and key from the given key.
  45. //
  46. // Users can configure a root path, and when a SecretKey is provided with a slash we assume that it is
  47. // within a path appended to the root path.
  48. //
  49. // If the key is not addressing a path at all (i.e. has no `/`), simply return the original
  50. // path and key.
  51. func getSecretAddress(defaultPath, key string) (string, string, error) {
  52. if !strings.Contains(key, "/") {
  53. return defaultPath, key, nil
  54. }
  55. // Check if `key` starts with a `/`, and throw and error if it does not.
  56. if !strings.HasPrefix(key, "/") {
  57. return "", "", fmt.Errorf("a secret key referencing a folder must start with a '/' as it is an absolute path, key: %s", key)
  58. }
  59. // Otherwise, take the prefix from `key` and use that as the path. We intentionally discard
  60. // `defaultPath`.
  61. lastIndex := strings.LastIndex(key, "/")
  62. return key[:lastIndex], key[lastIndex+1:], nil
  63. }
  64. // GetSecret if this returns an error with type NoSecretError then the secret entry will be deleted depending on the
  65. // deletionPolicy.
  66. func (p *Provider) GetSecret(ctx context.Context, ref esv1.ExternalSecretDataRemoteRef) ([]byte, error) {
  67. path, key, err := getSecretAddress(p.apiScope.SecretPath, ref.Key)
  68. if err != nil {
  69. return nil, err
  70. }
  71. secret, err := p.sdkClient.Secrets().Retrieve(infisical.RetrieveSecretOptions{
  72. Environment: p.apiScope.EnvironmentSlug,
  73. ProjectSlug: p.apiScope.ProjectSlug,
  74. SecretKey: key,
  75. SecretPath: path,
  76. IncludeImports: true,
  77. ExpandSecretReferences: p.apiScope.ExpandSecretReferences,
  78. })
  79. metrics.ObserveAPICall(constants.ProviderName, getSecretByKeyV3, err)
  80. if err != nil {
  81. return nil, err
  82. }
  83. if ref.Property != "" {
  84. propertyValue, err := getPropertyValue(secret.SecretValue, ref.Property, ref.Key)
  85. if err != nil {
  86. return nil, err
  87. }
  88. return propertyValue, nil
  89. }
  90. return []byte(secret.SecretValue), nil
  91. }
  92. // GetSecretMap returns multiple k/v pairs from the provider.
  93. func (p *Provider) GetSecretMap(ctx context.Context, ref esv1.ExternalSecretDataRemoteRef) (map[string][]byte, error) {
  94. secret, err := p.GetSecret(ctx, ref)
  95. if err != nil {
  96. return nil, err
  97. }
  98. kv := make(map[string]json.RawMessage)
  99. err = json.Unmarshal(secret, &kv)
  100. if err != nil {
  101. return nil, fmt.Errorf("unable to unmarshal secret %s: %w", ref.Key, err)
  102. }
  103. secretData := make(map[string][]byte)
  104. for k, v := range kv {
  105. var strVal string
  106. err = json.Unmarshal(v, &strVal)
  107. if err == nil {
  108. secretData[k] = []byte(strVal)
  109. } else {
  110. secretData[k] = v
  111. }
  112. }
  113. return secretData, nil
  114. }
  115. // GetAllSecrets returns multiple k/v pairs from the provider.
  116. func (p *Provider) GetAllSecrets(ctx context.Context, ref esv1.ExternalSecretFind) (map[string][]byte, error) {
  117. if ref.Tags != nil {
  118. return nil, errTagsNotImplemented
  119. }
  120. secrets, err := p.sdkClient.Secrets().List(infisical.ListSecretsOptions{
  121. Environment: p.apiScope.EnvironmentSlug,
  122. ProjectSlug: p.apiScope.ProjectSlug,
  123. SecretPath: p.apiScope.SecretPath,
  124. Recursive: p.apiScope.Recursive,
  125. ExpandSecretReferences: p.apiScope.ExpandSecretReferences,
  126. IncludeImports: true,
  127. })
  128. metrics.ObserveAPICall(constants.ProviderName, getSecretsV3, err)
  129. if err != nil {
  130. return nil, err
  131. }
  132. secretMap := make(map[string][]byte)
  133. for _, secret := range secrets {
  134. secretMap[secret.SecretKey] = []byte(secret.SecretValue)
  135. }
  136. if ref.Name == nil && ref.Path == nil {
  137. return secretMap, nil
  138. }
  139. var matcher *find.Matcher
  140. if ref.Name != nil {
  141. m, err := find.New(*ref.Name)
  142. if err != nil {
  143. return nil, err
  144. }
  145. matcher = m
  146. }
  147. selected := map[string][]byte{}
  148. for _, secret := range secrets {
  149. if (matcher != nil && !matcher.MatchName(secret.SecretKey)) || (ref.Path != nil && !strings.HasPrefix(secret.SecretKey, *ref.Path)) {
  150. continue
  151. }
  152. selected[secret.SecretKey] = []byte(secret.SecretValue)
  153. }
  154. return selected, nil
  155. }
  156. // Validate checks if the client is configured correctly.
  157. // and is able to retrieve secrets from the provider.
  158. // If the validation result is unknown it will be ignored.
  159. func (p *Provider) Validate() (esv1.ValidationResult, error) {
  160. // try to fetch the secrets to ensure provided credentials has access to read secrets
  161. _, err := p.sdkClient.Secrets().List(infisical.ListSecretsOptions{
  162. Environment: p.apiScope.EnvironmentSlug,
  163. ProjectSlug: p.apiScope.ProjectSlug,
  164. Recursive: p.apiScope.Recursive,
  165. SecretPath: p.apiScope.SecretPath,
  166. ExpandSecretReferences: p.apiScope.ExpandSecretReferences,
  167. })
  168. metrics.ObserveAPICall(constants.ProviderName, getSecretsV3, err)
  169. if err != nil {
  170. 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)
  171. }
  172. return esv1.ValidationResultReady, nil
  173. }
  174. // PushSecret will write a single secret into the provider.
  175. func (p *Provider) PushSecret(ctx context.Context, secret *corev1.Secret, data esv1.PushSecretData) error {
  176. return errNotImplemented
  177. }
  178. // DeleteSecret will delete the secret from a provider.
  179. func (p *Provider) DeleteSecret(ctx context.Context, remoteRef esv1.PushSecretRemoteRef) error {
  180. return errNotImplemented
  181. }
  182. // SecretExists checks if a secret is already present in the provider at the given location.
  183. func (p *Provider) SecretExists(ctx context.Context, remoteRef esv1.PushSecretRemoteRef) (bool, error) {
  184. return false, errNotImplemented
  185. }