grpc_client.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 mysterybox
  14. import (
  15. "context"
  16. "fmt"
  17. "github.com/nebius/gosdk"
  18. proto "github.com/nebius/gosdk/proto/nebius/mysterybox/v1"
  19. mbox "github.com/nebius/gosdk/services/nebius/mysterybox/v1"
  20. "google.golang.org/grpc"
  21. "github.com/external-secrets/external-secrets/providers/v1/nebius/common/sdk"
  22. )
  23. const (
  24. notSupportedPayloadType = "payload type not supported, key: %v"
  25. )
  26. // GrpcClient provides methods for interacting with a gRPC payload service and managing secret data via an SDK.
  27. type GrpcClient struct {
  28. PayloadService mbox.PayloadService
  29. sdk *gosdk.SDK
  30. }
  31. // Close shuts down the underlying gRPC SDK connection and releases associated resources.
  32. func (c *GrpcClient) Close() error {
  33. return c.sdk.Close()
  34. }
  35. // GetSecretByKey retrieves a specific key's payload for a given secretID and versionID using a provided token.
  36. // It returns the payload containing the key, its value (string or binary), versionID, or an error if retrieval fails.
  37. func (c *GrpcClient) GetSecretByKey(ctx context.Context, token, secretID, versionID, key string) (*PayloadEntry, error) {
  38. payloadRequest := proto.GetPayloadByKeyRequest{
  39. SecretId: secretID,
  40. VersionId: versionID,
  41. Key: key,
  42. }
  43. entry, err := c.PayloadService.GetByKey(
  44. ctx,
  45. &payloadRequest,
  46. grpc.PerRPCCredentials(PerRPCCredentials{IamToken: token}),
  47. )
  48. if err != nil {
  49. return nil, err
  50. }
  51. if entry.GetData() == nil {
  52. return nil, fmt.Errorf("received nil data for key: %v", key)
  53. }
  54. payloadEntry := PayloadEntry{
  55. VersionID: entry.GetVersionId(),
  56. Entry: Entry{
  57. Key: entry.GetData().GetKey(),
  58. },
  59. }
  60. switch entry.GetData().Payload.(type) {
  61. case *proto.Payload_StringValue:
  62. payloadEntry.Entry.StringValue = entry.GetData().GetStringValue()
  63. case *proto.Payload_BinaryValue:
  64. payloadEntry.Entry.BinaryValue = entry.GetData().GetBinaryValue()
  65. default:
  66. return nil, fmt.Errorf(notSupportedPayloadType, key)
  67. }
  68. return &payloadEntry, nil
  69. }
  70. // GetSecret retrieves the secret payload associated with a given secretID and versionID using the provided token.
  71. // It returns the payload containing the secret version and entries or an error if the retrieval fails.
  72. func (c *GrpcClient) GetSecret(ctx context.Context, token, secretID, versionID string) (*Payload, error) {
  73. payloadRequest := proto.GetPayloadRequest{
  74. SecretId: secretID,
  75. VersionId: versionID,
  76. }
  77. payload, err := c.PayloadService.Get(
  78. ctx,
  79. &payloadRequest,
  80. grpc.PerRPCCredentials(PerRPCCredentials{IamToken: token}),
  81. )
  82. if err != nil {
  83. return nil, err
  84. }
  85. payloadEntries := make([]Entry, 0, len(payload.Data))
  86. for _, entry := range payload.GetData() {
  87. payloadEntry := Entry{
  88. Key: entry.Key,
  89. }
  90. switch entry.Payload.(type) {
  91. case *proto.Payload_StringValue:
  92. payloadEntry.StringValue = entry.GetStringValue()
  93. case *proto.Payload_BinaryValue:
  94. payloadEntry.BinaryValue = entry.GetBinaryValue()
  95. default:
  96. return nil, fmt.Errorf(notSupportedPayloadType, entry.Key)
  97. }
  98. payloadEntries = append(payloadEntries, payloadEntry)
  99. }
  100. return &Payload{
  101. VersionID: payload.VersionId,
  102. Entries: payloadEntries,
  103. }, nil
  104. }
  105. // NewNebiusMysteryboxClientGrpc initializes a new gRPC client for Nebius Mysterybox using the provided context, API domain, and CA certificate.
  106. func NewNebiusMysteryboxClientGrpc(ctx context.Context, apiDomain string, caCertificate []byte) (*GrpcClient, error) {
  107. mysteryboxSdk, err := sdk.NewSDK(ctx, apiDomain, caCertificate)
  108. if err != nil {
  109. return nil, err
  110. }
  111. return &GrpcClient{
  112. mbox.NewPayloadService(mysteryboxSdk),
  113. mysteryboxSdk,
  114. }, nil
  115. }
  116. // PerRPCCredentials represents authentication credentials for each RPC call, including an IAM token for authorization.
  117. type PerRPCCredentials struct {
  118. IamToken string
  119. }
  120. // GetRequestMetadata returns request metadata as a map for RPC authorization using the IAM token.
  121. // It includes an "Authorization" header with a Bearer token constructed from the IAM token.
  122. func (c PerRPCCredentials) GetRequestMetadata(_ context.Context, _ ...string) (map[string]string, error) {
  123. return map[string]string{"Authorization": "Bearer " + c.IamToken}, nil
  124. }
  125. // RequireTransportSecurity specifies whether the transport should use a secure connection when sending credentials.
  126. func (PerRPCCredentials) RequireTransportSecurity() bool {
  127. return true
  128. }
  129. var _ Client = &GrpcClient{}