secure_credentials.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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_arguments.h>
  35. #include "src/cpp/client/channel.h"
  36. #include "src/cpp/client/secure_credentials.h"
  37. namespace grpc {
  38. std::shared_ptr<grpc::ChannelInterface> SecureCredentials::CreateChannel(
  39. const string& target, const grpc::ChannelArguments& args) {
  40. grpc_channel_args channel_args;
  41. args.SetChannelArgs(&channel_args);
  42. return std::shared_ptr<ChannelInterface>(new Channel(
  43. args.GetSslTargetNameOverride().empty() ? target
  44. : args.GetSslTargetNameOverride(),
  45. grpc_secure_channel_create(c_creds_, target.c_str(), &channel_args)));
  46. }
  47. bool SecureCredentials::ApplyToCall(grpc_call* call) {
  48. return grpc_call_set_credentials(call, c_creds_) == GRPC_CALL_OK;
  49. }
  50. namespace {
  51. std::shared_ptr<Credentials> WrapCredentials(grpc_credentials* creds) {
  52. return creds == nullptr
  53. ? nullptr
  54. : std::shared_ptr<Credentials>(new SecureCredentials(creds));
  55. }
  56. } // namespace
  57. std::shared_ptr<Credentials> GoogleDefaultCredentials() {
  58. return WrapCredentials(grpc_google_default_credentials_create());
  59. }
  60. // Builds SSL Credentials given SSL specific options
  61. std::shared_ptr<Credentials> SslCredentials(
  62. const SslCredentialsOptions& options) {
  63. grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {
  64. options.pem_private_key.c_str(), options.pem_cert_chain.c_str()};
  65. grpc_credentials* c_creds = grpc_ssl_credentials_create(
  66. options.pem_root_certs.empty() ? nullptr : options.pem_root_certs.c_str(),
  67. options.pem_private_key.empty() ? nullptr : &pem_key_cert_pair);
  68. return WrapCredentials(c_creds);
  69. }
  70. // Builds credentials for use when running in GCE
  71. std::shared_ptr<Credentials> ComputeEngineCredentials() {
  72. return WrapCredentials(grpc_compute_engine_credentials_create());
  73. }
  74. // Builds service account credentials.
  75. std::shared_ptr<Credentials> ServiceAccountCredentials(
  76. const grpc::string& json_key, const grpc::string& scope,
  77. long token_lifetime_seconds) {
  78. if (token_lifetime_seconds <= 0) {
  79. gpr_log(GPR_ERROR,
  80. "Trying to create ServiceAccountCredentials "
  81. "with non-positive lifetime");
  82. return WrapCredentials(nullptr);
  83. }
  84. gpr_timespec lifetime = gpr_time_from_seconds(token_lifetime_seconds);
  85. return WrapCredentials(grpc_service_account_credentials_create(
  86. json_key.c_str(), scope.c_str(), lifetime));
  87. }
  88. // Builds JWT credentials.
  89. std::shared_ptr<Credentials> JWTCredentials(const grpc::string& json_key,
  90. long token_lifetime_seconds) {
  91. if (token_lifetime_seconds <= 0) {
  92. gpr_log(GPR_ERROR,
  93. "Trying to create JWTCredentials with non-positive lifetime");
  94. return WrapCredentials(nullptr);
  95. }
  96. gpr_timespec lifetime = gpr_time_from_seconds(token_lifetime_seconds);
  97. return WrapCredentials(
  98. grpc_jwt_credentials_create(json_key.c_str(), lifetime));
  99. }
  100. // Builds refresh token credentials.
  101. std::shared_ptr<Credentials> RefreshTokenCredentials(
  102. const grpc::string& json_refresh_token) {
  103. return WrapCredentials(
  104. grpc_refresh_token_credentials_create(json_refresh_token.c_str()));
  105. }
  106. // Builds IAM credentials.
  107. std::shared_ptr<Credentials> IAMCredentials(
  108. const grpc::string& authorization_token,
  109. const grpc::string& authority_selector) {
  110. return WrapCredentials(grpc_iam_credentials_create(
  111. authorization_token.c_str(), authority_selector.c_str()));
  112. }
  113. // Combines two credentials objects into a composite credentials.
  114. std::shared_ptr<Credentials> CompositeCredentials(
  115. const std::shared_ptr<Credentials>& creds1,
  116. const std::shared_ptr<Credentials>& creds2) {
  117. // Note that we are not saving shared_ptrs to the two credentials
  118. // passed in here. This is OK because the underlying C objects (i.e.,
  119. // creds1 and creds2) into grpc_composite_credentials_create will see their
  120. // refcounts incremented.
  121. SecureCredentials* s1 = creds1->AsSecureCredentials();
  122. SecureCredentials* s2 = creds2->AsSecureCredentials();
  123. if (s1 && s2) {
  124. return WrapCredentials(grpc_composite_credentials_create(
  125. s1->GetRawCreds(), s2->GetRawCreds()));
  126. }
  127. return nullptr;
  128. }
  129. } // namespace grpc