cli_credentials.cc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. *
  3. * Copyright 2016 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include "test/cpp/util/cli_credentials.h"
  19. #include "absl/flags/flag.h"
  20. #include <grpc/slice.h>
  21. #include <grpc/support/log.h>
  22. #include <grpcpp/impl/codegen/slice.h>
  23. #include "src/core/lib/iomgr/load_file.h"
  24. ABSL_RETIRED_FLAG(bool, enable_ssl, false,
  25. "Replaced by --channel_creds_type=ssl.");
  26. ABSL_RETIRED_FLAG(bool, use_auth, false,
  27. "Replaced by --channel_creds_type=gdc.");
  28. ABSL_RETIRED_FLAG(std::string, access_token, "",
  29. "Replaced by --call_creds=access_token=<token>.");
  30. ABSL_FLAG(
  31. std::string, ssl_target, "",
  32. "If not empty, treat the server host name as this for ssl/tls certificate "
  33. "validation.");
  34. ABSL_FLAG(
  35. std::string, ssl_client_cert, "",
  36. "If not empty, load this PEM formatted client certificate file. Requires "
  37. "use of --ssl_client_key.");
  38. ABSL_FLAG(std::string, ssl_client_key, "",
  39. "If not empty, load this PEM formatted private key. Requires use of "
  40. "--ssl_client_cert");
  41. ABSL_FLAG(
  42. std::string, local_connect_type, "local_tcp",
  43. "The type of local connections for which local channel credentials will "
  44. "be applied. Should be local_tcp or uds.");
  45. ABSL_FLAG(
  46. std::string, channel_creds_type, "",
  47. "The channel creds type: insecure, ssl, gdc (Google Default Credentials), "
  48. "alts, or local.");
  49. ABSL_FLAG(
  50. std::string, call_creds, "",
  51. "Call credentials to use: none (default), or access_token=<token>. If "
  52. "provided, the call creds are composited on top of channel creds.");
  53. namespace grpc {
  54. namespace testing {
  55. namespace {
  56. const char ACCESS_TOKEN_PREFIX[] = "access_token=";
  57. constexpr int ACCESS_TOKEN_PREFIX_LEN =
  58. sizeof(ACCESS_TOKEN_PREFIX) / sizeof(*ACCESS_TOKEN_PREFIX) - 1;
  59. bool IsAccessToken(const std::string& auth) {
  60. return auth.length() > ACCESS_TOKEN_PREFIX_LEN &&
  61. auth.compare(0, ACCESS_TOKEN_PREFIX_LEN, ACCESS_TOKEN_PREFIX) == 0;
  62. }
  63. std::string AccessToken(const std::string& auth) {
  64. if (!IsAccessToken(auth)) {
  65. return "";
  66. }
  67. return std::string(auth, ACCESS_TOKEN_PREFIX_LEN);
  68. }
  69. } // namespace
  70. std::string CliCredentials::GetDefaultChannelCredsType() const {
  71. return "insecure";
  72. }
  73. std::string CliCredentials::GetDefaultCallCreds() const { return "none"; }
  74. std::shared_ptr<grpc::ChannelCredentials>
  75. CliCredentials::GetChannelCredentials() const {
  76. if (absl::GetFlag(FLAGS_channel_creds_type) == "insecure") {
  77. return grpc::InsecureChannelCredentials();
  78. } else if (absl::GetFlag(FLAGS_channel_creds_type) == "ssl") {
  79. grpc::SslCredentialsOptions ssl_creds_options;
  80. // TODO(@Capstan): This won't affect Google Default Credentials using SSL.
  81. if (!absl::GetFlag(FLAGS_ssl_client_cert).empty()) {
  82. grpc_slice cert_slice = grpc_empty_slice();
  83. GRPC_LOG_IF_ERROR(
  84. "load_file",
  85. grpc_load_file(absl::GetFlag(FLAGS_ssl_client_cert).c_str(), 1,
  86. &cert_slice));
  87. ssl_creds_options.pem_cert_chain =
  88. grpc::StringFromCopiedSlice(cert_slice);
  89. grpc_slice_unref(cert_slice);
  90. }
  91. if (!absl::GetFlag(FLAGS_ssl_client_key).empty()) {
  92. grpc_slice key_slice = grpc_empty_slice();
  93. GRPC_LOG_IF_ERROR(
  94. "load_file",
  95. grpc_load_file(absl::GetFlag(FLAGS_ssl_client_key).c_str(), 1,
  96. &key_slice));
  97. ssl_creds_options.pem_private_key =
  98. grpc::StringFromCopiedSlice(key_slice);
  99. grpc_slice_unref(key_slice);
  100. }
  101. return grpc::SslCredentials(ssl_creds_options);
  102. } else if (absl::GetFlag(FLAGS_channel_creds_type) == "gdc") {
  103. return grpc::GoogleDefaultCredentials();
  104. } else if (absl::GetFlag(FLAGS_channel_creds_type) == "alts") {
  105. return grpc::experimental::AltsCredentials(
  106. grpc::experimental::AltsCredentialsOptions());
  107. } else if (absl::GetFlag(FLAGS_channel_creds_type) == "local") {
  108. if (absl::GetFlag(FLAGS_local_connect_type) == "local_tcp") {
  109. return grpc::experimental::LocalCredentials(LOCAL_TCP);
  110. } else if (absl::GetFlag(FLAGS_local_connect_type) == "uds") {
  111. return grpc::experimental::LocalCredentials(UDS);
  112. } else {
  113. fprintf(stderr,
  114. "--local_connect_type=%s invalid; must be local_tcp or uds.\n",
  115. absl::GetFlag(FLAGS_local_connect_type).c_str());
  116. }
  117. }
  118. fprintf(stderr,
  119. "--channel_creds_type=%s invalid; must be insecure, ssl, gdc, "
  120. "alts, or local.\n",
  121. absl::GetFlag(FLAGS_channel_creds_type).c_str());
  122. return std::shared_ptr<grpc::ChannelCredentials>();
  123. }
  124. std::shared_ptr<grpc::CallCredentials> CliCredentials::GetCallCredentials()
  125. const {
  126. if (IsAccessToken(absl::GetFlag(FLAGS_call_creds))) {
  127. return grpc::AccessTokenCredentials(
  128. AccessToken(absl::GetFlag(FLAGS_call_creds)));
  129. }
  130. if (absl::GetFlag(FLAGS_call_creds) == "none") {
  131. // Nothing to do; creds, if any, are baked into the channel.
  132. return std::shared_ptr<grpc::CallCredentials>();
  133. }
  134. fprintf(stderr,
  135. "--call_creds=%s invalid; must be none "
  136. "or access_token=<token>.\n",
  137. absl::GetFlag(FLAGS_call_creds).c_str());
  138. return std::shared_ptr<grpc::CallCredentials>();
  139. }
  140. std::shared_ptr<grpc::ChannelCredentials> CliCredentials::GetCredentials()
  141. const {
  142. if (absl::GetFlag(FLAGS_call_creds).empty()) {
  143. absl::SetFlag(&FLAGS_call_creds, GetDefaultCallCreds());
  144. }
  145. if (absl::GetFlag(FLAGS_channel_creds_type).empty()) {
  146. absl::SetFlag(&FLAGS_channel_creds_type, GetDefaultChannelCredsType());
  147. }
  148. std::shared_ptr<grpc::ChannelCredentials> channel_creds =
  149. GetChannelCredentials();
  150. // Composite any call-type credentials on top of the base channel.
  151. std::shared_ptr<grpc::CallCredentials> call_creds = GetCallCredentials();
  152. return (channel_creds == nullptr || call_creds == nullptr)
  153. ? channel_creds
  154. : grpc::CompositeChannelCredentials(channel_creds, call_creds);
  155. }
  156. std::string CliCredentials::GetCredentialUsage() const {
  157. return " --ssl_target ; Set server host for ssl validation\n"
  158. " --ssl_client_cert ; Client cert for ssl\n"
  159. " --ssl_client_key ; Client private key for ssl\n"
  160. " --local_connect_type ; Set to local_tcp or uds\n"
  161. " --channel_creds_type ; Set to insecure, ssl, gdc, alts, or "
  162. "local\n"
  163. " --call_creds ; Set to none, or"
  164. " access_token=<token>\n";
  165. }
  166. std::string CliCredentials::GetSslTargetNameOverride() const {
  167. bool use_ssl = absl::GetFlag(FLAGS_channel_creds_type) == "ssl" ||
  168. absl::GetFlag(FLAGS_channel_creds_type) == "gdc";
  169. return use_ssl ? absl::GetFlag(FLAGS_ssl_target) : "";
  170. }
  171. } // namespace testing
  172. } // namespace grpc