client_manager.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 secretstore
  14. import (
  15. "context"
  16. "errors"
  17. "fmt"
  18. "regexp"
  19. "strings"
  20. "github.com/go-logr/logr"
  21. v1 "k8s.io/api/core/v1"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/apimachinery/pkg/labels"
  24. "k8s.io/apimachinery/pkg/types"
  25. ctrl "sigs.k8s.io/controller-runtime"
  26. "sigs.k8s.io/controller-runtime/pkg/client"
  27. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  28. )
  29. const (
  30. errGetClusterSecretStore = "could not get ClusterSecretStore %q, %w"
  31. errGetSecretStore = "could not get SecretStore %q, %w"
  32. errSecretStoreNotReady = "%s %q is not ready"
  33. errClusterStoreMismatch = "using cluster store %q is not allowed from namespace %q: denied by spec.condition"
  34. )
  35. // Manager stores instances of provider clients
  36. // At any given time we must have no more than one instance
  37. // of a client (due to limitations in GCP / see mutexlock there)
  38. // If the controller requests another instance of a given client
  39. // we will close the old client first and then construct a new one.
  40. type Manager struct {
  41. log logr.Logger
  42. client client.Client
  43. controllerClass string
  44. enableFloodgate bool
  45. // store clients by provider type
  46. clientMap map[clientKey]*clientVal
  47. }
  48. type clientKey struct {
  49. providerType string
  50. }
  51. type clientVal struct {
  52. client esv1.SecretsClient
  53. store esv1.GenericStore
  54. }
  55. // NewManager constructs a new manager with defaults.
  56. func NewManager(ctrlClient client.Client, controllerClass string, enableFloodgate bool) *Manager {
  57. log := ctrl.Log.WithName("clientmanager")
  58. return &Manager{
  59. log: log,
  60. client: ctrlClient,
  61. controllerClass: controllerClass,
  62. enableFloodgate: enableFloodgate,
  63. clientMap: make(map[clientKey]*clientVal),
  64. }
  65. }
  66. func (m *Manager) GetFromStore(ctx context.Context, store esv1.GenericStore, namespace string) (esv1.SecretsClient, error) {
  67. storeProvider, err := esv1.GetProvider(store)
  68. if err != nil {
  69. return nil, err
  70. }
  71. secretClient := m.getStoredClient(ctx, storeProvider, store)
  72. if secretClient != nil {
  73. return secretClient, nil
  74. }
  75. m.log.V(1).Info("creating new client",
  76. "provider", fmt.Sprintf("%T", storeProvider),
  77. "store", fmt.Sprintf("%s/%s", store.GetNamespace(), store.GetName()))
  78. // secret client is created only if we are going to refresh
  79. // this skip an unnecessary check/request in the case we are not going to do anything
  80. secretClient, err = storeProvider.NewClient(ctx, store, m.client, namespace)
  81. if err != nil {
  82. return nil, err
  83. }
  84. idx := storeKey(storeProvider)
  85. m.clientMap[idx] = &clientVal{
  86. client: secretClient,
  87. store: store,
  88. }
  89. return secretClient, nil
  90. }
  91. // Get returns a provider client from the given storeRef or sourceRef.secretStoreRef
  92. // while sourceRef.SecretStoreRef takes precedence over storeRef.
  93. // Do not close the client returned from this func, instead close
  94. // the manager once you're done with recinciling the external secret.
  95. func (m *Manager) Get(ctx context.Context, storeRef esv1.SecretStoreRef, namespace string, sourceRef *esv1.StoreGeneratorSourceRef) (esv1.SecretsClient, error) {
  96. if sourceRef != nil && sourceRef.SecretStoreRef != nil {
  97. storeRef = *sourceRef.SecretStoreRef
  98. }
  99. store, err := m.getStore(ctx, &storeRef, namespace)
  100. if err != nil {
  101. return nil, err
  102. }
  103. // check if store should be handled by this controller instance
  104. if !ShouldProcessStore(store, m.controllerClass) {
  105. return nil, errors.New("can not reference unmanaged store")
  106. }
  107. // when using ClusterSecretStore, validate the ClusterSecretStore namespace conditions
  108. shouldProcess, err := m.shouldProcessSecret(store, namespace)
  109. if err != nil {
  110. return nil, err
  111. }
  112. if !shouldProcess {
  113. return nil, fmt.Errorf(errClusterStoreMismatch, store.GetName(), namespace)
  114. }
  115. if m.enableFloodgate {
  116. err := assertStoreIsUsable(store)
  117. if err != nil {
  118. return nil, err
  119. }
  120. }
  121. return m.GetFromStore(ctx, store, namespace)
  122. }
  123. // returns a previously stored client from the cache if store and store-version match
  124. // if a client exists for the same provider which points to a different store or store version
  125. // it will be cleaned up.
  126. func (m *Manager) getStoredClient(ctx context.Context, storeProvider esv1.Provider, store esv1.GenericStore) esv1.SecretsClient {
  127. idx := storeKey(storeProvider)
  128. val, ok := m.clientMap[idx]
  129. if !ok {
  130. return nil
  131. }
  132. valGVK, err := m.client.GroupVersionKindFor(val.store)
  133. if err != nil {
  134. return nil
  135. }
  136. storeGVK, err := m.client.GroupVersionKindFor(store)
  137. if err != nil {
  138. return nil
  139. }
  140. storeName := fmt.Sprintf("%s/%s", store.GetNamespace(), store.GetName())
  141. // return client if it points to the very same store
  142. if val.store.GetObjectMeta().Generation == store.GetGeneration() &&
  143. valGVK == storeGVK &&
  144. val.store.GetName() == store.GetName() &&
  145. val.store.GetNamespace() == store.GetNamespace() {
  146. m.log.V(1).Info("reusing stored client",
  147. "provider", fmt.Sprintf("%T", storeProvider),
  148. "store", storeName)
  149. return val.client
  150. }
  151. m.log.V(1).Info("cleaning up client",
  152. "provider", fmt.Sprintf("%T", storeProvider),
  153. "store", storeName)
  154. // if we have a client, but it points to a different store
  155. // we must clean it up
  156. _ = val.client.Close(ctx)
  157. delete(m.clientMap, idx)
  158. return nil
  159. }
  160. func storeKey(storeProvider esv1.Provider) clientKey {
  161. return clientKey{
  162. providerType: fmt.Sprintf("%T", storeProvider),
  163. }
  164. }
  165. // getStore fetches the (Cluster)SecretStore from the kube-apiserver
  166. // and returns a GenericStore representing it.
  167. func (m *Manager) getStore(ctx context.Context, storeRef *esv1.SecretStoreRef, namespace string) (esv1.GenericStore, error) {
  168. ref := types.NamespacedName{
  169. Name: storeRef.Name,
  170. }
  171. if storeRef.Kind == esv1.ClusterSecretStoreKind {
  172. var store esv1.ClusterSecretStore
  173. err := m.client.Get(ctx, ref, &store)
  174. if err != nil {
  175. return nil, fmt.Errorf(errGetClusterSecretStore, ref.Name, err)
  176. }
  177. return &store, nil
  178. }
  179. ref.Namespace = namespace
  180. var store esv1.SecretStore
  181. err := m.client.Get(ctx, ref, &store)
  182. if err != nil {
  183. return nil, fmt.Errorf(errGetSecretStore, ref.Name, err)
  184. }
  185. return &store, nil
  186. }
  187. // Close cleans up all clients.
  188. func (m *Manager) Close(ctx context.Context) error {
  189. var errs []string
  190. for key, val := range m.clientMap {
  191. err := val.client.Close(ctx)
  192. if err != nil {
  193. errs = append(errs, err.Error())
  194. }
  195. delete(m.clientMap, key)
  196. }
  197. if len(errs) != 0 {
  198. return fmt.Errorf("errors while closing clients: %s", strings.Join(errs, ", "))
  199. }
  200. return nil
  201. }
  202. func (m *Manager) shouldProcessSecret(store esv1.GenericStore, ns string) (bool, error) {
  203. if store.GetKind() != esv1.ClusterSecretStoreKind {
  204. return true, nil
  205. }
  206. if len(store.GetSpec().Conditions) == 0 {
  207. return true, nil
  208. }
  209. namespace := v1.Namespace{}
  210. if err := m.client.Get(context.Background(), client.ObjectKey{Name: ns}, &namespace); err != nil {
  211. return false, fmt.Errorf("failed to get a namespace %q: %w", ns, err)
  212. }
  213. nsLabels := labels.Set(namespace.GetLabels())
  214. for _, condition := range store.GetSpec().Conditions {
  215. var labelSelectors []*metav1.LabelSelector
  216. if condition.NamespaceSelector != nil {
  217. labelSelectors = append(labelSelectors, condition.NamespaceSelector)
  218. }
  219. for _, n := range condition.Namespaces {
  220. labelSelectors = append(labelSelectors, &metav1.LabelSelector{
  221. MatchLabels: map[string]string{
  222. "kubernetes.io/metadata.name": n,
  223. },
  224. })
  225. }
  226. for _, ls := range labelSelectors {
  227. selector, err := metav1.LabelSelectorAsSelector(ls)
  228. if err != nil {
  229. return false, fmt.Errorf("failed to convert label selector into selector %v: %w", ls, err)
  230. }
  231. if selector.Matches(nsLabels) {
  232. return true, nil
  233. }
  234. }
  235. for _, reg := range condition.NamespaceRegexes {
  236. match, err := regexp.MatchString(reg, ns)
  237. if err != nil {
  238. // Should not happen since store validation already verified the regexes.
  239. return false, fmt.Errorf("failed to compile regex %v: %w", reg, err)
  240. }
  241. if match {
  242. return true, nil
  243. }
  244. }
  245. }
  246. return false, nil
  247. }
  248. // assertStoreIsUsable assert that the store is ready to use.
  249. func assertStoreIsUsable(store esv1.GenericStore) error {
  250. if store == nil {
  251. return nil
  252. }
  253. condition := GetSecretStoreCondition(store.GetStatus(), esv1.SecretStoreReady)
  254. if condition == nil || condition.Status != v1.ConditionTrue {
  255. return fmt.Errorf(errSecretStoreNotReady, store.GetKind(), store.GetName())
  256. }
  257. return nil
  258. }