secure_credentials.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. *
  3. * Copyright 2015, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include <grpc/support/log.h>
  34. #include <grpc++/channel.h>
  35. #include <grpc++/impl/grpc_library.h>
  36. #include <grpc++/support/channel_arguments.h>
  37. #include "src/cpp/client/create_channel_internal.h"
  38. #include "src/cpp/client/secure_credentials.h"
  39. namespace grpc {
  40. std::shared_ptr<grpc::Channel> SecureCredentials::CreateChannel(
  41. const string& target, const grpc::ChannelArguments& args) {
  42. grpc_channel_args channel_args;
  43. args.SetChannelArgs(&channel_args);
  44. return CreateChannelInternal(
  45. args.GetSslTargetNameOverride(),
  46. grpc_secure_channel_create(c_creds_, target.c_str(), &channel_args,
  47. nullptr));
  48. }
  49. bool SecureCredentials::ApplyToCall(grpc_call* call) {
  50. return grpc_call_set_credentials(call, c_creds_) == GRPC_CALL_OK;
  51. }
  52. namespace {
  53. std::shared_ptr<Credentials> WrapCredentials(grpc_credentials* creds) {
  54. return creds == nullptr
  55. ? nullptr
  56. : std::shared_ptr<Credentials>(new SecureCredentials(creds));
  57. }
  58. } // namespace
  59. std::shared_ptr<Credentials> GoogleDefaultCredentials() {
  60. GrpcLibrary init; // To call grpc_init().
  61. return WrapCredentials(grpc_google_default_credentials_create());
  62. }
  63. // Builds SSL Credentials given SSL specific options
  64. std::shared_ptr<Credentials> SslCredentials(
  65. const SslCredentialsOptions& options) {
  66. GrpcLibrary init; // To call grpc_init().
  67. grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {
  68. options.pem_private_key.c_str(), options.pem_cert_chain.c_str()};
  69. grpc_credentials* c_creds = grpc_ssl_credentials_create(
  70. options.pem_root_certs.empty() ? nullptr : options.pem_root_certs.c_str(),
  71. options.pem_private_key.empty() ? nullptr : &pem_key_cert_pair, nullptr);
  72. return WrapCredentials(c_creds);
  73. }
  74. // Builds credentials for use when running in GCE
  75. std::shared_ptr<Credentials> GoogleComputeEngineCredentials() {
  76. GrpcLibrary init; // To call grpc_init().
  77. return WrapCredentials(
  78. grpc_google_compute_engine_credentials_create(nullptr));
  79. }
  80. // Builds JWT credentials.
  81. std::shared_ptr<Credentials> ServiceAccountJWTAccessCredentials(
  82. const grpc::string& json_key, long token_lifetime_seconds) {
  83. GrpcLibrary init; // To call grpc_init().
  84. if (token_lifetime_seconds <= 0) {
  85. gpr_log(GPR_ERROR,
  86. "Trying to create JWTCredentials with non-positive lifetime");
  87. return WrapCredentials(nullptr);
  88. }
  89. gpr_timespec lifetime =
  90. gpr_time_from_seconds(token_lifetime_seconds, GPR_TIMESPAN);
  91. return WrapCredentials(grpc_service_account_jwt_access_credentials_create(
  92. json_key.c_str(), lifetime, nullptr));
  93. }
  94. // Builds refresh token credentials.
  95. std::shared_ptr<Credentials> GoogleRefreshTokenCredentials(
  96. const grpc::string& json_refresh_token) {
  97. GrpcLibrary init; // To call grpc_init().
  98. return WrapCredentials(grpc_google_refresh_token_credentials_create(
  99. json_refresh_token.c_str(), nullptr));
  100. }
  101. // Builds access token credentials.
  102. std::shared_ptr<Credentials> AccessTokenCredentials(
  103. const grpc::string& access_token) {
  104. GrpcLibrary init; // To call grpc_init().
  105. return WrapCredentials(
  106. grpc_access_token_credentials_create(access_token.c_str(), nullptr));
  107. }
  108. // Builds IAM credentials.
  109. std::shared_ptr<Credentials> GoogleIAMCredentials(
  110. const grpc::string& authorization_token,
  111. const grpc::string& authority_selector) {
  112. GrpcLibrary init; // To call grpc_init().
  113. return WrapCredentials(grpc_google_iam_credentials_create(
  114. authorization_token.c_str(), authority_selector.c_str(), nullptr));
  115. }
  116. // Combines two credentials objects into a composite credentials.
  117. std::shared_ptr<Credentials> CompositeCredentials(
  118. const std::shared_ptr<Credentials>& creds1,
  119. const std::shared_ptr<Credentials>& creds2) {
  120. // Note that we are not saving shared_ptrs to the two credentials
  121. // passed in here. This is OK because the underlying C objects (i.e.,
  122. // creds1 and creds2) into grpc_composite_credentials_create will see their
  123. // refcounts incremented.
  124. SecureCredentials* s1 = creds1->AsSecureCredentials();
  125. SecureCredentials* s2 = creds2->AsSecureCredentials();
  126. if (s1 && s2) {
  127. return WrapCredentials(grpc_composite_credentials_create(
  128. s1->GetRawCreds(), s2->GetRawCreds(), nullptr));
  129. }
  130. return nullptr;
  131. }
  132. } // namespace grpc