commit
dc3e1097e9
323 changed files with 9774 additions and 21200 deletions
@ -0,0 +1,40 @@ |
||||
# Copyright 2020 The gRPC authors. |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
"""Hello World without using protoc. |
||||
|
||||
This example parses message and service schemas directly from a |
||||
.proto file on the filesystem. |
||||
|
||||
Several APIs used in this example are in an experimental state. |
||||
""" |
||||
|
||||
from __future__ import print_function |
||||
import logging |
||||
|
||||
import grpc |
||||
import grpc.experimental |
||||
|
||||
# NOTE: The path to the .proto file must be reachable from an entry |
||||
# on sys.path. Use sys.path.insert or set the $PYTHONPATH variable to |
||||
# import from files located elsewhere on the filesystem. |
||||
|
||||
protos = grpc.protos("helloworld.proto") |
||||
services = grpc.services("helloworld.proto") |
||||
|
||||
logging.basicConfig() |
||||
|
||||
response = services.Greeter.SayHello(protos.HelloRequest(name='you'), |
||||
'localhost:50051', |
||||
insecure=True) |
||||
print("Greeter client received: " + response.message) |
@ -0,0 +1,40 @@ |
||||
# Copyright 2020 The gRPC authors. |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
"""The Python implementation of the GRPC helloworld.Greeter server.""" |
||||
|
||||
from concurrent import futures |
||||
import logging |
||||
|
||||
import grpc |
||||
|
||||
protos, services = grpc.protos_and_services("helloworld.proto") |
||||
|
||||
|
||||
class Greeter(services.GreeterServicer): |
||||
|
||||
def SayHello(self, request, context): |
||||
return protos.HelloReply(message='Hello, %s!' % request.name) |
||||
|
||||
|
||||
def serve(): |
||||
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) |
||||
services.add_GreeterServicer_to_server(Greeter(), server) |
||||
server.add_insecure_port('[::]:50051') |
||||
server.start() |
||||
server.wait_for_termination() |
||||
|
||||
|
||||
if __name__ == '__main__': |
||||
logging.basicConfig() |
||||
serve() |
@ -0,0 +1,38 @@ |
||||
// Copyright 2020 The gRPC authors. |
||||
// |
||||
// Licensed under the Apache License, Version 2.0 (the "License"); |
||||
// you may not use this file except in compliance with the License. |
||||
// You may obtain a copy of the License at |
||||
// |
||||
// http://www.apache.org/licenses/LICENSE-2.0 |
||||
// |
||||
// Unless required by applicable law or agreed to in writing, software |
||||
// distributed under the License is distributed on an "AS IS" BASIS, |
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
// See the License for the specific language governing permissions and |
||||
// limitations under the License. |
||||
|
||||
syntax = "proto3"; |
||||
|
||||
option java_multiple_files = true; |
||||
option java_package = "io.grpc.examples.helloworld"; |
||||
option java_outer_classname = "HelloWorldProto"; |
||||
option objc_class_prefix = "HLW"; |
||||
|
||||
package helloworld; |
||||
|
||||
// The greeting service definition. |
||||
service Greeter { |
||||
// Sends a greeting |
||||
rpc SayHello (HelloRequest) returns (HelloReply) {} |
||||
} |
||||
|
||||
// The request message containing the user's name. |
||||
message HelloRequest { |
||||
string name = 1; |
||||
} |
||||
|
||||
// The response message containing the greetings |
||||
message HelloReply { |
||||
string message = 1; |
||||
} |
@ -0,0 +1,22 @@ |
||||
# Welcome to `include/grpc/impl/codegen` |
||||
|
||||
## Why is this directory here? |
||||
|
||||
This directory exists so that generated C++ code can include selected files upon |
||||
which it depends without having to depend on the entire gRPC C++ library. This |
||||
directory thus exists to support `include/grpcpp/impl/codegen`. This constraint |
||||
is particularly relevant for users of bazel, particularly if they use the |
||||
multi-lingual `proto_library` target type. Generated code that uses this target |
||||
only depends on the gRPC C++ targets associated with these header files, not the |
||||
entire gRPC C++ codebase since that would make the build time of these types of |
||||
targets excessively large (particularly when they are not even C++ specific). |
||||
|
||||
## What should user code do? |
||||
|
||||
User code should *not* include anything from this directory. Only generated code |
||||
and gRPC library code should include contents from this directory. C++ user code |
||||
should instead include contents from the main `grpcpp` directory or its |
||||
accessible subcomponents like `grpcpp/support`. It is possible that we may |
||||
remove this directory altogether if the motivations for its existence are no |
||||
longer strong enough (e.g., if the gRPC C++ library no longer has a need for an |
||||
`impl/codegen` directory of its own). |
@ -0,0 +1,21 @@ |
||||
# Welcome to `include/grpcpp/impl/codegen` |
||||
|
||||
## Why is this directory here? |
||||
|
||||
This directory exists so that generated code can include selected files upon |
||||
which it depends without having to depend on the entire gRPC C++ library. This |
||||
is particularly relevant for users of bazel, particularly if they use the |
||||
multi-lingual `proto_library` target type. Generated code that uses this target |
||||
only depends on the gRPC C++ targets associated with these header files, not the |
||||
entire gRPC C++ codebase since that would make the build time of these types of |
||||
targets excessively large (particularly when they are not even C++ specific). |
||||
|
||||
## What should user code do? |
||||
|
||||
User code should *not* include anything from this directory. Only generated code |
||||
and gRPC library code should include contents from this directory. User code |
||||
should instead include contents from the main `grpcpp` directory or its |
||||
accessible subcomponents like `grpcpp/support`. It is possible that we may |
||||
remove this directory altogether if the motivations for its existence are no |
||||
longer strong enough (e.g., if most users migrate away from the `proto_library` |
||||
target type or if the additional overhead of depending on gRPC C++ is not high). |
@ -1,517 +0,0 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015 gRPC authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
* |
||||
*/ |
||||
|
||||
/// A ClientContext allows the person implementing a service client to:
|
||||
///
|
||||
/// - Add custom metadata key-value pairs that will propagated to the server
|
||||
/// side.
|
||||
/// - Control call settings such as compression and authentication.
|
||||
/// - Initial and trailing metadata coming from the server.
|
||||
/// - Get performance metrics (ie, census).
|
||||
///
|
||||
/// Context settings are only relevant to the call they are invoked with, that
|
||||
/// is to say, they aren't sticky. Some of these settings, such as the
|
||||
/// compression options, can be made persistent at channel construction time
|
||||
/// (see \a grpc::CreateCustomChannel).
|
||||
///
|
||||
/// \warning ClientContext instances should \em not be reused across rpcs.
|
||||
|
||||
#ifndef GRPCPP_IMPL_CODEGEN_CLIENT_CONTEXT_IMPL_H |
||||
#define GRPCPP_IMPL_CODEGEN_CLIENT_CONTEXT_IMPL_H |
||||
|
||||
#include <map> |
||||
#include <memory> |
||||
#include <string> |
||||
|
||||
#include <grpc/impl/codegen/compression_types.h> |
||||
#include <grpc/impl/codegen/propagation_bits.h> |
||||
#include <grpcpp/impl/codegen/client_interceptor.h> |
||||
#include <grpcpp/impl/codegen/config.h> |
||||
#include <grpcpp/impl/codegen/core_codegen_interface.h> |
||||
#include <grpcpp/impl/codegen/create_auth_context.h> |
||||
#include <grpcpp/impl/codegen/metadata_map.h> |
||||
#include <grpcpp/impl/codegen/rpc_method.h> |
||||
#include <grpcpp/impl/codegen/security/auth_context.h> |
||||
#include <grpcpp/impl/codegen/slice.h> |
||||
#include <grpcpp/impl/codegen/status.h> |
||||
#include <grpcpp/impl/codegen/string_ref.h> |
||||
#include <grpcpp/impl/codegen/sync.h> |
||||
#include <grpcpp/impl/codegen/time.h> |
||||
|
||||
struct census_context; |
||||
struct grpc_call; |
||||
|
||||
namespace grpc { |
||||
|
||||
class CallbackServerContext; |
||||
class CallCredentials; |
||||
class Channel; |
||||
class ChannelInterface; |
||||
class CompletionQueue; |
||||
class ServerContext; |
||||
class ServerContextBase; |
||||
|
||||
namespace internal { |
||||
class RpcMethod; |
||||
template <class InputMessage, class OutputMessage> |
||||
class BlockingUnaryCallImpl; |
||||
class CallOpClientRecvStatus; |
||||
class CallOpRecvInitialMetadata; |
||||
class ServerContextImpl; |
||||
} // namespace internal
|
||||
|
||||
namespace testing { |
||||
class InteropClientContextInspector; |
||||
} // namespace testing
|
||||
} // namespace grpc
|
||||
namespace grpc_impl { |
||||
|
||||
namespace internal { |
||||
template <class InputMessage, class OutputMessage> |
||||
class CallbackUnaryCallImpl; |
||||
template <class Request, class Response> |
||||
class ClientCallbackReaderWriterImpl; |
||||
template <class Response> |
||||
class ClientCallbackReaderImpl; |
||||
template <class Request> |
||||
class ClientCallbackWriterImpl; |
||||
class ClientCallbackUnaryImpl; |
||||
class ClientContextAccessor; |
||||
} // namespace internal
|
||||
|
||||
template <class R> |
||||
class ClientReader; |
||||
template <class W> |
||||
class ClientWriter; |
||||
template <class W, class R> |
||||
class ClientReaderWriter; |
||||
template <class R> |
||||
class ClientAsyncReader; |
||||
template <class W> |
||||
class ClientAsyncWriter; |
||||
template <class W, class R> |
||||
class ClientAsyncReaderWriter; |
||||
template <class R> |
||||
class ClientAsyncResponseReader; |
||||
|
||||
|
||||
/// Options for \a ClientContext::FromServerContext specifying which traits from
|
||||
/// the \a ServerContext to propagate (copy) from it into a new \a
|
||||
/// ClientContext.
|
||||
///
|
||||
/// \see ClientContext::FromServerContext
|
||||
class PropagationOptions { |
||||
public: |
||||
PropagationOptions() : propagate_(GRPC_PROPAGATE_DEFAULTS) {} |
||||
|
||||
PropagationOptions& enable_deadline_propagation() { |
||||
propagate_ |= GRPC_PROPAGATE_DEADLINE; |
||||
return *this; |
||||
} |
||||
|
||||
PropagationOptions& disable_deadline_propagation() { |
||||
propagate_ &= ~GRPC_PROPAGATE_DEADLINE; |
||||
return *this; |
||||
} |
||||
|
||||
PropagationOptions& enable_census_stats_propagation() { |
||||
propagate_ |= GRPC_PROPAGATE_CENSUS_STATS_CONTEXT; |
||||
return *this; |
||||
} |
||||
|
||||
PropagationOptions& disable_census_stats_propagation() { |
||||
propagate_ &= ~GRPC_PROPAGATE_CENSUS_STATS_CONTEXT; |
||||
return *this; |
||||
} |
||||
|
||||
PropagationOptions& enable_census_tracing_propagation() { |
||||
propagate_ |= GRPC_PROPAGATE_CENSUS_TRACING_CONTEXT; |
||||
return *this; |
||||
} |
||||
|
||||
PropagationOptions& disable_census_tracing_propagation() { |
||||
propagate_ &= ~GRPC_PROPAGATE_CENSUS_TRACING_CONTEXT; |
||||
return *this; |
||||
} |
||||
|
||||
PropagationOptions& enable_cancellation_propagation() { |
||||
propagate_ |= GRPC_PROPAGATE_CANCELLATION; |
||||
return *this; |
||||
} |
||||
|
||||
PropagationOptions& disable_cancellation_propagation() { |
||||
propagate_ &= ~GRPC_PROPAGATE_CANCELLATION; |
||||
return *this; |
||||
} |
||||
|
||||
uint32_t c_bitmask() const { return propagate_; } |
||||
|
||||
private: |
||||
uint32_t propagate_; |
||||
}; |
||||
|
||||
/// A ClientContext allows the person implementing a service client to:
|
||||
///
|
||||
/// - Add custom metadata key-value pairs that will propagated to the server
|
||||
/// side.
|
||||
/// - Control call settings such as compression and authentication.
|
||||
/// - Initial and trailing metadata coming from the server.
|
||||
/// - Get performance metrics (ie, census).
|
||||
///
|
||||
/// Context settings are only relevant to the call they are invoked with, that
|
||||
/// is to say, they aren't sticky. Some of these settings, such as the
|
||||
/// compression options, can be made persistent at channel construction time
|
||||
/// (see \a grpc::CreateCustomChannel).
|
||||
///
|
||||
/// \warning ClientContext instances should \em not be reused across rpcs.
|
||||
/// \warning The ClientContext instance used for creating an rpc must remain
|
||||
/// alive and valid for the lifetime of the rpc.
|
||||
class ClientContext { |
||||
public: |
||||
ClientContext(); |
||||
~ClientContext(); |
||||
|
||||
/// Create a new \a ClientContext as a child of an incoming server call,
|
||||
/// according to \a options (\see PropagationOptions).
|
||||
///
|
||||
/// \param server_context The source server context to use as the basis for
|
||||
/// constructing the client context.
|
||||
/// \param options The options controlling what to copy from the \a
|
||||
/// server_context.
|
||||
///
|
||||
/// \return A newly constructed \a ClientContext instance based on \a
|
||||
/// server_context, with traits propagated (copied) according to \a options.
|
||||
static std::unique_ptr<ClientContext> FromServerContext( |
||||
const grpc::ServerContext& server_context, |
||||
PropagationOptions options = PropagationOptions()); |
||||
static std::unique_ptr<ClientContext> FromCallbackServerContext( |
||||
const grpc::CallbackServerContext& server_context, |
||||
PropagationOptions options = PropagationOptions()); |
||||
|
||||
/// Add the (\a meta_key, \a meta_value) pair to the metadata associated with
|
||||
/// a client call. These are made available at the server side by the \a
|
||||
/// grpc::ServerContext::client_metadata() method.
|
||||
///
|
||||
/// \warning This method should only be called before invoking the rpc.
|
||||
///
|
||||
/// \param meta_key The metadata key. If \a meta_value is binary data, it must
|
||||
/// end in "-bin".
|
||||
/// \param meta_value The metadata value. If its value is binary, the key name
|
||||
/// must end in "-bin".
|
||||
///
|
||||
/// Metadata must conform to the following format:
|
||||
/// Custom-Metadata -> Binary-Header / ASCII-Header
|
||||
/// Binary-Header -> {Header-Name "-bin" } {binary value}
|
||||
/// ASCII-Header -> Header-Name ASCII-Value
|
||||
/// Header-Name -> 1*( %x30-39 / %x61-7A / "_" / "-" / ".") ; 0-9 a-z _ - .
|
||||
/// ASCII-Value -> 1*( %x20-%x7E ) ; space and printable ASCII
|
||||
void AddMetadata(const std::string& meta_key, const std::string& meta_value); |
||||
|
||||
/// Return a collection of initial metadata key-value pairs. Note that keys
|
||||
/// may happen more than once (ie, a \a std::multimap is returned).
|
||||
///
|
||||
/// \warning This method should only be called after initial metadata has been
|
||||
/// received. For streaming calls, see \a
|
||||
/// ClientReaderInterface::WaitForInitialMetadata().
|
||||
///
|
||||
/// \return A multimap of initial metadata key-value pairs from the server.
|
||||
const std::multimap<grpc::string_ref, grpc::string_ref>& |
||||
GetServerInitialMetadata() const { |
||||
GPR_CODEGEN_ASSERT(initial_metadata_received_); |
||||
return *recv_initial_metadata_.map(); |
||||
} |
||||
|
||||
/// Return a collection of trailing metadata key-value pairs. Note that keys
|
||||
/// may happen more than once (ie, a \a std::multimap is returned).
|
||||
///
|
||||
/// \warning This method is only callable once the stream has finished.
|
||||
///
|
||||
/// \return A multimap of metadata trailing key-value pairs from the server.
|
||||
const std::multimap<grpc::string_ref, grpc::string_ref>& |
||||
GetServerTrailingMetadata() const { |
||||
// TODO(yangg) check finished
|
||||
return *trailing_metadata_.map(); |
||||
} |
||||
|
||||
/// Set the deadline for the client call.
|
||||
///
|
||||
/// \warning This method should only be called before invoking the rpc.
|
||||
///
|
||||
/// \param deadline the deadline for the client call. Units are determined by
|
||||
/// the type used. The deadline is an absolute (not relative) time.
|
||||
template <typename T> |
||||
void set_deadline(const T& deadline) { |
||||
grpc::TimePoint<T> deadline_tp(deadline); |
||||
deadline_ = deadline_tp.raw_time(); |
||||
} |
||||
|
||||
/// EXPERIMENTAL: Indicate that this request is idempotent.
|
||||
/// By default, RPCs are assumed to <i>not</i> be idempotent.
|
||||
///
|
||||
/// If true, the gRPC library assumes that it's safe to initiate
|
||||
/// this RPC multiple times.
|
||||
void set_idempotent(bool idempotent) { idempotent_ = idempotent; } |
||||
|
||||
/// EXPERIMENTAL: Set this request to be cacheable.
|
||||
/// If set, grpc is free to use the HTTP GET verb for sending the request,
|
||||
/// with the possibility of receiving a cached response.
|
||||
void set_cacheable(bool cacheable) { cacheable_ = cacheable; } |
||||
|
||||
/// EXPERIMENTAL: Trigger wait-for-ready or not on this request.
|
||||
/// See https://github.com/grpc/grpc/blob/master/doc/wait-for-ready.md.
|
||||
/// If set, if an RPC is made when a channel's connectivity state is
|
||||
/// TRANSIENT_FAILURE or CONNECTING, the call will not "fail fast",
|
||||
/// and the channel will wait until the channel is READY before making the
|
||||
/// call.
|
||||
void set_wait_for_ready(bool wait_for_ready) { |
||||
wait_for_ready_ = wait_for_ready; |
||||
wait_for_ready_explicitly_set_ = true; |
||||
} |
||||
|
||||
/// DEPRECATED: Use set_wait_for_ready() instead.
|
||||
void set_fail_fast(bool fail_fast) { set_wait_for_ready(!fail_fast); } |
||||
|
||||
/// Return the deadline for the client call.
|
||||
std::chrono::system_clock::time_point deadline() const { |
||||
return grpc::Timespec2Timepoint(deadline_); |
||||
} |
||||
|
||||
/// Return a \a gpr_timespec representation of the client call's deadline.
|
||||
gpr_timespec raw_deadline() const { return deadline_; } |
||||
|
||||
/// Set the per call authority header (see
|
||||
/// https://tools.ietf.org/html/rfc7540#section-8.1.2.3).
|
||||
void set_authority(const std::string& authority) { authority_ = authority; } |
||||
|
||||
/// Return the authentication context for the associated client call.
|
||||
/// It is only valid to call this during the lifetime of the client call.
|
||||
///
|
||||
/// \see grpc::AuthContext.
|
||||
std::shared_ptr<const grpc::AuthContext> auth_context() const { |
||||
if (auth_context_.get() == nullptr) { |
||||
auth_context_ = grpc::CreateAuthContext(call_); |
||||
} |
||||
return auth_context_; |
||||
} |
||||
|
||||
/// Set credentials for the client call.
|
||||
///
|
||||
/// A credentials object encapsulates all the state needed by a client to
|
||||
/// authenticate with a server and make various assertions, e.g., about the
|
||||
/// client’s identity, role, or whether it is authorized to make a particular
|
||||
/// call.
|
||||
///
|
||||
/// It is legal to call this only before initial metadata is sent.
|
||||
///
|
||||
/// \see https://grpc.io/docs/guides/auth.html
|
||||
void set_credentials(const std::shared_ptr<grpc::CallCredentials>& creds); |
||||
|
||||
/// EXPERIMENTAL debugging API
|
||||
///
|
||||
/// Returns the credentials for the client call. This should be used only in
|
||||
/// tests and for diagnostic purposes, and should not be used by application
|
||||
/// logic.
|
||||
std::shared_ptr<grpc::CallCredentials> credentials() { return creds_; } |
||||
|
||||
/// Return the compression algorithm the client call will request be used.
|
||||
/// Note that the gRPC runtime may decide to ignore this request, for example,
|
||||
/// due to resource constraints.
|
||||
grpc_compression_algorithm compression_algorithm() const { |
||||
return compression_algorithm_; |
||||
} |
||||
|
||||
/// Set \a algorithm to be the compression algorithm used for the client call.
|
||||
///
|
||||
/// \param algorithm The compression algorithm used for the client call.
|
||||
void set_compression_algorithm(grpc_compression_algorithm algorithm); |
||||
|
||||
/// Flag whether the initial metadata should be \a corked
|
||||
///
|
||||
/// If \a corked is true, then the initial metadata will be coalesced with the
|
||||
/// write of first message in the stream. As a result, any tag set for the
|
||||
/// initial metadata operation (starting a client-streaming or bidi-streaming
|
||||
/// RPC) will not actually be sent to the completion queue or delivered
|
||||
/// via Next.
|
||||
///
|
||||
/// \param corked The flag indicating whether the initial metadata is to be
|
||||
/// corked or not.
|
||||
void set_initial_metadata_corked(bool corked) { |
||||
initial_metadata_corked_ = corked; |
||||
} |
||||
|
||||
/// Return the peer uri in a string.
|
||||
/// It is only valid to call this during the lifetime of the client call.
|
||||
///
|
||||
/// \warning This value is never authenticated or subject to any security
|
||||
/// related code. It must not be used for any authentication related
|
||||
/// functionality. Instead, use auth_context.
|
||||
///
|
||||
/// \return The call's peer URI.
|
||||
std::string peer() const; |
||||
|
||||
/// Sets the census context.
|
||||
/// It is only valid to call this before the client call is created. A common
|
||||
/// place of setting census context is from within the DefaultConstructor
|
||||
/// method of GlobalCallbacks.
|
||||
void set_census_context(struct census_context* ccp) { census_context_ = ccp; } |
||||
|
||||
/// Returns the census context that has been set, or nullptr if not set.
|
||||
struct census_context* census_context() const { |
||||
return census_context_; |
||||
} |
||||
|
||||
/// Send a best-effort out-of-band cancel on the call associated with
|
||||
/// this client context. The call could be in any stage; e.g., if it is
|
||||
/// already finished, it may still return success.
|
||||
///
|
||||
/// There is no guarantee the call will be cancelled.
|
||||
///
|
||||
/// Note that TryCancel() does not change any of the tags that are pending
|
||||
/// on the completion queue. All pending tags will still be delivered
|
||||
/// (though their ok result may reflect the effect of cancellation).
|
||||
void TryCancel(); |
||||
|
||||
/// Global Callbacks
|
||||
///
|
||||
/// Can be set exactly once per application to install hooks whenever
|
||||
/// a client context is constructed and destructed.
|
||||
class GlobalCallbacks { |
||||
public: |
||||
virtual ~GlobalCallbacks() {} |
||||
virtual void DefaultConstructor(ClientContext* context) = 0; |
||||
virtual void Destructor(ClientContext* context) = 0; |
||||
}; |
||||
static void SetGlobalCallbacks(GlobalCallbacks* callbacks); |
||||
|
||||
/// Should be used for framework-level extensions only.
|
||||
/// Applications never need to call this method.
|
||||
grpc_call* c_call() { return call_; } |
||||
|
||||
/// EXPERIMENTAL debugging API
|
||||
///
|
||||
/// if status is not ok() for an RPC, this will return a detailed string
|
||||
/// of the gRPC Core error that led to the failure. It should not be relied
|
||||
/// upon for anything other than gaining more debug data in failure cases.
|
||||
std::string debug_error_string() const { return debug_error_string_; } |
||||
|
||||
private: |
||||
// Disallow copy and assign.
|
||||
ClientContext(const ClientContext&); |
||||
ClientContext& operator=(const ClientContext&); |
||||
|
||||
friend class ::grpc::testing::InteropClientContextInspector; |
||||
friend class ::grpc::internal::CallOpClientRecvStatus; |
||||
friend class ::grpc::internal::CallOpRecvInitialMetadata; |
||||
friend class ::grpc::Channel; |
||||
template <class R> |
||||
friend class ::grpc_impl::ClientReader; |
||||
template <class W> |
||||
friend class ::grpc_impl::ClientWriter; |
||||
template <class W, class R> |
||||
friend class ::grpc_impl::ClientReaderWriter; |
||||
template <class R> |
||||
friend class ::grpc_impl::ClientAsyncReader; |
||||
template <class W> |
||||
friend class ::grpc_impl::ClientAsyncWriter; |
||||
template <class W, class R> |
||||
friend class ::grpc_impl::ClientAsyncReaderWriter; |
||||
template <class R> |
||||
friend class ::grpc_impl::ClientAsyncResponseReader; |
||||
template <class InputMessage, class OutputMessage> |
||||
friend class ::grpc::internal::BlockingUnaryCallImpl; |
||||
template <class InputMessage, class OutputMessage> |
||||
friend class ::grpc_impl::internal::CallbackUnaryCallImpl; |
||||
template <class Request, class Response> |
||||
friend class ::grpc_impl::internal::ClientCallbackReaderWriterImpl; |
||||
template <class Response> |
||||
friend class ::grpc_impl::internal::ClientCallbackReaderImpl; |
||||
template <class Request> |
||||
friend class ::grpc_impl::internal::ClientCallbackWriterImpl; |
||||
friend class ::grpc_impl::internal::ClientCallbackUnaryImpl; |
||||
friend class ::grpc_impl::internal::ClientContextAccessor; |
||||
|
||||
// Used by friend class CallOpClientRecvStatus
|
||||
void set_debug_error_string(const std::string& debug_error_string) { |
||||
debug_error_string_ = debug_error_string; |
||||
} |
||||
|
||||
grpc_call* call() const { return call_; } |
||||
void set_call(grpc_call* call, |
||||
const std::shared_ptr<::grpc::Channel>& channel); |
||||
|
||||
grpc::experimental::ClientRpcInfo* set_client_rpc_info( |
||||
const char* method, grpc::internal::RpcMethod::RpcType type, |
||||
grpc::ChannelInterface* channel, |
||||
const std::vector<std::unique_ptr< |
||||
grpc::experimental::ClientInterceptorFactoryInterface>>& creators, |
||||
size_t interceptor_pos) { |
||||
rpc_info_ = grpc::experimental::ClientRpcInfo(this, type, method, channel); |
||||
rpc_info_.RegisterInterceptors(creators, interceptor_pos); |
||||
return &rpc_info_; |
||||
} |
||||
|
||||
uint32_t initial_metadata_flags() const { |
||||
return (idempotent_ ? GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST : 0) | |
||||
(wait_for_ready_ ? GRPC_INITIAL_METADATA_WAIT_FOR_READY : 0) | |
||||
(cacheable_ ? GRPC_INITIAL_METADATA_CACHEABLE_REQUEST : 0) | |
||||
(wait_for_ready_explicitly_set_ |
||||
? GRPC_INITIAL_METADATA_WAIT_FOR_READY_EXPLICITLY_SET |
||||
: 0) | |
||||
(initial_metadata_corked_ ? GRPC_INITIAL_METADATA_CORKED : 0); |
||||
} |
||||
|
||||
std::string authority() { return authority_; } |
||||
|
||||
void SendCancelToInterceptors(); |
||||
|
||||
static std::unique_ptr<ClientContext> FromInternalServerContext( |
||||
const grpc::ServerContextBase& server_context, |
||||
PropagationOptions options); |
||||
|
||||
bool initial_metadata_received_; |
||||
bool wait_for_ready_; |
||||
bool wait_for_ready_explicitly_set_; |
||||
bool idempotent_; |
||||
bool cacheable_; |
||||
std::shared_ptr<::grpc::Channel> channel_; |
||||
grpc::internal::Mutex mu_; |
||||
grpc_call* call_; |
||||
bool call_canceled_; |
||||
gpr_timespec deadline_; |
||||
grpc::string authority_; |
||||
std::shared_ptr<grpc::CallCredentials> creds_; |
||||
mutable std::shared_ptr<const grpc::AuthContext> auth_context_; |
||||
struct census_context* census_context_; |
||||
std::multimap<std::string, std::string> send_initial_metadata_; |
||||
mutable grpc::internal::MetadataMap recv_initial_metadata_; |
||||
mutable grpc::internal::MetadataMap trailing_metadata_; |
||||
|
||||
grpc_call* propagate_from_call_; |
||||
PropagationOptions propagation_options_; |
||||
|
||||
grpc_compression_algorithm compression_algorithm_; |
||||
bool initial_metadata_corked_; |
||||
|
||||
std::string debug_error_string_; |
||||
|
||||
grpc::experimental::ClientRpcInfo rpc_info_; |
||||
}; |
||||
|
||||
} // namespace grpc_impl
|
||||
|
||||
#endif // GRPCPP_IMPL_CODEGEN_CLIENT_CONTEXT_IMPL_H
|
@ -0,0 +1,43 @@ |
||||
/* This file was generated by upbc (the upb compiler) from the input
|
||||
* file: |
||||
* |
||||
* third_party/istio/security/proto/providers/google/meshca.proto |
||||
* |
||||
* Do not edit -- your changes will be discarded when the file is |
||||
* regenerated. */ |
||||
|
||||
#include <stddef.h> |
||||
#include "upb/msg.h" |
||||
#include "third_party/istio/security/proto/providers/google/meshca.upb.h" |
||||
#include "google/protobuf/duration.upb.h" |
||||
|
||||
#include "upb/port_def.inc" |
||||
|
||||
static const upb_msglayout *const google_security_meshca_v1_MeshCertificateRequest_submsgs[1] = { |
||||
&google_protobuf_Duration_msginit, |
||||
}; |
||||
|
||||
static const upb_msglayout_field google_security_meshca_v1_MeshCertificateRequest__fields[3] = { |
||||
{1, UPB_SIZE(0, 0), 0, 0, 9, 1}, |
||||
{2, UPB_SIZE(8, 16), 0, 0, 9, 1}, |
||||
{3, UPB_SIZE(16, 32), 0, 0, 11, 1}, |
||||
}; |
||||
|
||||
const upb_msglayout google_security_meshca_v1_MeshCertificateRequest_msginit = { |
||||
&google_security_meshca_v1_MeshCertificateRequest_submsgs[0], |
||||
&google_security_meshca_v1_MeshCertificateRequest__fields[0], |
||||
UPB_SIZE(24, 48), 3, false, |
||||
}; |
||||
|
||||
static const upb_msglayout_field google_security_meshca_v1_MeshCertificateResponse__fields[1] = { |
||||
{1, UPB_SIZE(0, 0), 0, 0, 9, 3}, |
||||
}; |
||||
|
||||
const upb_msglayout google_security_meshca_v1_MeshCertificateResponse_msginit = { |
||||
NULL, |
||||
&google_security_meshca_v1_MeshCertificateResponse__fields[0], |
||||
UPB_SIZE(4, 8), 1, false, |
||||
}; |
||||
|
||||
#include "upb/port_undef.inc" |
||||
|
@ -0,0 +1,103 @@ |
||||
/* This file was generated by upbc (the upb compiler) from the input
|
||||
* file: |
||||
* |
||||
* third_party/istio/security/proto/providers/google/meshca.proto |
||||
* |
||||
* Do not edit -- your changes will be discarded when the file is |
||||
* regenerated. */ |
||||
|
||||
#ifndef THIRD_PARTY_ISTIO_SECURITY_PROTO_PROVIDERS_GOOGLE_MESHCA_PROTO_UPB_H_ |
||||
#define THIRD_PARTY_ISTIO_SECURITY_PROTO_PROVIDERS_GOOGLE_MESHCA_PROTO_UPB_H_ |
||||
|
||||
#include "upb/msg.h" |
||||
#include "upb/decode.h" |
||||
#include "upb/encode.h" |
||||
|
||||
#include "upb/port_def.inc" |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
struct google_security_meshca_v1_MeshCertificateRequest; |
||||
struct google_security_meshca_v1_MeshCertificateResponse; |
||||
typedef struct google_security_meshca_v1_MeshCertificateRequest google_security_meshca_v1_MeshCertificateRequest; |
||||
typedef struct google_security_meshca_v1_MeshCertificateResponse google_security_meshca_v1_MeshCertificateResponse; |
||||
extern const upb_msglayout google_security_meshca_v1_MeshCertificateRequest_msginit; |
||||
extern const upb_msglayout google_security_meshca_v1_MeshCertificateResponse_msginit; |
||||
struct google_protobuf_Duration; |
||||
extern const upb_msglayout google_protobuf_Duration_msginit; |
||||
|
||||
|
||||
/* google.security.meshca.v1.MeshCertificateRequest */ |
||||
|
||||
UPB_INLINE google_security_meshca_v1_MeshCertificateRequest *google_security_meshca_v1_MeshCertificateRequest_new(upb_arena *arena) { |
||||
return (google_security_meshca_v1_MeshCertificateRequest *)_upb_msg_new(&google_security_meshca_v1_MeshCertificateRequest_msginit, arena); |
||||
} |
||||
UPB_INLINE google_security_meshca_v1_MeshCertificateRequest *google_security_meshca_v1_MeshCertificateRequest_parse(const char *buf, size_t size, |
||||
upb_arena *arena) { |
||||
google_security_meshca_v1_MeshCertificateRequest *ret = google_security_meshca_v1_MeshCertificateRequest_new(arena); |
||||
return (ret && upb_decode(buf, size, ret, &google_security_meshca_v1_MeshCertificateRequest_msginit, arena)) ? ret : NULL; |
||||
} |
||||
UPB_INLINE char *google_security_meshca_v1_MeshCertificateRequest_serialize(const google_security_meshca_v1_MeshCertificateRequest *msg, upb_arena *arena, size_t *len) { |
||||
return upb_encode(msg, &google_security_meshca_v1_MeshCertificateRequest_msginit, arena, len); |
||||
} |
||||
|
||||
UPB_INLINE upb_strview google_security_meshca_v1_MeshCertificateRequest_request_id(const google_security_meshca_v1_MeshCertificateRequest *msg) { return *UPB_PTR_AT(msg, UPB_SIZE(0, 0), upb_strview); } |
||||
UPB_INLINE upb_strview google_security_meshca_v1_MeshCertificateRequest_csr(const google_security_meshca_v1_MeshCertificateRequest *msg) { return *UPB_PTR_AT(msg, UPB_SIZE(8, 16), upb_strview); } |
||||
UPB_INLINE bool google_security_meshca_v1_MeshCertificateRequest_has_validity(const google_security_meshca_v1_MeshCertificateRequest *msg) { return _upb_has_submsg_nohasbit(msg, UPB_SIZE(16, 32)); } |
||||
UPB_INLINE const struct google_protobuf_Duration* google_security_meshca_v1_MeshCertificateRequest_validity(const google_security_meshca_v1_MeshCertificateRequest *msg) { return *UPB_PTR_AT(msg, UPB_SIZE(16, 32), const struct google_protobuf_Duration*); } |
||||
|
||||
UPB_INLINE void google_security_meshca_v1_MeshCertificateRequest_set_request_id(google_security_meshca_v1_MeshCertificateRequest *msg, upb_strview value) { |
||||
*UPB_PTR_AT(msg, UPB_SIZE(0, 0), upb_strview) = value; |
||||
} |
||||
UPB_INLINE void google_security_meshca_v1_MeshCertificateRequest_set_csr(google_security_meshca_v1_MeshCertificateRequest *msg, upb_strview value) { |
||||
*UPB_PTR_AT(msg, UPB_SIZE(8, 16), upb_strview) = value; |
||||
} |
||||
UPB_INLINE void google_security_meshca_v1_MeshCertificateRequest_set_validity(google_security_meshca_v1_MeshCertificateRequest *msg, struct google_protobuf_Duration* value) { |
||||
*UPB_PTR_AT(msg, UPB_SIZE(16, 32), struct google_protobuf_Duration*) = value; |
||||
} |
||||
UPB_INLINE struct google_protobuf_Duration* google_security_meshca_v1_MeshCertificateRequest_mutable_validity(google_security_meshca_v1_MeshCertificateRequest *msg, upb_arena *arena) { |
||||
struct google_protobuf_Duration* sub = (struct google_protobuf_Duration*)google_security_meshca_v1_MeshCertificateRequest_validity(msg); |
||||
if (sub == NULL) { |
||||
sub = (struct google_protobuf_Duration*)_upb_msg_new(&google_protobuf_Duration_msginit, arena); |
||||
if (!sub) return NULL; |
||||
google_security_meshca_v1_MeshCertificateRequest_set_validity(msg, sub); |
||||
} |
||||
return sub; |
||||
} |
||||
|
||||
/* google.security.meshca.v1.MeshCertificateResponse */ |
||||
|
||||
UPB_INLINE google_security_meshca_v1_MeshCertificateResponse *google_security_meshca_v1_MeshCertificateResponse_new(upb_arena *arena) { |
||||
return (google_security_meshca_v1_MeshCertificateResponse *)_upb_msg_new(&google_security_meshca_v1_MeshCertificateResponse_msginit, arena); |
||||
} |
||||
UPB_INLINE google_security_meshca_v1_MeshCertificateResponse *google_security_meshca_v1_MeshCertificateResponse_parse(const char *buf, size_t size, |
||||
upb_arena *arena) { |
||||
google_security_meshca_v1_MeshCertificateResponse *ret = google_security_meshca_v1_MeshCertificateResponse_new(arena); |
||||
return (ret && upb_decode(buf, size, ret, &google_security_meshca_v1_MeshCertificateResponse_msginit, arena)) ? ret : NULL; |
||||
} |
||||
UPB_INLINE char *google_security_meshca_v1_MeshCertificateResponse_serialize(const google_security_meshca_v1_MeshCertificateResponse *msg, upb_arena *arena, size_t *len) { |
||||
return upb_encode(msg, &google_security_meshca_v1_MeshCertificateResponse_msginit, arena, len); |
||||
} |
||||
|
||||
UPB_INLINE upb_strview const* google_security_meshca_v1_MeshCertificateResponse_cert_chain(const google_security_meshca_v1_MeshCertificateResponse *msg, size_t *len) { return (upb_strview const*)_upb_array_accessor(msg, UPB_SIZE(0, 0), len); } |
||||
|
||||
UPB_INLINE upb_strview* google_security_meshca_v1_MeshCertificateResponse_mutable_cert_chain(google_security_meshca_v1_MeshCertificateResponse *msg, size_t *len) { |
||||
return (upb_strview*)_upb_array_mutable_accessor(msg, UPB_SIZE(0, 0), len); |
||||
} |
||||
UPB_INLINE upb_strview* google_security_meshca_v1_MeshCertificateResponse_resize_cert_chain(google_security_meshca_v1_MeshCertificateResponse *msg, size_t len, upb_arena *arena) { |
||||
return (upb_strview*)_upb_array_resize_accessor(msg, UPB_SIZE(0, 0), len, UPB_TYPE_STRING, arena); |
||||
} |
||||
UPB_INLINE bool google_security_meshca_v1_MeshCertificateResponse_add_cert_chain(google_security_meshca_v1_MeshCertificateResponse *msg, upb_strview val, upb_arena *arena) { |
||||
return _upb_array_append_accessor(msg, UPB_SIZE(0, 0), UPB_SIZE(8, 16), UPB_TYPE_STRING, &val, |
||||
arena); |
||||
} |
||||
|
||||
#ifdef __cplusplus |
||||
} /* extern "C" */ |
||||
#endif |
||||
|
||||
#include "upb/port_undef.inc" |
||||
|
||||
#endif /* THIRD_PARTY_ISTIO_SECURITY_PROTO_PROVIDERS_GOOGLE_MESHCA_PROTO_UPB_H_ */ |
@ -0,0 +1,153 @@ |
||||
//
|
||||
//
|
||||
// Copyright 2020 gRPC authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
//
|
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include "src/core/lib/security/authorization/evaluate_args.h" |
||||
|
||||
#include "src/core/lib/iomgr/parse_address.h" |
||||
#include "src/core/lib/iomgr/resolve_address.h" |
||||
#include "src/core/lib/iomgr/sockaddr_utils.h" |
||||
#include "src/core/lib/slice/slice_utils.h" |
||||
|
||||
namespace grpc_core { |
||||
|
||||
absl::string_view EvaluateArgs::GetPath() const { |
||||
absl::string_view path; |
||||
if (metadata_ != nullptr && metadata_->idx.named.path != nullptr) { |
||||
grpc_linked_mdelem* elem = metadata_->idx.named.path; |
||||
const grpc_slice& val = GRPC_MDVALUE(elem->md); |
||||
path = StringViewFromSlice(val); |
||||
} |
||||
return path; |
||||
} |
||||
|
||||
absl::string_view EvaluateArgs::GetHost() const { |
||||
absl::string_view host; |
||||
if (metadata_ != nullptr && metadata_->idx.named.host != nullptr) { |
||||
grpc_linked_mdelem* elem = metadata_->idx.named.host; |
||||
const grpc_slice& val = GRPC_MDVALUE(elem->md); |
||||
host = StringViewFromSlice(val); |
||||
} |
||||
return host; |
||||
} |
||||
|
||||
absl::string_view EvaluateArgs::GetMethod() const { |
||||
absl::string_view method; |
||||
if (metadata_ != nullptr && metadata_->idx.named.method != nullptr) { |
||||
grpc_linked_mdelem* elem = metadata_->idx.named.method; |
||||
const grpc_slice& val = GRPC_MDVALUE(elem->md); |
||||
method = StringViewFromSlice(val); |
||||
} |
||||
return method; |
||||
} |
||||
|
||||
std::multimap<absl::string_view, absl::string_view> EvaluateArgs::GetHeaders() |
||||
const { |
||||
std::multimap<absl::string_view, absl::string_view> headers; |
||||
if (metadata_ == nullptr) { |
||||
return headers; |
||||
} |
||||
for (grpc_linked_mdelem* elem = metadata_->list.head; elem != nullptr; |
||||
elem = elem->next) { |
||||
const grpc_slice& key = GRPC_MDKEY(elem->md); |
||||
const grpc_slice& val = GRPC_MDVALUE(elem->md); |
||||
headers.emplace(StringViewFromSlice(key), StringViewFromSlice(val)); |
||||
} |
||||
return headers; |
||||
} |
||||
|
||||
absl::string_view EvaluateArgs::GetLocalAddress() const { |
||||
absl::string_view addr = grpc_endpoint_get_local_address(endpoint_); |
||||
size_t first_colon = addr.find(":"); |
||||
size_t last_colon = addr.rfind(":"); |
||||
if (first_colon == std::string::npos || last_colon == std::string::npos) { |
||||
return ""; |
||||
} else { |
||||
return addr.substr(first_colon + 1, last_colon - first_colon - 1); |
||||
} |
||||
} |
||||
|
||||
int EvaluateArgs::GetLocalPort() const { |
||||
if (endpoint_ == nullptr) { |
||||
return 0; |
||||
} |
||||
grpc_uri* uri = grpc_uri_parse( |
||||
std::string(grpc_endpoint_get_local_address(endpoint_)).c_str(), true); |
||||
grpc_resolved_address resolved_addr; |
||||
if (uri == nullptr || !grpc_parse_uri(uri, &resolved_addr)) { |
||||
grpc_uri_destroy(uri); |
||||
return 0; |
||||
} |
||||
grpc_uri_destroy(uri); |
||||
return grpc_sockaddr_get_port(&resolved_addr); |
||||
} |
||||
|
||||
absl::string_view EvaluateArgs::GetPeerAddress() const { |
||||
absl::string_view addr = grpc_endpoint_get_peer(endpoint_); |
||||
size_t first_colon = addr.find(":"); |
||||
size_t last_colon = addr.rfind(":"); |
||||
if (first_colon == std::string::npos || last_colon == std::string::npos) { |
||||
return ""; |
||||
} else { |
||||
return addr.substr(first_colon + 1, last_colon - first_colon - 1); |
||||
} |
||||
} |
||||
|
||||
int EvaluateArgs::GetPeerPort() const { |
||||
if (endpoint_ == nullptr) { |
||||
return 0; |
||||
} |
||||
grpc_uri* uri = grpc_uri_parse( |
||||
std::string(grpc_endpoint_get_peer(endpoint_)).c_str(), true); |
||||
grpc_resolved_address resolved_addr; |
||||
if (uri == nullptr || !grpc_parse_uri(uri, &resolved_addr)) { |
||||
grpc_uri_destroy(uri); |
||||
return 0; |
||||
} |
||||
grpc_uri_destroy(uri); |
||||
return grpc_sockaddr_get_port(&resolved_addr); |
||||
} |
||||
|
||||
absl::string_view EvaluateArgs::GetSpiffeId() const { |
||||
if (auth_context_ == nullptr) { |
||||
return ""; |
||||
} |
||||
grpc_auth_property_iterator it = grpc_auth_context_find_properties_by_name( |
||||
auth_context_, GRPC_PEER_SPIFFE_ID_PROPERTY_NAME); |
||||
const grpc_auth_property* prop = grpc_auth_property_iterator_next(&it); |
||||
if (prop == nullptr || grpc_auth_property_iterator_next(&it) != nullptr) { |
||||
return ""; |
||||
} |
||||
return absl::string_view(prop->value, prop->value_length); |
||||
} |
||||
|
||||
absl::string_view EvaluateArgs::GetCertServerName() const { |
||||
if (auth_context_ == nullptr) { |
||||
return ""; |
||||
} |
||||
grpc_auth_property_iterator it = grpc_auth_context_find_properties_by_name( |
||||
auth_context_, GRPC_X509_CN_PROPERTY_NAME); |
||||
const grpc_auth_property* prop = grpc_auth_property_iterator_next(&it); |
||||
if (prop == nullptr || grpc_auth_property_iterator_next(&it) != nullptr) { |
||||
return ""; |
||||
} |
||||
return absl::string_view(prop->value, prop->value_length); |
||||
} |
||||
|
||||
} // namespace grpc_core
|
@ -0,0 +1,59 @@ |
||||
//
|
||||
//
|
||||
// Copyright 2020 gRPC authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
//
|
||||
|
||||
#ifndef GRPC_CORE_LIB_SECURITY_AUTHORIZATION_EVALUATE_ARGS_H |
||||
#define GRPC_CORE_LIB_SECURITY_AUTHORIZATION_EVALUATE_ARGS_H |
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include <map> |
||||
|
||||
#include "src/core/lib/iomgr/endpoint.h" |
||||
#include "src/core/lib/security/context/security_context.h" |
||||
#include "src/core/lib/transport/metadata_batch.h" |
||||
|
||||
namespace grpc_core { |
||||
|
||||
class EvaluateArgs { |
||||
public: |
||||
EvaluateArgs(grpc_metadata_batch* metadata, grpc_auth_context* auth_context, |
||||
grpc_endpoint* endpoint) |
||||
: metadata_(metadata), auth_context_(auth_context), endpoint_(endpoint) {} |
||||
|
||||
absl::string_view GetPath() const; |
||||
absl::string_view GetHost() const; |
||||
absl::string_view GetMethod() const; |
||||
std::multimap<absl::string_view, absl::string_view> GetHeaders() const; |
||||
absl::string_view GetLocalAddress() const; |
||||
int GetLocalPort() const; |
||||
absl::string_view GetPeerAddress() const; |
||||
int GetPeerPort() const; |
||||
absl::string_view GetSpiffeId() const; |
||||
absl::string_view GetCertServerName() const; |
||||
|
||||
// TODO: Add a getter function for source.principal
|
||||
|
||||
private: |
||||
grpc_metadata_batch* metadata_; |
||||
grpc_auth_context* auth_context_; |
||||
grpc_endpoint* endpoint_; |
||||
}; |
||||
|
||||
} // namespace grpc_core
|
||||
|
||||
#endif // GRPC_CORE_LIB_SECURITY_AUTHORIZATION_EVALUATE_ARGS_H
|
@ -0,0 +1,90 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2020 The gRPC Authors |
||||
// |
||||
// Licensed under the Apache License, Version 2.0 (the "License"); |
||||
// you may not use this file except in compliance with the License. |
||||
// You may obtain a copy of the License at |
||||
// |
||||
// http://www.apache.org/licenses/LICENSE-2.0 |
||||
// |
||||
// Unless required by applicable law or agreed to in writing, software |
||||
// distributed under the License is distributed on an "AS IS" BASIS, |
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
// See the License for the specific language governing permissions and |
||||
// limitations under the License. |
||||
|
||||
#endregion |
||||
|
||||
using System; |
||||
using System.Threading.Tasks; |
||||
using System.IO; |
||||
using System.Linq; |
||||
using Grpc.Core; |
||||
using Grpc.Core.Internal; |
||||
using Grpc.Core.Utils; |
||||
using NUnit.Framework; |
||||
|
||||
namespace Grpc.Core.Tests |
||||
{ |
||||
public class ServerBindFailedTest |
||||
{ |
||||
Method<string, string> UnimplementedMethod = new Method<string, string>( |
||||
MethodType.Unary, |
||||
"FooService", |
||||
"SomeNonExistentMethod", |
||||
Marshallers.StringMarshaller, |
||||
Marshallers.StringMarshaller); |
||||
|
||||
// https://github.com/grpc/grpc/issues/18100 |
||||
[Test] |
||||
public async Task Issue18100() |
||||
{ |
||||
var server = new Server(new[] { new ChannelOption(ChannelOptions.SoReuseport, 0) }); |
||||
|
||||
// this port will successfully bind |
||||
int successfullyBoundPort = server.Ports.Add(new ServerPort("localhost", ServerPort.PickUnused, ServerCredentials.Insecure)); |
||||
Assert.AreNotEqual(0, successfullyBoundPort); |
||||
|
||||
// use bad ssl server credentials so this port is guaranteed to fail to bind |
||||
Assert.AreEqual(0, server.Ports.Add(new ServerPort("localhost", ServerPort.PickUnused, MakeBadSslServerCredentials()))); |
||||
|
||||
try |
||||
{ |
||||
server.Start(); |
||||
} |
||||
catch (IOException ex) |
||||
{ |
||||
// eat the expected "Failed to bind port" exception. |
||||
Console.Error.WriteLine($"Ignoring expected exception when starting the server: {ex}"); |
||||
} |
||||
|
||||
// Create a channel to the port that has been bound successfully |
||||
var channel = new Channel("localhost", successfullyBoundPort, ChannelCredentials.Insecure); |
||||
|
||||
var callDeadline = DateTime.UtcNow.AddSeconds(5); // set deadline to make sure we fail quickly if the server doesn't respond |
||||
|
||||
// call a method that's not implemented on the server. |
||||
var call = Calls.AsyncUnaryCall(new CallInvocationDetails<string, string>(channel, UnimplementedMethod, new CallOptions(deadline: callDeadline)), "someRequest"); |
||||
try |
||||
{ |
||||
await call; |
||||
Assert.Fail("the call should have failed."); |
||||
} |
||||
catch (RpcException) |
||||
{ |
||||
// We called a nonexistent method. A healthy server should immediately respond with StatusCode.Unimplemented |
||||
Assert.AreEqual(StatusCode.Unimplemented, call.GetStatus().StatusCode); |
||||
} |
||||
|
||||
await channel.ShutdownAsync(); |
||||
await server.ShutdownAsync(); |
||||
} |
||||
|
||||
private static SslServerCredentials MakeBadSslServerCredentials() |
||||
{ |
||||
var serverCert = new[] { new KeyCertificatePair("this is a bad certificate chain", "this is a bad private key") }; |
||||
return new SslServerCredentials(serverCert, "this is a bad root set", forceClientAuth: false); |
||||
} |
||||
} |
||||
} |
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue