sdk.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 common
  13. import (
  14. "context"
  15. "crypto/tls"
  16. "crypto/x509"
  17. "errors"
  18. "time"
  19. "github.com/yandex-cloud/go-genproto/yandex/cloud/endpoint"
  20. ycsdk "github.com/yandex-cloud/go-sdk"
  21. "github.com/yandex-cloud/go-sdk/iamkey"
  22. "google.golang.org/grpc"
  23. "google.golang.org/grpc/credentials"
  24. "google.golang.org/grpc/keepalive"
  25. )
  26. // Creates a connection to the given Yandex.Cloud API endpoint.
  27. func NewGrpcConnection(
  28. ctx context.Context,
  29. apiEndpoint string,
  30. apiEndpointID string, // an ID from https://api.cloud.yandex.net/endpoints
  31. authorizedKey *iamkey.Key,
  32. caCertificate []byte,
  33. ) (*grpc.ClientConn, error) {
  34. tlsConfig, err := tlsConfig(caCertificate)
  35. if err != nil {
  36. return nil, err
  37. }
  38. sdk, err := buildSDK(ctx, apiEndpoint, authorizedKey, tlsConfig)
  39. if err != nil {
  40. return nil, err
  41. }
  42. defer func() {
  43. _ = closeSDK(ctx, sdk)
  44. }()
  45. serviceAPIEndpoint, err := sdk.ApiEndpoint().ApiEndpoint().Get(ctx, &endpoint.GetApiEndpointRequest{
  46. ApiEndpointId: apiEndpointID,
  47. })
  48. if err != nil {
  49. return nil, err
  50. }
  51. return grpc.Dial(serviceAPIEndpoint.Address,
  52. grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)),
  53. grpc.WithKeepaliveParams(keepalive.ClientParameters{
  54. Time: time.Second * 30,
  55. Timeout: time.Second * 10,
  56. PermitWithoutStream: false,
  57. }),
  58. grpc.WithUserAgent("external-secrets"),
  59. )
  60. }
  61. // Exchanges the given authorized key to an IAM token.
  62. func NewIamToken(ctx context.Context, apiEndpoint string, authorizedKey *iamkey.Key, caCertificate []byte) (*IamToken, error) {
  63. tlsConfig, err := tlsConfig(caCertificate)
  64. if err != nil {
  65. return nil, err
  66. }
  67. sdk, err := buildSDK(ctx, apiEndpoint, authorizedKey, tlsConfig)
  68. if err != nil {
  69. return nil, err
  70. }
  71. defer func() {
  72. _ = closeSDK(ctx, sdk)
  73. }()
  74. iamToken, err := sdk.CreateIAMToken(ctx)
  75. if err != nil {
  76. return nil, err
  77. }
  78. return &IamToken{Token: iamToken.IamToken, ExpiresAt: iamToken.ExpiresAt.AsTime()}, nil
  79. }
  80. func tlsConfig(caCertificate []byte) (*tls.Config, error) {
  81. tlsConfig := &tls.Config{MinVersion: tls.VersionTLS12}
  82. if caCertificate != nil {
  83. caCertPool := x509.NewCertPool()
  84. ok := caCertPool.AppendCertsFromPEM(caCertificate)
  85. if !ok {
  86. return nil, errors.New("unable to read trusted CA certificates")
  87. }
  88. tlsConfig.RootCAs = caCertPool
  89. }
  90. return tlsConfig, nil
  91. }
  92. func buildSDK(ctx context.Context, apiEndpoint string, authorizedKey *iamkey.Key, tlsConfig *tls.Config) (*ycsdk.SDK, error) {
  93. creds, err := ycsdk.ServiceAccountKey(authorizedKey)
  94. if err != nil {
  95. return nil, err
  96. }
  97. sdk, err := ycsdk.Build(ctx, ycsdk.Config{
  98. Credentials: creds,
  99. Endpoint: apiEndpoint,
  100. TLSConfig: tlsConfig,
  101. })
  102. if err != nil {
  103. return nil, err
  104. }
  105. return sdk, nil
  106. }
  107. func closeSDK(ctx context.Context, sdk *ycsdk.SDK) error {
  108. return sdk.Shutdown(ctx)
  109. }
  110. type PerRPCCredentials struct {
  111. IamToken string
  112. }
  113. func (t PerRPCCredentials) GetRequestMetadata(_ context.Context, _ ...string) (map[string]string, error) {
  114. return map[string]string{"Authorization": "Bearer " + t.IamToken}, nil
  115. }
  116. func (PerRPCCredentials) RequireTransportSecurity() bool {
  117. return true
  118. }