secure_server_credentials.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. *
  3. * Copyright 2015 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 <functional>
  19. #include <map>
  20. #include <memory>
  21. #include <grpcpp/impl/codegen/slice.h>
  22. #include <grpcpp/security/auth_metadata_processor.h>
  23. #include "src/cpp/common/secure_auth_context.h"
  24. #include "src/cpp/server/secure_server_credentials.h"
  25. namespace grpc {
  26. void AuthMetadataProcessorAyncWrapper::Destroy(void* wrapper) {
  27. auto* w = static_cast<AuthMetadataProcessorAyncWrapper*>(wrapper);
  28. delete w;
  29. }
  30. void AuthMetadataProcessorAyncWrapper::Process(
  31. void* wrapper, grpc_auth_context* context, const grpc_metadata* md,
  32. size_t num_md, grpc_process_auth_metadata_done_cb cb, void* user_data) {
  33. auto* w = static_cast<AuthMetadataProcessorAyncWrapper*>(wrapper);
  34. if (!w->processor_) {
  35. // Early exit.
  36. cb(user_data, nullptr, 0, nullptr, 0, GRPC_STATUS_OK, nullptr);
  37. return;
  38. }
  39. if (w->processor_->IsBlocking()) {
  40. w->thread_pool_->Add([w, context, md, num_md, cb, user_data] {
  41. w->AuthMetadataProcessorAyncWrapper::InvokeProcessor(context, md, num_md,
  42. cb, user_data);
  43. });
  44. } else {
  45. // invoke directly.
  46. w->InvokeProcessor(context, md, num_md, cb, user_data);
  47. }
  48. }
  49. void AuthMetadataProcessorAyncWrapper::InvokeProcessor(
  50. grpc_auth_context* ctx, const grpc_metadata* md, size_t num_md,
  51. grpc_process_auth_metadata_done_cb cb, void* user_data) {
  52. AuthMetadataProcessor::InputMetadata metadata;
  53. for (size_t i = 0; i < num_md; i++) {
  54. metadata.insert(std::make_pair(StringRefFromSlice(&md[i].key),
  55. StringRefFromSlice(&md[i].value)));
  56. }
  57. SecureAuthContext context(ctx);
  58. AuthMetadataProcessor::OutputMetadata consumed_metadata;
  59. AuthMetadataProcessor::OutputMetadata response_metadata;
  60. Status status = processor_->Process(metadata, &context, &consumed_metadata,
  61. &response_metadata);
  62. std::vector<grpc_metadata> consumed_md;
  63. for (const auto& consumed : consumed_metadata) {
  64. grpc_metadata md_entry;
  65. md_entry.key = SliceReferencingString(consumed.first);
  66. md_entry.value = SliceReferencingString(consumed.second);
  67. md_entry.flags = 0;
  68. consumed_md.push_back(md_entry);
  69. }
  70. std::vector<grpc_metadata> response_md;
  71. for (const auto& response : response_metadata) {
  72. grpc_metadata md_entry;
  73. md_entry.key = SliceReferencingString(response.first);
  74. md_entry.value = SliceReferencingString(response.second);
  75. md_entry.flags = 0;
  76. response_md.push_back(md_entry);
  77. }
  78. auto consumed_md_data = consumed_md.empty() ? nullptr : &consumed_md[0];
  79. auto response_md_data = response_md.empty() ? nullptr : &response_md[0];
  80. cb(user_data, consumed_md_data, consumed_md.size(), response_md_data,
  81. response_md.size(), static_cast<grpc_status_code>(status.error_code()),
  82. status.error_message().c_str());
  83. }
  84. } // namespace grpc
  85. namespace grpc_impl {
  86. int SecureServerCredentials::AddPortToServer(const grpc::string& addr,
  87. grpc_server* server) {
  88. return grpc_server_add_secure_http2_port(server, addr.c_str(), creds_);
  89. }
  90. void SecureServerCredentials::SetAuthMetadataProcessor(
  91. const std::shared_ptr<grpc::AuthMetadataProcessor>& processor) {
  92. auto* wrapper = new grpc::AuthMetadataProcessorAyncWrapper(processor);
  93. grpc_server_credentials_set_auth_metadata_processor(
  94. creds_, {grpc::AuthMetadataProcessorAyncWrapper::Process,
  95. grpc::AuthMetadataProcessorAyncWrapper::Destroy, wrapper});
  96. }
  97. std::shared_ptr<ServerCredentials> SslServerCredentials(
  98. const grpc::SslServerCredentialsOptions& options) {
  99. std::vector<grpc_ssl_pem_key_cert_pair> pem_key_cert_pairs;
  100. for (const auto& key_cert_pair : options.pem_key_cert_pairs) {
  101. grpc_ssl_pem_key_cert_pair p = {key_cert_pair.private_key.c_str(),
  102. key_cert_pair.cert_chain.c_str()};
  103. pem_key_cert_pairs.push_back(p);
  104. }
  105. grpc_server_credentials* c_creds = grpc_ssl_server_credentials_create_ex(
  106. options.pem_root_certs.empty() ? nullptr : options.pem_root_certs.c_str(),
  107. pem_key_cert_pairs.empty() ? nullptr : &pem_key_cert_pairs[0],
  108. pem_key_cert_pairs.size(),
  109. options.force_client_auth
  110. ? GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY
  111. : options.client_certificate_request,
  112. nullptr);
  113. return std::shared_ptr<ServerCredentials>(
  114. new SecureServerCredentials(c_creds));
  115. }
  116. namespace experimental {
  117. std::shared_ptr<ServerCredentials> AltsServerCredentials(
  118. const AltsServerCredentialsOptions& /* options */) {
  119. grpc_alts_credentials_options* c_options =
  120. grpc_alts_credentials_server_options_create();
  121. grpc_server_credentials* c_creds =
  122. grpc_alts_server_credentials_create(c_options);
  123. grpc_alts_credentials_options_destroy(c_options);
  124. return std::shared_ptr<ServerCredentials>(
  125. new SecureServerCredentials(c_creds));
  126. }
  127. std::shared_ptr<ServerCredentials> LocalServerCredentials(
  128. grpc_local_connect_type type) {
  129. return std::shared_ptr<ServerCredentials>(
  130. new SecureServerCredentials(grpc_local_server_credentials_create(type)));
  131. }
  132. std::shared_ptr<ServerCredentials> TlsServerCredentials(
  133. const TlsCredentialsOptions& options) {
  134. return std::shared_ptr<ServerCredentials>(
  135. new SecureServerCredentials(grpc_tls_spiffe_server_credentials_create(
  136. options.c_credentials_options())));
  137. }
  138. } // namespace experimental
  139. } // namespace grpc_impl