passbolt.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 passbolt implements a provider for Passbolt password manager.
  14. // It allows fetching secrets stored in Passbolt using their REST API.
  15. package passbolt
  16. import (
  17. "context"
  18. "encoding/json"
  19. "errors"
  20. "fmt"
  21. "net/url"
  22. "regexp"
  23. "github.com/passbolt/go-passbolt/api"
  24. corev1 "k8s.io/api/core/v1"
  25. kclient "sigs.k8s.io/controller-runtime/pkg/client"
  26. "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
  27. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  28. "github.com/external-secrets/external-secrets/pkg/esutils"
  29. "github.com/external-secrets/external-secrets/pkg/esutils/resolvers"
  30. )
  31. const (
  32. errPassboltStoreMissingProvider = "missing: spec.provider.passbolt"
  33. errPassboltStoreMissingAuth = "missing: spec.provider.passbolt.auth"
  34. errPassboltStoreMissingAuthPassword = "missing: spec.provider.passbolt.auth.passwordSecretRef"
  35. errPassboltStoreMissingAuthPrivateKey = "missing: spec.provider.passbolt.auth.privateKeySecretRef"
  36. errPassboltStoreMissingHost = "missing: spec.provider.passbolt.host"
  37. errPassboltExternalSecretMissingFindNameRegExp = "missing: find.name.regexp"
  38. errPassboltStoreHostSchemeNotHTTPS = "host Url has to be https scheme"
  39. errPassboltSecretPropertyInvalid = "property must be one of name, username, uri, password or description"
  40. errNotImplemented = "not implemented"
  41. )
  42. // ProviderPassbolt implements the External Secrets provider interface for Passbolt.
  43. type ProviderPassbolt struct {
  44. client Client
  45. }
  46. // Capabilities return the provider supported capabilities (ReadOnly, WriteOnly, ReadWrite).
  47. func (provider *ProviderPassbolt) Capabilities() esv1.SecretStoreCapabilities {
  48. return esv1.SecretStoreReadOnly
  49. }
  50. // Client defines the interface for interacting with the Passbolt API.
  51. type Client interface {
  52. CheckSession(ctx context.Context) bool
  53. Login(ctx context.Context) error
  54. Logout(ctx context.Context) error
  55. GetResource(ctx context.Context, resourceID string) (*api.Resource, error)
  56. GetResources(ctx context.Context, opts *api.GetResourcesOptions) ([]api.Resource, error)
  57. GetResourceType(ctx context.Context, typeID string) (*api.ResourceType, error)
  58. DecryptMessage(message string) (string, error)
  59. GetSecret(ctx context.Context, resourceID string) (*api.Secret, error)
  60. }
  61. // NewClient constructs a new secrets client based on the provided store.
  62. func (provider *ProviderPassbolt) NewClient(ctx context.Context, store esv1.GenericStore, kube kclient.Client, namespace string) (esv1.SecretsClient, error) {
  63. config := store.GetSpec().Provider.Passbolt
  64. password, err := resolvers.SecretKeyRef(
  65. ctx,
  66. kube,
  67. store.GetKind(),
  68. namespace,
  69. config.Auth.PasswordSecretRef,
  70. )
  71. if err != nil {
  72. return nil, err
  73. }
  74. privateKey, err := resolvers.SecretKeyRef(
  75. ctx,
  76. kube,
  77. store.GetKind(),
  78. namespace,
  79. config.Auth.PrivateKeySecretRef,
  80. )
  81. if err != nil {
  82. return nil, err
  83. }
  84. client, err := api.NewClient(nil, "", config.Host, privateKey, password)
  85. if err != nil {
  86. return nil, err
  87. }
  88. provider.client = client
  89. return provider, nil
  90. }
  91. // SecretExists checks if a secret exists in Passbolt.
  92. func (provider *ProviderPassbolt) SecretExists(_ context.Context, _ esv1.PushSecretRemoteRef) (bool, error) {
  93. return false, errors.New(errNotImplemented)
  94. }
  95. // GetSecret retrieves a secret from Passbolt.
  96. func (provider *ProviderPassbolt) GetSecret(ctx context.Context, ref esv1.ExternalSecretDataRemoteRef) ([]byte, error) {
  97. if err := assureLoggedIn(ctx, provider.client); err != nil {
  98. return nil, err
  99. }
  100. secret, err := provider.getPassboltSecret(ctx, ref.Key)
  101. if err != nil {
  102. return nil, err
  103. }
  104. if ref.Property == "" {
  105. return esutils.JSONMarshal(secret)
  106. }
  107. return secret.GetProp(ref.Property)
  108. }
  109. // PushSecret is not implemented for Passbolt as it is read-only.
  110. func (provider *ProviderPassbolt) PushSecret(_ context.Context, _ *corev1.Secret, _ esv1.PushSecretData) error {
  111. return errors.New(errNotImplemented)
  112. }
  113. // DeleteSecret is not implemented for Passbolt as it is read-only.
  114. func (provider *ProviderPassbolt) DeleteSecret(_ context.Context, _ esv1.PushSecretRemoteRef) error {
  115. return errors.New(errNotImplemented)
  116. }
  117. // Validate performs validation of the Passbolt provider configuration.
  118. func (provider *ProviderPassbolt) Validate() (esv1.ValidationResult, error) {
  119. return esv1.ValidationResultUnknown, nil
  120. }
  121. // GetSecretMap retrieves a secret and returns it as a map of key/value pairs.
  122. func (provider *ProviderPassbolt) GetSecretMap(_ context.Context, _ esv1.ExternalSecretDataRemoteRef) (map[string][]byte, error) {
  123. return nil, errors.New(errNotImplemented)
  124. }
  125. // GetAllSecrets retrieves all secrets from Passbolt that match the given criteria.
  126. func (provider *ProviderPassbolt) GetAllSecrets(ctx context.Context, ref esv1.ExternalSecretFind) (map[string][]byte, error) {
  127. res := make(map[string][]byte)
  128. if ref.Name == nil || ref.Name.RegExp == "" {
  129. return res, errors.New(errPassboltExternalSecretMissingFindNameRegExp)
  130. }
  131. if err := assureLoggedIn(ctx, provider.client); err != nil {
  132. return nil, err
  133. }
  134. resources, err := provider.client.GetResources(ctx, &api.GetResourcesOptions{})
  135. if err != nil {
  136. return nil, err
  137. }
  138. nameRegexp, err := regexp.Compile(ref.Name.RegExp)
  139. if err != nil {
  140. return nil, err
  141. }
  142. for _, resource := range resources {
  143. if !nameRegexp.MatchString(resource.Name) {
  144. continue
  145. }
  146. secret, err := provider.getPassboltSecret(ctx, resource.ID)
  147. if err != nil {
  148. return nil, err
  149. }
  150. marshaled, err := esutils.JSONMarshal(secret)
  151. if err != nil {
  152. return nil, err
  153. }
  154. res[resource.ID] = marshaled
  155. }
  156. return res, nil
  157. }
  158. // Close implements cleanup operations for the Passbolt provider.
  159. func (provider *ProviderPassbolt) Close(ctx context.Context) error {
  160. return provider.client.Logout(ctx)
  161. }
  162. // ValidateStore validates the Passbolt SecretStore resource configuration.
  163. func (provider *ProviderPassbolt) ValidateStore(store esv1.GenericStore) (admission.Warnings, error) {
  164. config := store.GetSpec().Provider.Passbolt
  165. if config == nil {
  166. return nil, errors.New(errPassboltStoreMissingProvider)
  167. }
  168. if config.Auth == nil {
  169. return nil, errors.New(errPassboltStoreMissingAuth)
  170. }
  171. if config.Auth.PasswordSecretRef == nil || config.Auth.PasswordSecretRef.Name == "" || config.Auth.PasswordSecretRef.Key == "" {
  172. return nil, errors.New(errPassboltStoreMissingAuthPassword)
  173. }
  174. if config.Auth.PrivateKeySecretRef == nil || config.Auth.PrivateKeySecretRef.Name == "" || config.Auth.PrivateKeySecretRef.Key == "" {
  175. return nil, errors.New(errPassboltStoreMissingAuthPrivateKey)
  176. }
  177. if config.Host == "" {
  178. return nil, errors.New(errPassboltStoreMissingHost)
  179. }
  180. host, err := url.Parse(config.Host)
  181. if err != nil {
  182. return nil, err
  183. }
  184. if host.Scheme != "https" {
  185. return nil, errors.New(errPassboltStoreHostSchemeNotHTTPS)
  186. }
  187. return nil, nil
  188. }
  189. func init() {
  190. esv1.Register(&ProviderPassbolt{}, &esv1.SecretStoreProvider{
  191. Passbolt: &esv1.PassboltProvider{},
  192. }, esv1.MaintenanceStatusNotMaintained)
  193. }
  194. // Secret represents a Passbolt secret with its properties.
  195. type Secret struct {
  196. Name string `json:"name"`
  197. Username string `json:"username"`
  198. Password string `json:"password"`
  199. URI string `json:"uri"`
  200. Description string `json:"description"`
  201. }
  202. // GetProp retrieves a specific property from the Passbolt secret.
  203. func (ps Secret) GetProp(key string) ([]byte, error) {
  204. switch key {
  205. case "name":
  206. return []byte(ps.Name), nil
  207. case "username":
  208. return []byte(ps.Username), nil
  209. case "uri":
  210. return []byte(ps.URI), nil
  211. case "password":
  212. return []byte(ps.Password), nil
  213. case "description":
  214. return []byte(ps.Description), nil
  215. default:
  216. return nil, errors.New(errPassboltSecretPropertyInvalid)
  217. }
  218. }
  219. func (provider *ProviderPassbolt) getPassboltSecret(ctx context.Context, id string) (*Secret, error) {
  220. resource, err := provider.client.GetResource(ctx, id)
  221. if err != nil {
  222. return nil, err
  223. }
  224. secret, err := provider.client.GetSecret(ctx, resource.ID)
  225. if err != nil {
  226. return nil, err
  227. }
  228. res := Secret{
  229. Name: resource.Name,
  230. Username: resource.Username,
  231. URI: resource.URI,
  232. Description: resource.Description,
  233. }
  234. raw, err := provider.client.DecryptMessage(secret.Data)
  235. if err != nil {
  236. return nil, err
  237. }
  238. resourceType, err := provider.client.GetResourceType(ctx, resource.ResourceTypeID)
  239. if err != nil {
  240. return nil, err
  241. }
  242. switch resourceType.Slug {
  243. case "password-string":
  244. res.Password = raw
  245. case "password-and-description", "password-description-totp":
  246. var pwAndDesc api.SecretDataTypePasswordAndDescription
  247. if err := json.Unmarshal([]byte(raw), &pwAndDesc); err != nil {
  248. return nil, err
  249. }
  250. res.Password = pwAndDesc.Password
  251. res.Description = pwAndDesc.Description
  252. case "totp":
  253. default:
  254. return nil, fmt.Errorf("UnknownPassboltResourceType: %q", resourceType)
  255. }
  256. return &res, nil
  257. }
  258. func assureLoggedIn(ctx context.Context, client Client) error {
  259. if client.CheckSession(ctx) {
  260. return nil
  261. }
  262. return client.Login(ctx)
  263. }