grpcclient.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 client
  14. import (
  15. "context"
  16. api "github.com/yandex-cloud/go-genproto/yandex/cloud/lockbox/v1"
  17. "github.com/yandex-cloud/go-sdk/iamkey"
  18. "google.golang.org/grpc"
  19. ydxcommon "github.com/external-secrets/external-secrets/providers/v1/yandex/common"
  20. )
  21. // Real/gRPC implementation of LockboxClient.
  22. type grpcLockboxClient struct {
  23. lockboxPayloadClient api.PayloadServiceClient
  24. }
  25. // NewGrpcLockboxClient creates a new LockboxClient.
  26. func NewGrpcLockboxClient(ctx context.Context, apiEndpoint string, authorizedKey *iamkey.Key, caCertificate []byte) (LockboxClient, error) {
  27. conn, err := ydxcommon.NewGrpcConnection(
  28. ctx,
  29. apiEndpoint,
  30. "lockbox-payload", // taken from https://api.cloud.yandex.net/endpoints
  31. authorizedKey,
  32. caCertificate,
  33. )
  34. if err != nil {
  35. return nil, err
  36. }
  37. return &grpcLockboxClient{api.NewPayloadServiceClient(conn)}, nil
  38. }
  39. func (c *grpcLockboxClient) GetPayloadEntries(ctx context.Context, iamToken, secretID, versionID string) ([]*api.Payload_Entry, error) {
  40. payload, err := c.lockboxPayloadClient.Get(
  41. ctx,
  42. &api.GetPayloadRequest{
  43. SecretId: secretID,
  44. VersionId: versionID,
  45. },
  46. grpc.PerRPCCredentials(ydxcommon.PerRPCCredentials{IamToken: iamToken}),
  47. )
  48. if err != nil {
  49. return nil, err
  50. }
  51. return payload.Entries, nil
  52. }
  53. func (c *grpcLockboxClient) GetExPayload(ctx context.Context, iamToken, folderID, name, versionID string) (map[string][]byte, error) {
  54. request := &api.GetExRequest{
  55. Identifier: &api.GetExRequest_FolderAndName{
  56. FolderAndName: &api.FolderAndName{
  57. FolderId: folderID,
  58. SecretName: name,
  59. },
  60. },
  61. VersionId: versionID,
  62. }
  63. response, err := c.lockboxPayloadClient.GetEx(
  64. ctx,
  65. request,
  66. grpc.PerRPCCredentials(ydxcommon.PerRPCCredentials{IamToken: iamToken}),
  67. )
  68. if err != nil {
  69. return nil, err
  70. }
  71. return response.Entries, nil
  72. }