fortanix.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package fortanix
  13. import (
  14. "context"
  15. "encoding/json"
  16. "errors"
  17. "fmt"
  18. "github.com/fortanix/sdkms-client-go/sdkms"
  19. corev1 "k8s.io/api/core/v1"
  20. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  21. "github.com/external-secrets/external-secrets/pkg/utils"
  22. )
  23. type client struct {
  24. sdkms sdkms.Client
  25. }
  26. const (
  27. errPushSecretsNotSupported = "pushing secrets is currently not supported"
  28. errDeleteSecretsNotSupported = "deleting secrets is currently not supported"
  29. errUnmarshalSecret = "unable to unmarshal secret, is it a valid JSON?: %w"
  30. errUnableToGetValue = "unable to get value for key %s"
  31. errGettingSecretMapNotSupported = "getting secret map is currently not supported"
  32. errGettingAllSecretsNotSupported = "getting all secrets is currently not supported"
  33. )
  34. func (c *client) GetSecret(ctx context.Context, ref esv1beta1.ExternalSecretDataRemoteRef) ([]byte, error) {
  35. securityObject, err := c.sdkms.GetSobject(ctx, &sdkms.GetSobjectParams{}, *sdkms.SobjectByName(ref.Key))
  36. if err != nil {
  37. return nil, err
  38. }
  39. if securityObject.ObjType == sdkms.ObjectTypeSecret {
  40. securityObject, err = c.sdkms.ExportSobject(ctx, *sdkms.SobjectByID(*securityObject.Kid))
  41. if err != nil {
  42. return nil, err
  43. }
  44. }
  45. if ref.Property == "" {
  46. return *securityObject.Value, nil
  47. }
  48. kv := make(map[string]string)
  49. err = json.Unmarshal(*securityObject.Value, &kv)
  50. if err != nil {
  51. return nil, fmt.Errorf(errUnmarshalSecret, err)
  52. }
  53. value, ok := kv[ref.Property]
  54. if !ok {
  55. return nil, fmt.Errorf(errUnableToGetValue, ref.Property)
  56. }
  57. return utils.GetByteValue(value)
  58. }
  59. func (c *client) PushSecret(_ context.Context, _ *corev1.Secret, _ esv1beta1.PushSecretData) error {
  60. return errors.New(errPushSecretsNotSupported)
  61. }
  62. func (c *client) DeleteSecret(_ context.Context, _ esv1beta1.PushSecretRemoteRef) error {
  63. return errors.New(errDeleteSecretsNotSupported)
  64. }
  65. func (c *client) Validate() (esv1beta1.ValidationResult, error) {
  66. return esv1beta1.ValidationResultReady, nil
  67. }
  68. func (c *client) GetSecretMap(_ context.Context, _ esv1beta1.ExternalSecretDataRemoteRef) (map[string][]byte, error) {
  69. return nil, errors.New(errGettingSecretMapNotSupported)
  70. }
  71. func (c *client) GetAllSecrets(_ context.Context, _ esv1beta1.ExternalSecretFind) (map[string][]byte, error) {
  72. return nil, errors.New(errGettingAllSecretsNotSupported)
  73. }
  74. func (c *client) Close(context.Context) error {
  75. return nil
  76. }