client.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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 keepersecurity
  14. import (
  15. "context"
  16. "encoding/json"
  17. "errors"
  18. "fmt"
  19. "maps"
  20. "regexp"
  21. "strings"
  22. ksm "github.com/keeper-security/secrets-manager-go/core"
  23. corev1 "k8s.io/api/core/v1"
  24. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  25. "github.com/external-secrets/external-secrets/runtime/constants"
  26. "github.com/external-secrets/external-secrets/runtime/metrics"
  27. )
  28. const (
  29. errKeeperSecuritySecretsNotFound = "unable to find secrets. %w"
  30. errKeeperSecuritySecretNotFound = "unable to find secret %s. Error: %w"
  31. errKeeperSecuritySecretNotUnique = "more than 1 secret %s found"
  32. errKeeperSecurityNoSecretsFound = "no secrets found"
  33. errKeeperSecurityInvalidSecretInvalidFormat = "invalid secret. Invalid format: %w"
  34. errKeeperSecurityInvalidSecretDuplicatedKey = "invalid Secret. Following keys are duplicated %s"
  35. errKeeperSecurityInvalidProperty = "invalid Property. Secret %s does not have any key matching %s"
  36. errKeeperSecurityInvalidField = "invalid Field. Key %s does not exists"
  37. errKeeperSecurityNoFields = "invalid Secret. Secret %s does not contain any valid field/file"
  38. keeperSecurityFileRef = "fileRef"
  39. keeperSecurityMfa = "oneTimeCode"
  40. errTagsNotImplemented = "'find.tags' is not implemented in the KeeperSecurity provider"
  41. errPathNotImplemented = "'find.path' is not implemented in the KeeperSecurity provider"
  42. errInvalidJSONSecret = "invalid Secret. Secret %s can not be converted to JSON. %w"
  43. errInvalidRegex = "find.name.regex. Invalid Regular expresion %s. %w"
  44. errInvalidRemoteRefKey = "match.remoteRef.remoteKey. Invalid format. Format should match secretName/key got %s"
  45. errInvalidSecretType = "ESO can only push/delete records of type %s. Secret %s is type %s"
  46. errFieldNotFound = "secret %s does not contain any custom field with label %s"
  47. externalSecretType = "externalSecrets"
  48. secretType = "secret"
  49. // LoginType represents the login field type.
  50. LoginType = "login"
  51. // LoginTypeExpr is the regex expression for matching login/username fields.
  52. LoginTypeExpr = "login|username"
  53. // PasswordType represents the password field type.
  54. PasswordType = "password"
  55. // URLTypeExpr is the regex expression for matching URL/baseurl fields.
  56. URLTypeExpr = "url|baseurl"
  57. // URLType represents the URL field type.
  58. URLType = "url"
  59. )
  60. // Client represents a KeeperSecurity client that can interact with the KeeperSecurity API.
  61. type Client struct {
  62. ksmClient SecurityClient
  63. folderID string
  64. getByTitleFallback bool
  65. }
  66. // SecurityClient defines the interface for interacting with KeeperSecurity's API.
  67. type SecurityClient interface {
  68. GetSecrets(filter []string) ([]*ksm.Record, error)
  69. GetSecretByTitle(recordTitle string) (*ksm.Record, error)
  70. GetSecretsByTitle(recordTitle string) (records []*ksm.Record, err error)
  71. CreateSecretWithRecordData(recUID, folderUID string, recordData *ksm.RecordCreate) (string, error)
  72. DeleteSecrets(recrecordUids []string) (map[string]string, error)
  73. Save(record *ksm.Record) error
  74. }
  75. // Field represents a KeeperSecurity field with its type, label (optional), and value.
  76. type Field struct {
  77. Type string `json:"type"`
  78. Label string `json:"label,omitempty"`
  79. Value []any `json:"value"`
  80. }
  81. // CustomField represents a custom field in KeeperSecurity with its type, label and value.
  82. type CustomField struct {
  83. Type string `json:"type"`
  84. Label string `json:"label"`
  85. Value []any `json:"value"`
  86. }
  87. // File represents a file stored in KeeperSecurity with its title and content.
  88. type File struct {
  89. Title string `json:"type"`
  90. Content string `json:"content"`
  91. }
  92. // Secret represents a KeeperSecurity secret with its metadata and content.
  93. type Secret struct {
  94. Title string `json:"title"`
  95. Type string `json:"type"`
  96. Fields []Field `json:"fields"`
  97. Custom []CustomField `json:"custom"`
  98. Files []File `json:"files"`
  99. }
  100. // Validate performs validation of the Keeper Security client configuration.
  101. func (c *Client) Validate() (esv1.ValidationResult, error) {
  102. return esv1.ValidationResultReady, nil
  103. }
  104. // GetSecret retrieves a secret from Keeper Security by ID or name.
  105. // It first attempts to find the secret by ID, then falls back to name lookup.
  106. // The name lookup must be opted in by setting getByTitleFallback on the provider.
  107. func (c *Client) GetSecret(_ context.Context, ref esv1.ExternalSecretDataRemoteRef) ([]byte, error) {
  108. secret, err := c.findByIDWithNameFallback(ref.Key)
  109. if err != nil {
  110. return nil, err
  111. }
  112. return secret.getItem(ref)
  113. }
  114. // GetSecretMap retrieves a secret from Keeper Security and returns it as a map.
  115. func (c *Client) GetSecretMap(_ context.Context, ref esv1.ExternalSecretDataRemoteRef) (map[string][]byte, error) {
  116. secret, err := c.findByIDWithNameFallback(ref.Key)
  117. if err != nil {
  118. return nil, err
  119. }
  120. return secret.getItems(ref)
  121. }
  122. // It first attempts to find the secret by ID, then falls back to name lookup.
  123. // The name lookup must be opted in by setting getByTitleFallback on the provider.
  124. func (c *Client) findByIDWithNameFallback(key string) (*Secret, error) {
  125. record, err := c.findSecretByID(key)
  126. if err != nil {
  127. return nil, err
  128. }
  129. if record == nil && c.getByTitleFallback {
  130. records, err := c.ksmClient.GetSecretsByTitle(key)
  131. metrics.ObserveAPICall(constants.ProviderKeeperSecurity, constants.CallKeeperSecurityGetSecretsByTitle, err)
  132. if err != nil {
  133. return nil, err
  134. }
  135. if len(records) > 1 {
  136. return nil, errors.New(errKeeperSecuritySecretNotUnique)
  137. } else if len(records) == 1 {
  138. record = records[0]
  139. }
  140. }
  141. if record == nil {
  142. return nil, errors.New(errKeeperSecurityNoSecretsFound)
  143. }
  144. secret, err := c.getValidKeeperSecret(record)
  145. if err != nil {
  146. return nil, err
  147. }
  148. return secret, nil
  149. }
  150. // GetAllSecrets retrieves all secrets from Keeper Security that match the given criteria.
  151. func (c *Client) GetAllSecrets(_ context.Context, ref esv1.ExternalSecretFind) (map[string][]byte, error) {
  152. if ref.Tags != nil {
  153. return nil, errors.New(errTagsNotImplemented)
  154. }
  155. if ref.Path != nil {
  156. return nil, errors.New(errPathNotImplemented)
  157. }
  158. secretData := make(map[string][]byte)
  159. records, err := c.findSecrets()
  160. // GetAllSecrets retrieves all secrets from Keeper Security that match the given criteria.
  161. // Currently supports filtering by name pattern only.
  162. if err != nil {
  163. return nil, err
  164. }
  165. for _, record := range records {
  166. secret, err := c.getValidKeeperSecret(record)
  167. if err != nil {
  168. return nil, err
  169. }
  170. match, err := regexp.MatchString(ref.Name.RegExp, secret.Title)
  171. if err != nil {
  172. return nil, fmt.Errorf(errInvalidRegex, ref.Name.RegExp, err)
  173. }
  174. if !match {
  175. continue
  176. }
  177. secretData[secret.Title], err = secret.getItem(esv1.ExternalSecretDataRemoteRef{})
  178. if err != nil {
  179. return nil, err
  180. }
  181. }
  182. return secretData, nil
  183. }
  184. // Close implements cleanup operations for the Keeper Security client.
  185. func (c *Client) Close(_ context.Context) error {
  186. return nil
  187. }
  188. // PushSecret creates or updates a secret in Keeper Security.
  189. func (c *Client) PushSecret(_ context.Context, secret *corev1.Secret, data esv1.PushSecretData) error {
  190. if data.GetSecretKey() == "" {
  191. return errors.New("pushing the whole secret is not yet implemented")
  192. }
  193. // Close implements cleanup operations for the Keeper Security client
  194. value := secret.Data[data.GetSecretKey()]
  195. parts, err := c.buildSecretNameAndKey(data)
  196. if err != nil {
  197. return err
  198. // PushSecret creates or updates a secret in Keeper Security.
  199. // Currently only supports pushing individual secret values, not entire secrets.
  200. }
  201. record, err := c.findSecretByName(parts[0])
  202. if err != nil {
  203. return err
  204. }
  205. if record != nil {
  206. if record.Type() != externalSecretType {
  207. return fmt.Errorf(errInvalidSecretType, externalSecretType, record.Title(), record.Type())
  208. }
  209. return c.updateSecret(record, parts[1], value)
  210. }
  211. _, err = c.createSecret(parts[0], parts[1], value)
  212. return err
  213. }
  214. // DeleteSecret removes a secret from Keeper Security.
  215. func (c *Client) DeleteSecret(_ context.Context, remoteRef esv1.PushSecretRemoteRef) error {
  216. parts, err := c.buildSecretNameAndKey(remoteRef)
  217. if err != nil {
  218. return err
  219. }
  220. secret, err := c.findSecretByName(parts[0])
  221. if err != nil {
  222. return err
  223. } else if secret == nil {
  224. // DeleteSecret removes a secret from Keeper Security.
  225. // Returns nil if the secret doesn't exist (already deleted).
  226. return nil // not found == already deleted (success)
  227. }
  228. if secret.Type() != externalSecretType {
  229. return fmt.Errorf(errInvalidSecretType, externalSecretType, secret.Title(), secret.Type())
  230. }
  231. _, err = c.ksmClient.DeleteSecrets([]string{secret.Uid})
  232. metrics.ObserveAPICall(constants.ProviderKeeperSecurity, constants.CallKeeperSecurityDeleteSecrets, err)
  233. return err
  234. }
  235. // SecretExists checks if a secret exists in Keeper Security.
  236. func (c *Client) SecretExists(_ context.Context, _ esv1.PushSecretRemoteRef) (bool, error) {
  237. return false, errors.New("not implemented")
  238. }
  239. func (c *Client) buildSecretNameAndKey(remoteRef esv1.PushSecretRemoteRef) ([]string, error) {
  240. parts := strings.Split(remoteRef.GetRemoteKey(), "/")
  241. if len(parts) != 2 {
  242. return nil, fmt.Errorf(errInvalidRemoteRefKey, remoteRef.GetRemoteKey())
  243. }
  244. // SecretExists checks if a secret exists in Keeper Security.
  245. // This method is not implemented yet.
  246. return parts, nil
  247. }
  248. func (c *Client) createSecret(name, key string, value []byte) (string, error) {
  249. normalizedKey := strings.ToLower(key)
  250. externalSecretRecord := ksm.NewRecordCreate(externalSecretType, name)
  251. login := regexp.MustCompile(LoginTypeExpr)
  252. pass := regexp.MustCompile(PasswordType)
  253. url := regexp.MustCompile(URLTypeExpr)
  254. switch {
  255. case login.MatchString(normalizedKey):
  256. externalSecretRecord.Fields = append(externalSecretRecord.Fields,
  257. ksm.NewLogin(string(value)),
  258. )
  259. case pass.MatchString(normalizedKey):
  260. externalSecretRecord.Fields = append(externalSecretRecord.Fields,
  261. ksm.NewPassword(string(value)),
  262. )
  263. case url.MatchString(normalizedKey):
  264. externalSecretRecord.Fields = append(externalSecretRecord.Fields,
  265. ksm.NewUrl(string(value)),
  266. )
  267. default:
  268. field := ksm.KeeperRecordField{Type: secretType, Label: key}
  269. externalSecretRecord.Custom = append(externalSecretRecord.Custom,
  270. ksm.Secret{KeeperRecordField: field, Value: []string{string(value)}},
  271. )
  272. }
  273. uid, err := c.ksmClient.CreateSecretWithRecordData("", c.folderID, externalSecretRecord)
  274. metrics.ObserveAPICall(constants.ProviderKeeperSecurity, constants.CallKeeperSecurityCreateSecretWithRecordData, err)
  275. return uid, err
  276. }
  277. func (c *Client) updateSecret(secret *ksm.Record, key string, value []byte) error {
  278. normalizedKey := strings.ToLower(key)
  279. login := regexp.MustCompile(LoginTypeExpr)
  280. pass := regexp.MustCompile(PasswordType)
  281. url := regexp.MustCompile(URLTypeExpr)
  282. custom := false
  283. switch {
  284. case login.MatchString(normalizedKey):
  285. secret.SetFieldValueSingle(LoginType, string(value))
  286. case pass.MatchString(normalizedKey):
  287. secret.SetPassword(string(value))
  288. case url.MatchString(normalizedKey):
  289. secret.SetFieldValueSingle(URLType, string(value))
  290. default:
  291. custom = true
  292. }
  293. if custom {
  294. field := secret.GetCustomFieldValueByLabel(key)
  295. if field != "" {
  296. secret.SetCustomFieldValueSingle(key, string(value))
  297. } else {
  298. return fmt.Errorf(errFieldNotFound, secret.Title(), key)
  299. }
  300. }
  301. err := c.ksmClient.Save(secret)
  302. metrics.ObserveAPICall(constants.ProviderKeeperSecurity, constants.CallKeeperSecuritySave, err)
  303. return err
  304. }
  305. func (c *Client) getValidKeeperSecret(secret *ksm.Record) (*Secret, error) {
  306. keeperSecret := Secret{}
  307. err := json.Unmarshal([]byte(secret.RawJson), &keeperSecret)
  308. if err != nil {
  309. return nil, fmt.Errorf(errKeeperSecurityInvalidSecretInvalidFormat, err)
  310. }
  311. keeperSecret.addFiles(secret.Files)
  312. err = keeperSecret.validate()
  313. if err != nil {
  314. return nil, err
  315. }
  316. return &keeperSecret, nil
  317. }
  318. func (c *Client) findSecrets() ([]*ksm.Record, error) {
  319. records, err := c.ksmClient.GetSecrets([]string{})
  320. metrics.ObserveAPICall(constants.ProviderKeeperSecurity, constants.CallKeeperSecurityGetSecrets, err)
  321. if err != nil {
  322. return nil, fmt.Errorf(errKeeperSecuritySecretsNotFound, err)
  323. }
  324. return records, nil
  325. }
  326. func (c *Client) findSecretByID(id string) (*ksm.Record, error) {
  327. records, err := c.ksmClient.GetSecrets([]string{id})
  328. metrics.ObserveAPICall(constants.ProviderKeeperSecurity, constants.CallKeeperSecurityGetSecrets, err)
  329. if err != nil {
  330. return nil, fmt.Errorf(errKeeperSecuritySecretNotFound, id, err)
  331. }
  332. if len(records) == 0 {
  333. return nil, nil
  334. }
  335. return records[0], nil
  336. }
  337. func (c *Client) findSecretByName(name string) (*ksm.Record, error) {
  338. records, err := c.ksmClient.GetSecretsByTitle(name)
  339. metrics.ObserveAPICall(constants.ProviderKeeperSecurity, constants.CallKeeperSecurityGetSecretsByTitle, err)
  340. if err != nil {
  341. return nil, err
  342. }
  343. // filter in-place, preserve only records of type externalSecretType
  344. n := 0
  345. for _, record := range records {
  346. if record.Type() == externalSecretType {
  347. records[n] = record
  348. n++
  349. }
  350. }
  351. records = records[:n]
  352. // record not found is not an error - handled differently:
  353. // PushSecret will create new record instead
  354. // DeleteSecret will consider record already deleted (no error)
  355. if len(records) == 0 {
  356. return nil, nil
  357. } else if len(records) == 1 {
  358. return records[0], nil
  359. }
  360. // len(records) > 1
  361. return nil, fmt.Errorf(errKeeperSecuritySecretNotUnique, name)
  362. }
  363. func (s *Secret) validate() error {
  364. fields := make(map[string]int)
  365. for _, field := range s.Fields {
  366. fieldKey := field.Label
  367. if fieldKey == "" {
  368. fieldKey = field.Type
  369. }
  370. fields[fieldKey]++
  371. }
  372. for _, customField := range s.Custom {
  373. fields[customField.Label]++
  374. }
  375. for _, file := range s.Files {
  376. fields[file.Title]++
  377. }
  378. var duplicates []string
  379. for key, ocurrences := range fields {
  380. if ocurrences > 1 {
  381. duplicates = append(duplicates, key)
  382. }
  383. }
  384. if len(duplicates) != 0 {
  385. return fmt.Errorf(errKeeperSecurityInvalidSecretDuplicatedKey, strings.Join(duplicates, ", "))
  386. }
  387. return nil
  388. }
  389. func (s *Secret) addFiles(keeperFiles []*ksm.KeeperFile) {
  390. for _, f := range keeperFiles {
  391. s.Files = append(
  392. s.Files,
  393. File{
  394. Title: f.Title,
  395. Content: string(f.GetFileData()),
  396. },
  397. )
  398. }
  399. }
  400. func (s *Secret) getItem(ref esv1.ExternalSecretDataRemoteRef) ([]byte, error) {
  401. if ref.Property != "" {
  402. return s.getProperty(ref.Property)
  403. }
  404. secret, err := s.toString()
  405. return []byte(secret), err
  406. }
  407. func (s *Secret) getItems(ref esv1.ExternalSecretDataRemoteRef) (map[string][]byte, error) {
  408. secretData := make(map[string][]byte)
  409. if ref.Property != "" {
  410. value, err := s.getProperty(ref.Property)
  411. if err != nil {
  412. return nil, err
  413. }
  414. secretData[ref.Property] = value
  415. return secretData, nil
  416. }
  417. fields := s.getFields()
  418. maps.Copy(secretData, fields)
  419. customFields := s.getCustomFields()
  420. maps.Copy(secretData, customFields)
  421. files := s.getFiles()
  422. maps.Copy(secretData, files)
  423. if len(secretData) == 0 {
  424. return nil, fmt.Errorf(errKeeperSecurityNoFields, s.Title)
  425. }
  426. return secretData, nil
  427. }
  428. func getFieldValue(value []any) []byte {
  429. if len(value) < 1 {
  430. return []byte{}
  431. }
  432. if len(value) == 1 {
  433. res, _ := json.Marshal(value[0])
  434. if str, ok := value[0].(string); ok {
  435. res = []byte(str)
  436. }
  437. return res
  438. }
  439. res, _ := json.Marshal(value)
  440. return res
  441. }
  442. func (s *Secret) getField(key string) ([]byte, error) {
  443. for _, field := range s.Fields {
  444. fieldKey := field.Label
  445. if fieldKey == "" {
  446. fieldKey = field.Type
  447. }
  448. if fieldKey == key && field.Type != keeperSecurityFileRef && field.Type != keeperSecurityMfa && len(field.Value) > 0 {
  449. return getFieldValue(field.Value), nil
  450. }
  451. }
  452. return nil, fmt.Errorf(errKeeperSecurityInvalidField, key)
  453. }
  454. func (s *Secret) getFields() map[string][]byte {
  455. secretData := make(map[string][]byte)
  456. for _, field := range s.Fields {
  457. if len(field.Value) > 0 {
  458. fieldKey := field.Label
  459. if fieldKey == "" {
  460. fieldKey = field.Type
  461. }
  462. secretData[fieldKey] = getFieldValue(field.Value)
  463. }
  464. }
  465. return secretData
  466. }
  467. func (s *Secret) getCustomField(key string) ([]byte, error) {
  468. for _, field := range s.Custom {
  469. if field.Label == key && len(field.Value) > 0 {
  470. return getFieldValue(field.Value), nil
  471. }
  472. }
  473. return nil, fmt.Errorf(errKeeperSecurityInvalidField, key)
  474. }
  475. func (s *Secret) getCustomFields() map[string][]byte {
  476. secretData := make(map[string][]byte)
  477. for _, field := range s.Custom {
  478. if len(field.Value) > 0 {
  479. secretData[field.Label] = getFieldValue(field.Value)
  480. }
  481. }
  482. return secretData
  483. }
  484. func (s *Secret) getFile(key string) ([]byte, error) {
  485. for _, file := range s.Files {
  486. if file.Title == key {
  487. return []byte(file.Content), nil
  488. }
  489. }
  490. return nil, fmt.Errorf(errKeeperSecurityInvalidField, key)
  491. }
  492. func (s *Secret) getProperty(key string) ([]byte, error) {
  493. field, _ := s.getField(key)
  494. if field != nil {
  495. return field, nil
  496. }
  497. customField, _ := s.getCustomField(key)
  498. if customField != nil {
  499. return customField, nil
  500. }
  501. file, _ := s.getFile(key)
  502. if file != nil {
  503. return file, nil
  504. }
  505. return nil, fmt.Errorf(errKeeperSecurityInvalidProperty, s.Title, key)
  506. }
  507. func (s *Secret) getFiles() map[string][]byte {
  508. secretData := make(map[string][]byte)
  509. for _, file := range s.Files {
  510. secretData[file.Title] = []byte(file.Content)
  511. }
  512. return secretData
  513. }
  514. func (s *Secret) toString() (string, error) {
  515. secretJSON, err := json.Marshal(s)
  516. if err != nil {
  517. return "", fmt.Errorf(errInvalidJSONSecret, s.Title, err)
  518. }
  519. return string(secretJSON), nil
  520. }