certificatemanagersecretgetter.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 certificatemanager
  13. import (
  14. "context"
  15. "fmt"
  16. "strings"
  17. "github.com/external-secrets/external-secrets/pkg/provider/yandex/certificatemanager/client"
  18. "github.com/external-secrets/external-secrets/pkg/provider/yandex/common"
  19. )
  20. const (
  21. chainProperty = "chain"
  22. privateKeyProperty = "privateKey"
  23. chainAndPrivateKeyProperty = "chainAndPrivateKey"
  24. )
  25. // Implementation of common.SecretGetter.
  26. type certificateManagerSecretGetter struct {
  27. certificateManagerClient client.CertificateManagerClient
  28. }
  29. func newCertificateManagerSecretGetter(certificateManagerClient client.CertificateManagerClient) (common.SecretGetter, error) {
  30. return &certificateManagerSecretGetter{
  31. certificateManagerClient: certificateManagerClient,
  32. }, nil
  33. }
  34. func (g *certificateManagerSecretGetter) GetSecret(ctx context.Context, iamToken, resourceID, versionID, property string) ([]byte, error) {
  35. response, err := g.certificateManagerClient.GetCertificateContent(ctx, iamToken, resourceID, versionID)
  36. if err != nil {
  37. return nil, fmt.Errorf("unable to request certificate content to get secret: %w", err)
  38. }
  39. chain := trimAndJoin(response.CertificateChain...)
  40. privateKey := trimAndJoin(response.PrivateKey)
  41. switch property {
  42. case "", chainAndPrivateKeyProperty:
  43. return []byte(trimAndJoin(chain, privateKey)), nil
  44. case chainProperty:
  45. return []byte(chain), nil
  46. case privateKeyProperty:
  47. return []byte(privateKey), nil
  48. default:
  49. return nil, fmt.Errorf("unsupported property '%s'", property)
  50. }
  51. }
  52. func (g *certificateManagerSecretGetter) GetSecretMap(ctx context.Context, iamToken, resourceID, versionID string) (map[string][]byte, error) {
  53. response, err := g.certificateManagerClient.GetCertificateContent(ctx, iamToken, resourceID, versionID)
  54. if err != nil {
  55. return nil, fmt.Errorf("unable to request certificate content to get secret map: %w", err)
  56. }
  57. chain := strings.Join(response.CertificateChain, "\n")
  58. privateKey := response.PrivateKey
  59. return map[string][]byte{
  60. chainProperty: []byte(chain),
  61. privateKeyProperty: []byte(privateKey),
  62. }, nil
  63. }
  64. func trimAndJoin(elems ...string) string {
  65. var sb strings.Builder
  66. for _, elem := range elems {
  67. sb.WriteString(strings.TrimSpace(elem))
  68. sb.WriteRune('\n')
  69. }
  70. return sb.String()
  71. }