mirror of https://github.com/grpc/grpc.git
commit
e945cd8064
145 changed files with 3678 additions and 1291 deletions
@ -1,296 +0,0 @@ |
||||
# GRPC gyp file |
||||
# This currently builds C code. |
||||
# This file has been automatically generated from a template file. |
||||
# Please look at the templates directory instead. |
||||
# This file can be regenerated from the template by running |
||||
# tools/buildgen/generate_projects.sh |
||||
|
||||
# Copyright 2015, Google Inc. |
||||
# All rights reserved. |
||||
# |
||||
# Redistribution and use in source and binary forms, with or without |
||||
# modification, are permitted provided that the following conditions are |
||||
# met: |
||||
# |
||||
# * Redistributions of source code must retain the above copyright |
||||
# notice, this list of conditions and the following disclaimer. |
||||
# * Redistributions in binary form must reproduce the above |
||||
# copyright notice, this list of conditions and the following disclaimer |
||||
# in the documentation and/or other materials provided with the |
||||
# distribution. |
||||
# * Neither the name of Google Inc. nor the names of its |
||||
# contributors may be used to endorse or promote products derived from |
||||
# this software without specific prior written permission. |
||||
# |
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
# Some of this file is built with the help of |
||||
# https://n8.io/converting-a-c-library-to-gyp/ |
||||
{ |
||||
# TODO: Finish windows support |
||||
'target_defaults': { |
||||
'default_configuration': 'Debug', |
||||
'configurations': { |
||||
'Debug': { |
||||
'defines': [ 'DEBUG', '_DEBUG' ], |
||||
'msvs_settings': { |
||||
'VCCLCompilerTool': { |
||||
'RuntimeLibrary': 1, # static debug |
||||
}, |
||||
}, |
||||
}, |
||||
'Release': { |
||||
'defines': [ 'NDEBUG' ], |
||||
'msvs_settings': { |
||||
'VCCLCompilerTool': { |
||||
'RuntimeLibrary': 0, # static release |
||||
}, |
||||
}, |
||||
} |
||||
}, |
||||
'msvs_settings': { |
||||
'VCLinkerTool': { |
||||
'GenerateDebugInformation': 'true', |
||||
}, |
||||
}, |
||||
# TODO: Add fallback for Windows, and if pkg-config is not available |
||||
'defines': [ |
||||
'TSI_OPENSSL_ALPN_SUPPORT=<!(pkg-config --atleast-version=1.0.2 openssl >/dev/null 2>&1 && echo 1 || echo 0)' |
||||
], |
||||
'include_dirs': [ |
||||
'.', |
||||
'include' |
||||
], |
||||
# TODO: Check for libraries with pkg-config |
||||
'libraries': [ |
||||
'-lcrypto', |
||||
'-lssl', |
||||
'-ldl', |
||||
'-lpthread', |
||||
'-lz' |
||||
], |
||||
'direct_dependent_settings': { |
||||
'include_dirs': [ |
||||
'.', |
||||
'include' |
||||
], |
||||
} |
||||
}, |
||||
'targets': [ |
||||
{ |
||||
'target_name': 'gpr', |
||||
'product_prefix': 'lib', |
||||
'type': 'static_library', |
||||
'dependencies': [ |
||||
], |
||||
'sources': [ |
||||
'src/core/support/alloc.c', |
||||
'src/core/support/cmdline.c', |
||||
'src/core/support/cpu_iphone.c', |
||||
'src/core/support/cpu_linux.c', |
||||
'src/core/support/cpu_posix.c', |
||||
'src/core/support/cpu_windows.c', |
||||
'src/core/support/env_linux.c', |
||||
'src/core/support/env_posix.c', |
||||
'src/core/support/env_win32.c', |
||||
'src/core/support/file.c', |
||||
'src/core/support/file_posix.c', |
||||
'src/core/support/file_win32.c', |
||||
'src/core/support/histogram.c', |
||||
'src/core/support/host_port.c', |
||||
'src/core/support/log.c', |
||||
'src/core/support/log_android.c', |
||||
'src/core/support/log_linux.c', |
||||
'src/core/support/log_posix.c', |
||||
'src/core/support/log_win32.c', |
||||
'src/core/support/murmur_hash.c', |
||||
'src/core/support/slice.c', |
||||
'src/core/support/slice_buffer.c', |
||||
'src/core/support/stack_lockfree.c', |
||||
'src/core/support/string.c', |
||||
'src/core/support/string_posix.c', |
||||
'src/core/support/string_win32.c', |
||||
'src/core/support/subprocess_posix.c', |
||||
'src/core/support/sync.c', |
||||
'src/core/support/sync_posix.c', |
||||
'src/core/support/sync_win32.c', |
||||
'src/core/support/thd.c', |
||||
'src/core/support/thd_posix.c', |
||||
'src/core/support/thd_win32.c', |
||||
'src/core/support/time.c', |
||||
'src/core/support/time_posix.c', |
||||
'src/core/support/time_win32.c', |
||||
'src/core/support/tls_pthread.c', |
||||
], |
||||
}, |
||||
{ |
||||
'target_name': 'grpc', |
||||
'product_prefix': 'lib', |
||||
'type': 'static_library', |
||||
'dependencies': [ |
||||
'gpr', |
||||
], |
||||
'sources': [ |
||||
'src/core/httpcli/httpcli_security_connector.c', |
||||
'src/core/security/base64.c', |
||||
'src/core/security/client_auth_filter.c', |
||||
'src/core/security/credentials.c', |
||||
'src/core/security/credentials_metadata.c', |
||||
'src/core/security/credentials_posix.c', |
||||
'src/core/security/credentials_win32.c', |
||||
'src/core/security/google_default_credentials.c', |
||||
'src/core/security/handshake.c', |
||||
'src/core/security/json_token.c', |
||||
'src/core/security/jwt_verifier.c', |
||||
'src/core/security/secure_endpoint.c', |
||||
'src/core/security/security_connector.c', |
||||
'src/core/security/security_context.c', |
||||
'src/core/security/server_auth_filter.c', |
||||
'src/core/security/server_secure_chttp2.c', |
||||
'src/core/surface/init_secure.c', |
||||
'src/core/surface/secure_channel_create.c', |
||||
'src/core/tsi/fake_transport_security.c', |
||||
'src/core/tsi/ssl_transport_security.c', |
||||
'src/core/tsi/transport_security.c', |
||||
'src/core/census/grpc_context.c', |
||||
'src/core/census/grpc_filter.c', |
||||
'src/core/channel/channel_args.c', |
||||
'src/core/channel/channel_stack.c', |
||||
'src/core/channel/client_channel.c', |
||||
'src/core/channel/compress_filter.c', |
||||
'src/core/channel/connected_channel.c', |
||||
'src/core/channel/http_client_filter.c', |
||||
'src/core/channel/http_server_filter.c', |
||||
'src/core/channel/noop_filter.c', |
||||
'src/core/client_config/client_config.c', |
||||
'src/core/client_config/connector.c', |
||||
'src/core/client_config/lb_policies/pick_first.c', |
||||
'src/core/client_config/lb_policies/round_robin.c', |
||||
'src/core/client_config/lb_policy.c', |
||||
'src/core/client_config/lb_policy_factory.c', |
||||
'src/core/client_config/lb_policy_registry.c', |
||||
'src/core/client_config/resolver.c', |
||||
'src/core/client_config/resolver_factory.c', |
||||
'src/core/client_config/resolver_registry.c', |
||||
'src/core/client_config/resolvers/dns_resolver.c', |
||||
'src/core/client_config/resolvers/sockaddr_resolver.c', |
||||
'src/core/client_config/subchannel.c', |
||||
'src/core/client_config/subchannel_factory.c', |
||||
'src/core/client_config/subchannel_factory_decorators/add_channel_arg.c', |
||||
'src/core/client_config/subchannel_factory_decorators/merge_channel_args.c', |
||||
'src/core/client_config/uri_parser.c', |
||||
'src/core/compression/algorithm.c', |
||||
'src/core/compression/message_compress.c', |
||||
'src/core/debug/trace.c', |
||||
'src/core/httpcli/format_request.c', |
||||
'src/core/httpcli/httpcli.c', |
||||
'src/core/httpcli/parser.c', |
||||
'src/core/iomgr/alarm.c', |
||||
'src/core/iomgr/alarm_heap.c', |
||||
'src/core/iomgr/closure.c', |
||||
'src/core/iomgr/endpoint.c', |
||||
'src/core/iomgr/endpoint_pair_posix.c', |
||||
'src/core/iomgr/endpoint_pair_windows.c', |
||||
'src/core/iomgr/exec_ctx.c', |
||||
'src/core/iomgr/executor.c', |
||||
'src/core/iomgr/fd_posix.c', |
||||
'src/core/iomgr/iocp_windows.c', |
||||
'src/core/iomgr/iomgr.c', |
||||
'src/core/iomgr/iomgr_posix.c', |
||||
'src/core/iomgr/iomgr_windows.c', |
||||
'src/core/iomgr/pollset_multipoller_with_epoll.c', |
||||
'src/core/iomgr/pollset_multipoller_with_poll_posix.c', |
||||
'src/core/iomgr/pollset_posix.c', |
||||
'src/core/iomgr/pollset_set_posix.c', |
||||
'src/core/iomgr/pollset_set_windows.c', |
||||
'src/core/iomgr/pollset_windows.c', |
||||
'src/core/iomgr/resolve_address_posix.c', |
||||
'src/core/iomgr/resolve_address_windows.c', |
||||
'src/core/iomgr/sockaddr_utils.c', |
||||
'src/core/iomgr/socket_utils_common_posix.c', |
||||
'src/core/iomgr/socket_utils_linux.c', |
||||
'src/core/iomgr/socket_utils_posix.c', |
||||
'src/core/iomgr/socket_windows.c', |
||||
'src/core/iomgr/tcp_client_posix.c', |
||||
'src/core/iomgr/tcp_client_windows.c', |
||||
'src/core/iomgr/tcp_posix.c', |
||||
'src/core/iomgr/tcp_server_posix.c', |
||||
'src/core/iomgr/tcp_server_windows.c', |
||||
'src/core/iomgr/tcp_windows.c', |
||||
'src/core/iomgr/time_averaged_stats.c', |
||||
'src/core/iomgr/udp_server.c', |
||||
'src/core/iomgr/wakeup_fd_eventfd.c', |
||||
'src/core/iomgr/wakeup_fd_nospecial.c', |
||||
'src/core/iomgr/wakeup_fd_pipe.c', |
||||
'src/core/iomgr/wakeup_fd_posix.c', |
||||
'src/core/iomgr/workqueue_posix.c', |
||||
'src/core/iomgr/workqueue_windows.c', |
||||
'src/core/json/json.c', |
||||
'src/core/json/json_reader.c', |
||||
'src/core/json/json_string.c', |
||||
'src/core/json/json_writer.c', |
||||
'src/core/profiling/basic_timers.c', |
||||
'src/core/profiling/stap_timers.c', |
||||
'src/core/surface/api_trace.c', |
||||
'src/core/surface/byte_buffer.c', |
||||
'src/core/surface/byte_buffer_queue.c', |
||||
'src/core/surface/byte_buffer_reader.c', |
||||
'src/core/surface/call.c', |
||||
'src/core/surface/call_details.c', |
||||
'src/core/surface/call_log_batch.c', |
||||
'src/core/surface/channel.c', |
||||
'src/core/surface/channel_connectivity.c', |
||||
'src/core/surface/channel_create.c', |
||||
'src/core/surface/completion_queue.c', |
||||
'src/core/surface/event_string.c', |
||||
'src/core/surface/init.c', |
||||
'src/core/surface/lame_client.c', |
||||
'src/core/surface/metadata_array.c', |
||||
'src/core/surface/server.c', |
||||
'src/core/surface/server_chttp2.c', |
||||
'src/core/surface/server_create.c', |
||||
'src/core/surface/version.c', |
||||
'src/core/transport/chttp2/alpn.c', |
||||
'src/core/transport/chttp2/bin_encoder.c', |
||||
'src/core/transport/chttp2/frame_data.c', |
||||
'src/core/transport/chttp2/frame_goaway.c', |
||||
'src/core/transport/chttp2/frame_ping.c', |
||||
'src/core/transport/chttp2/frame_rst_stream.c', |
||||
'src/core/transport/chttp2/frame_settings.c', |
||||
'src/core/transport/chttp2/frame_window_update.c', |
||||
'src/core/transport/chttp2/hpack_parser.c', |
||||
'src/core/transport/chttp2/hpack_table.c', |
||||
'src/core/transport/chttp2/huffsyms.c', |
||||
'src/core/transport/chttp2/incoming_metadata.c', |
||||
'src/core/transport/chttp2/parsing.c', |
||||
'src/core/transport/chttp2/status_conversion.c', |
||||
'src/core/transport/chttp2/stream_encoder.c', |
||||
'src/core/transport/chttp2/stream_lists.c', |
||||
'src/core/transport/chttp2/stream_map.c', |
||||
'src/core/transport/chttp2/timeout_encoding.c', |
||||
'src/core/transport/chttp2/varint.c', |
||||
'src/core/transport/chttp2/writing.c', |
||||
'src/core/transport/chttp2_transport.c', |
||||
'src/core/transport/connectivity_state.c', |
||||
'src/core/transport/metadata.c', |
||||
'src/core/transport/stream_op.c', |
||||
'src/core/transport/transport.c', |
||||
'src/core/transport/transport_op_string.c', |
||||
'src/core/census/context.c', |
||||
'src/core/census/initialize.c', |
||||
'src/core/census/operation.c', |
||||
'src/core/census/tracing.c', |
||||
], |
||||
}, |
||||
] |
||||
} |
@ -0,0 +1,65 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#ifndef GRPC_INTERNAL_CORE_SURFACE_CALL_TEST_ONLY_H |
||||
#define GRPC_INTERNAL_CORE_SURFACE_CALL_TEST_ONLY_H |
||||
|
||||
#include <grpc/grpc.h> |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
/** Return the compression algorithm from \a call.
|
||||
* |
||||
* \warning This function should \b only be used in test code. */ |
||||
grpc_compression_algorithm grpc_call_test_only_get_compression_algorithm( |
||||
grpc_call *call); |
||||
|
||||
/** Return the message flags from \a call.
|
||||
* |
||||
* \warning This function should \b only be used in test code. */ |
||||
gpr_uint32 grpc_call_test_only_get_message_flags(grpc_call *call); |
||||
|
||||
/** Returns a bitset for the encodings (compression algorithms) supported by \a
|
||||
* call's peer. |
||||
* |
||||
* To be indexed by grpc_compression_algorithm enum values. */ |
||||
gpr_uint32 grpc_call_test_only_get_encodings_accepted_by_peer(grpc_call *call); |
||||
|
||||
|
||||
#ifdef __cplusplus |
||||
} |
||||
#endif |
||||
|
||||
#endif /* GRPC_INTERNAL_CORE_SURFACE_CALL_TEST_ONLY_H */ |
@ -0,0 +1,93 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2015, Google Inc. |
||||
// All rights reserved. |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
#endregion |
||||
|
||||
using System; |
||||
using System.Threading; |
||||
|
||||
using Google.Apis.Auth.OAuth2; |
||||
using Grpc.Core; |
||||
using Grpc.Core.Utils; |
||||
|
||||
namespace Grpc.Auth |
||||
{ |
||||
/// <summary> |
||||
/// Factory methods to create instances of <see cref="ChannelCredentials"/> and <see cref="CallCredentials"/> classes. |
||||
/// </summary> |
||||
public static class GrpcCredentials |
||||
{ |
||||
/// <summary> |
||||
/// Creates a <see cref="MetadataCredentials"/> instance that will obtain access tokens |
||||
/// from any credential that implements <c>ITokenAccess</c>. (e.g. <c>GoogleCredential</c>). |
||||
/// </summary> |
||||
/// <param name="credential">The credential to use to obtain access tokens.</param> |
||||
/// <returns>The <c>MetadataCredentials</c> instance.</returns> |
||||
public static MetadataCredentials Create(ITokenAccess credential) |
||||
{ |
||||
return new MetadataCredentials(AuthInterceptors.FromCredential(credential)); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Convenience method to create a <see cref="ChannelCredentials"/> instance from |
||||
/// <c>ITokenAccess</c> credential and <c>SslCredentials</c> instance. |
||||
/// </summary> |
||||
/// <param name="credential">The credential to use to obtain access tokens.</param> |
||||
/// <param name="sslCredentials">The <c>SslCredentials</c> instance.</param> |
||||
/// <returns>The channel credentials for access token based auth over a secure channel.</returns> |
||||
public static ChannelCredentials Create(ITokenAccess credential, SslCredentials sslCredentials) |
||||
{ |
||||
return ChannelCredentials.Create(sslCredentials, Create(credential)); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Creates an instance of <see cref="MetadataCredentials"/> that will use given access token to authenticate |
||||
/// with a gRPC service. |
||||
/// </summary> |
||||
/// <param name="accessToken">OAuth2 access token.</param> |
||||
/// /// <returns>The <c>MetadataCredentials</c> instance.</returns> |
||||
public static MetadataCredentials FromAccessToken(string accessToken) |
||||
{ |
||||
return new MetadataCredentials(AuthInterceptors.FromAccessToken(accessToken)); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Converts a <c>ITokenAccess</c> object into a <see cref="MetadataCredentials"/> object supported |
||||
/// by gRPC. |
||||
/// </summary> |
||||
/// <param name="credential"></param> |
||||
/// <returns></returns> |
||||
public static MetadataCredentials ToGrpcCredentials(this ITokenAccess credential) |
||||
{ |
||||
return GrpcCredentials.Create(credential); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,73 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2015, Google Inc. |
||||
// All rights reserved. |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
#endregion |
||||
|
||||
using System; |
||||
using System.Diagnostics; |
||||
using System.Runtime.InteropServices; |
||||
using System.Threading; |
||||
using System.Threading.Tasks; |
||||
using Grpc.Core; |
||||
using Grpc.Core.Internal; |
||||
using Grpc.Core.Utils; |
||||
using NUnit.Framework; |
||||
|
||||
namespace Grpc.Core.Tests |
||||
{ |
||||
public class ChannelCredentialsTest |
||||
{ |
||||
[Test] |
||||
public void InsecureCredentials_IsNonComposable() |
||||
{ |
||||
Assert.IsFalse(ChannelCredentials.Insecure.IsComposable); |
||||
} |
||||
|
||||
[Test] |
||||
public void ChannelCredentials_CreateComposite() |
||||
{ |
||||
var composite = ChannelCredentials.Create(new FakeChannelCredentials(true), new FakeCallCredentials()); |
||||
Assert.IsFalse(composite.IsComposable); |
||||
|
||||
Assert.Throws(typeof(ArgumentNullException), () => ChannelCredentials.Create(null, new FakeCallCredentials())); |
||||
Assert.Throws(typeof(ArgumentNullException), () => ChannelCredentials.Create(new FakeChannelCredentials(true), null)); |
||||
|
||||
// forbid composing non-composable |
||||
Assert.Throws(typeof(ArgumentException), () => ChannelCredentials.Create(new FakeChannelCredentials(false), new FakeCallCredentials())); |
||||
} |
||||
|
||||
[Test] |
||||
public void ChannelCredentials_CreateWrapped() |
||||
{ |
||||
ChannelCredentials.Create(new FakeCallCredentials()); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,142 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2015, Google Inc. |
||||
// All rights reserved. |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
#endregion |
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Threading.Tasks; |
||||
|
||||
using Grpc.Core.Internal; |
||||
using Grpc.Core.Utils; |
||||
|
||||
namespace Grpc.Core |
||||
{ |
||||
/// <summary> |
||||
/// Client-side call credentials. Provide authorization with per-call granularity. |
||||
/// </summary> |
||||
public abstract class CallCredentials |
||||
{ |
||||
/// <summary> |
||||
/// Composes multiple multiple <c>CallCredentials</c> objects into |
||||
/// a single <c>CallCredentials</c> object. |
||||
/// </summary> |
||||
/// <param name="credentials">credentials to compose</param> |
||||
/// <returns>The new <c>CompositeCallCredentials</c></returns> |
||||
public static CallCredentials Compose(params CallCredentials[] credentials) |
||||
{ |
||||
return new CompositeCallCredentials(credentials); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Creates native object for the credentials. |
||||
/// </summary> |
||||
/// <returns>The native credentials.</returns> |
||||
internal abstract CredentialsSafeHandle ToNativeCredentials(); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Asynchronous authentication interceptor for <see cref="MetadataCredentials"/>. |
||||
/// </summary> |
||||
/// <param name="authUri">URL of a service to which current remote call needs to authenticate</param> |
||||
/// <param name="metadata">Metadata to populate with entries that will be added to outgoing call's headers.</param> |
||||
/// <returns></returns> |
||||
public delegate Task AsyncAuthInterceptor(string authUri, Metadata metadata); |
||||
|
||||
/// <summary> |
||||
/// Client-side credentials that delegate metadata based auth to an interceptor. |
||||
/// The interceptor is automatically invoked for each remote call that uses <c>MetadataCredentials.</c> |
||||
/// </summary> |
||||
public class MetadataCredentials : CallCredentials |
||||
{ |
||||
readonly AsyncAuthInterceptor interceptor; |
||||
|
||||
/// <summary> |
||||
/// Initializes a new instance of <c>MetadataCredentials</c> class. |
||||
/// </summary> |
||||
/// <param name="interceptor">authentication interceptor</param> |
||||
public MetadataCredentials(AsyncAuthInterceptor interceptor) |
||||
{ |
||||
this.interceptor = interceptor; |
||||
} |
||||
|
||||
internal override CredentialsSafeHandle ToNativeCredentials() |
||||
{ |
||||
NativeMetadataCredentialsPlugin plugin = new NativeMetadataCredentialsPlugin(interceptor); |
||||
return plugin.Credentials; |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Credentials that allow composing multiple credentials objects into one <see cref="CallCredentials"/> object. |
||||
/// </summary> |
||||
internal sealed class CompositeCallCredentials : CallCredentials |
||||
{ |
||||
readonly List<CallCredentials> credentials; |
||||
|
||||
/// <summary> |
||||
/// Initializes a new instance of <c>CompositeCallCredentials</c> class. |
||||
/// The resulting credentials object will be composite of all the credentials specified as parameters. |
||||
/// </summary> |
||||
/// <param name="credentials">credentials to compose</param> |
||||
public CompositeCallCredentials(params CallCredentials[] credentials) |
||||
{ |
||||
Preconditions.CheckArgument(credentials.Length >= 2, "Composite credentials object can only be created from 2 or more credentials."); |
||||
this.credentials = new List<CallCredentials>(credentials); |
||||
} |
||||
|
||||
internal override CredentialsSafeHandle ToNativeCredentials() |
||||
{ |
||||
return ToNativeRecursive(0); |
||||
} |
||||
|
||||
// Recursive descent makes managing lifetime of intermediate CredentialSafeHandle instances easier. |
||||
// In practice, we won't usually see composites from more than two credentials anyway. |
||||
private CredentialsSafeHandle ToNativeRecursive(int startIndex) |
||||
{ |
||||
if (startIndex == credentials.Count - 1) |
||||
{ |
||||
return credentials[startIndex].ToNativeCredentials(); |
||||
} |
||||
|
||||
using (var cred1 = credentials[startIndex].ToNativeCredentials()) |
||||
using (var cred2 = ToNativeRecursive(startIndex + 1)) |
||||
{ |
||||
var nativeComposite = CredentialsSafeHandle.CreateComposite(cred1, cred2); |
||||
if (nativeComposite.IsInvalid) |
||||
{ |
||||
throw new ArgumentException("Error creating native composite credentials. Likely, this is because you are trying to compose incompatible credentials."); |
||||
} |
||||
return nativeComposite; |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,238 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2015, Google Inc. |
||||
// All rights reserved. |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
#endregion |
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Threading.Tasks; |
||||
|
||||
using Grpc.Core.Internal; |
||||
using Grpc.Core.Utils; |
||||
|
||||
namespace Grpc.Core |
||||
{ |
||||
/// <summary> |
||||
/// Client-side channel credentials. Used for creation of a secure channel. |
||||
/// </summary> |
||||
public abstract class ChannelCredentials |
||||
{ |
||||
static readonly ChannelCredentials InsecureInstance = new InsecureCredentialsImpl(); |
||||
|
||||
/// <summary> |
||||
/// Returns instance of credentials that provides no security and |
||||
/// will result in creating an unsecure channel with no encryption whatsoever. |
||||
/// </summary> |
||||
public static ChannelCredentials Insecure |
||||
{ |
||||
get |
||||
{ |
||||
return InsecureInstance; |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Creates a new instance of <c>ChannelCredentials</c> class by composing |
||||
/// given channel credentials with call credentials. |
||||
/// </summary> |
||||
/// <param name="channelCredentials">Channel credentials.</param> |
||||
/// <param name="callCredentials">Call credentials.</param> |
||||
/// <returns>The new composite <c>ChannelCredentials</c></returns> |
||||
public static ChannelCredentials Create(ChannelCredentials channelCredentials, CallCredentials callCredentials) |
||||
{ |
||||
return new CompositeChannelCredentials(channelCredentials, callCredentials); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Creates a new instance of <c>ChannelCredentials</c> by wrapping |
||||
/// an instance of <c>CallCredentials</c>. |
||||
/// </summary> |
||||
/// <param name="callCredentials">Call credentials.</param> |
||||
/// <returns>The <c>ChannelCredentials</c> wrapping given call credentials.</returns> |
||||
public static ChannelCredentials Create(CallCredentials callCredentials) |
||||
{ |
||||
return new WrappedCallCredentials(callCredentials); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Creates native object for the credentials. May return null if insecure channel |
||||
/// should be created. |
||||
/// </summary> |
||||
/// <returns>The native credentials.</returns> |
||||
internal abstract CredentialsSafeHandle ToNativeCredentials(); |
||||
|
||||
/// <summary> |
||||
/// Returns <c>true</c> if this credential type allows being composed by <c>CompositeCredentials</c>. |
||||
/// </summary> |
||||
internal virtual bool IsComposable |
||||
{ |
||||
get { return false; } |
||||
} |
||||
|
||||
private sealed class InsecureCredentialsImpl : ChannelCredentials |
||||
{ |
||||
internal override CredentialsSafeHandle ToNativeCredentials() |
||||
{ |
||||
return null; |
||||
} |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Client-side SSL credentials. |
||||
/// </summary> |
||||
public sealed class SslCredentials : ChannelCredentials |
||||
{ |
||||
readonly string rootCertificates; |
||||
readonly KeyCertificatePair keyCertificatePair; |
||||
|
||||
/// <summary> |
||||
/// Creates client-side SSL credentials loaded from |
||||
/// disk file pointed to by the GRPC_DEFAULT_SSL_ROOTS_FILE_PATH environment variable. |
||||
/// If that fails, gets the roots certificates from a well known place on disk. |
||||
/// </summary> |
||||
public SslCredentials() : this(null, null) |
||||
{ |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Creates client-side SSL credentials from |
||||
/// a string containing PEM encoded root certificates. |
||||
/// </summary> |
||||
public SslCredentials(string rootCertificates) : this(rootCertificates, null) |
||||
{ |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Creates client-side SSL credentials. |
||||
/// </summary> |
||||
/// <param name="rootCertificates">string containing PEM encoded server root certificates.</param> |
||||
/// <param name="keyCertificatePair">a key certificate pair.</param> |
||||
public SslCredentials(string rootCertificates, KeyCertificatePair keyCertificatePair) |
||||
{ |
||||
this.rootCertificates = rootCertificates; |
||||
this.keyCertificatePair = keyCertificatePair; |
||||
} |
||||
|
||||
/// <summary> |
||||
/// PEM encoding of the server root certificates. |
||||
/// </summary> |
||||
public string RootCertificates |
||||
{ |
||||
get |
||||
{ |
||||
return this.rootCertificates; |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Client side key and certificate pair. |
||||
/// If null, client will not use key and certificate pair. |
||||
/// </summary> |
||||
public KeyCertificatePair KeyCertificatePair |
||||
{ |
||||
get |
||||
{ |
||||
return this.keyCertificatePair; |
||||
} |
||||
} |
||||
|
||||
// Composing composite makes no sense. |
||||
internal override bool IsComposable |
||||
{ |
||||
get { return true; } |
||||
} |
||||
|
||||
internal override CredentialsSafeHandle ToNativeCredentials() |
||||
{ |
||||
return CredentialsSafeHandle.CreateSslCredentials(rootCertificates, keyCertificatePair); |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Credentials that allow composing one <see cref="ChannelCredentials"/> object and |
||||
/// one or more <see cref="CallCredentials"/> objects into a single <see cref="ChannelCredentials"/>. |
||||
/// </summary> |
||||
internal sealed class CompositeChannelCredentials : ChannelCredentials |
||||
{ |
||||
readonly ChannelCredentials channelCredentials; |
||||
readonly CallCredentials callCredentials; |
||||
|
||||
/// <summary> |
||||
/// Initializes a new instance of <c>CompositeChannelCredentials</c> class. |
||||
/// The resulting credentials object will be composite of all the credentials specified as parameters. |
||||
/// </summary> |
||||
/// <param name="channelCredentials">channelCredentials to compose</param> |
||||
/// <param name="callCredentials">channelCredentials to compose</param> |
||||
public CompositeChannelCredentials(ChannelCredentials channelCredentials, CallCredentials callCredentials) |
||||
{ |
||||
this.channelCredentials = Preconditions.CheckNotNull(channelCredentials); |
||||
this.callCredentials = Preconditions.CheckNotNull(callCredentials); |
||||
Preconditions.CheckArgument(channelCredentials.IsComposable, "Supplied channel credentials do not allow composition."); |
||||
} |
||||
|
||||
internal override CredentialsSafeHandle ToNativeCredentials() |
||||
{ |
||||
using (var cred1 = channelCredentials.ToNativeCredentials()) |
||||
using (var cred2 = callCredentials.ToNativeCredentials()) |
||||
{ |
||||
var nativeComposite = CredentialsSafeHandle.CreateComposite(cred1, cred2); |
||||
if (nativeComposite.IsInvalid) |
||||
{ |
||||
throw new ArgumentException("Error creating native composite credentials. Likely, this is because you are trying to compose incompatible credentials."); |
||||
} |
||||
return nativeComposite; |
||||
} |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Credentials wrapping <see cref="CallCredentials"/> as <see cref="ChannelCredentials"/>. |
||||
/// </summary> |
||||
internal sealed class WrappedCallCredentials : ChannelCredentials |
||||
{ |
||||
readonly CallCredentials callCredentials; |
||||
|
||||
/// <summary> |
||||
/// Wraps instance of <c>CallCredentials</c> as <c>ChannelCredentials</c>. |
||||
/// </summary> |
||||
/// <param name="callCredentials">credentials to wrap</param> |
||||
public WrappedCallCredentials(CallCredentials callCredentials) |
||||
{ |
||||
this.callCredentials = Preconditions.CheckNotNull(callCredentials); |
||||
} |
||||
|
||||
internal override CredentialsSafeHandle ToNativeCredentials() |
||||
{ |
||||
return callCredentials.ToNativeCredentials(); |
||||
} |
||||
} |
||||
} |
@ -1,138 +0,0 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2015, Google Inc. |
||||
// All rights reserved. |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
#endregion |
||||
|
||||
using System; |
||||
using Grpc.Core.Internal; |
||||
|
||||
namespace Grpc.Core |
||||
{ |
||||
/// <summary> |
||||
/// Client-side credentials. Used for creation of a secure channel. |
||||
/// </summary> |
||||
public abstract class Credentials |
||||
{ |
||||
static readonly Credentials InsecureInstance = new InsecureCredentialsImpl(); |
||||
|
||||
/// <summary> |
||||
/// Returns instance of credential that provides no security and |
||||
/// will result in creating an unsecure channel with no encryption whatsoever. |
||||
/// </summary> |
||||
public static Credentials Insecure |
||||
{ |
||||
get |
||||
{ |
||||
return InsecureInstance; |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Creates native object for the credentials. May return null if insecure channel |
||||
/// should be created. |
||||
/// </summary> |
||||
/// <returns>The native credentials.</returns> |
||||
internal abstract CredentialsSafeHandle ToNativeCredentials(); |
||||
|
||||
private sealed class InsecureCredentialsImpl : Credentials |
||||
{ |
||||
internal override CredentialsSafeHandle ToNativeCredentials() |
||||
{ |
||||
return null; |
||||
} |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Client-side SSL credentials. |
||||
/// </summary> |
||||
public sealed class SslCredentials : Credentials |
||||
{ |
||||
readonly string rootCertificates; |
||||
readonly KeyCertificatePair keyCertificatePair; |
||||
|
||||
/// <summary> |
||||
/// Creates client-side SSL credentials loaded from |
||||
/// disk file pointed to by the GRPC_DEFAULT_SSL_ROOTS_FILE_PATH environment variable. |
||||
/// If that fails, gets the roots certificates from a well known place on disk. |
||||
/// </summary> |
||||
public SslCredentials() : this(null, null) |
||||
{ |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Creates client-side SSL credentials from |
||||
/// a string containing PEM encoded root certificates. |
||||
/// </summary> |
||||
public SslCredentials(string rootCertificates) : this(rootCertificates, null) |
||||
{ |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Creates client-side SSL credentials. |
||||
/// </summary> |
||||
/// <param name="rootCertificates">string containing PEM encoded server root certificates.</param> |
||||
/// <param name="keyCertificatePair">a key certificate pair.</param> |
||||
public SslCredentials(string rootCertificates, KeyCertificatePair keyCertificatePair) |
||||
{ |
||||
this.rootCertificates = rootCertificates; |
||||
this.keyCertificatePair = keyCertificatePair; |
||||
} |
||||
|
||||
/// <summary> |
||||
/// PEM encoding of the server root certificates. |
||||
/// </summary> |
||||
public string RootCertificates |
||||
{ |
||||
get |
||||
{ |
||||
return this.rootCertificates; |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Client side key and certificate pair. |
||||
/// If null, client will not use key and certificate pair. |
||||
/// </summary> |
||||
public KeyCertificatePair KeyCertificatePair |
||||
{ |
||||
get |
||||
{ |
||||
return this.keyCertificatePair; |
||||
} |
||||
} |
||||
|
||||
internal override CredentialsSafeHandle ToNativeCredentials() |
||||
{ |
||||
return CredentialsSafeHandle.CreateSslCredentials(rootCertificates, keyCertificatePair); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,113 @@ |
||||
#region Copyright notice and license |
||||
// Copyright 2015, Google Inc. |
||||
// All rights reserved. |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
#endregion |
||||
using System; |
||||
using System.Runtime.InteropServices; |
||||
using System.Threading; |
||||
using System.Threading.Tasks; |
||||
|
||||
using Grpc.Core.Logging; |
||||
using Grpc.Core.Utils; |
||||
|
||||
namespace Grpc.Core.Internal |
||||
{ |
||||
internal delegate void NativeMetadataInterceptor(IntPtr statePtr, IntPtr serviceUrlPtr, IntPtr callbackPtr, IntPtr userDataPtr, bool isDestroy); |
||||
|
||||
internal class NativeMetadataCredentialsPlugin |
||||
{ |
||||
const string GetMetadataExceptionMsg = "Exception occured in metadata credentials plugin."; |
||||
static readonly ILogger Logger = GrpcEnvironment.Logger.ForType<NativeMetadataCredentialsPlugin>(); |
||||
|
||||
[DllImport("grpc_csharp_ext.dll")] |
||||
static extern CredentialsSafeHandle grpcsharp_metadata_credentials_create_from_plugin(NativeMetadataInterceptor interceptor); |
||||
|
||||
[DllImport("grpc_csharp_ext.dll", CharSet = CharSet.Ansi)] |
||||
static extern void grpcsharp_metadata_credentials_notify_from_plugin(IntPtr callbackPtr, IntPtr userData, MetadataArraySafeHandle metadataArray, StatusCode statusCode, string errorDetails); |
||||
|
||||
AsyncAuthInterceptor interceptor; |
||||
GCHandle gcHandle; |
||||
NativeMetadataInterceptor nativeInterceptor; |
||||
CredentialsSafeHandle credentials; |
||||
|
||||
public NativeMetadataCredentialsPlugin(AsyncAuthInterceptor interceptor) |
||||
{ |
||||
this.interceptor = Preconditions.CheckNotNull(interceptor, "interceptor"); |
||||
this.nativeInterceptor = NativeMetadataInterceptorHandler; |
||||
|
||||
// Make sure the callback doesn't get garbage collected until it is destroyed. |
||||
this.gcHandle = GCHandle.Alloc(this.nativeInterceptor, GCHandleType.Normal); |
||||
this.credentials = grpcsharp_metadata_credentials_create_from_plugin(nativeInterceptor); |
||||
} |
||||
|
||||
public CredentialsSafeHandle Credentials |
||||
{ |
||||
get { return credentials; } |
||||
} |
||||
|
||||
private void NativeMetadataInterceptorHandler(IntPtr statePtr, IntPtr serviceUrlPtr, IntPtr callbackPtr, IntPtr userDataPtr, bool isDestroy) |
||||
{ |
||||
if (isDestroy) |
||||
{ |
||||
gcHandle.Free(); |
||||
return; |
||||
} |
||||
|
||||
try |
||||
{ |
||||
string serviceUrl = Marshal.PtrToStringAnsi(serviceUrlPtr); |
||||
StartGetMetadata(serviceUrl, callbackPtr, userDataPtr); |
||||
} |
||||
catch (Exception e) |
||||
{ |
||||
grpcsharp_metadata_credentials_notify_from_plugin(callbackPtr, userDataPtr, MetadataArraySafeHandle.Create(Metadata.Empty), StatusCode.Unknown, GetMetadataExceptionMsg); |
||||
Logger.Error(e, GetMetadataExceptionMsg); |
||||
} |
||||
} |
||||
|
||||
private async void StartGetMetadata(string serviceUrl, IntPtr callbackPtr, IntPtr userDataPtr) |
||||
{ |
||||
try |
||||
{ |
||||
var metadata = new Metadata(); |
||||
await interceptor(serviceUrl, metadata); |
||||
|
||||
using (var metadataArray = MetadataArraySafeHandle.Create(metadata)) |
||||
{ |
||||
grpcsharp_metadata_credentials_notify_from_plugin(callbackPtr, userDataPtr, metadataArray, StatusCode.OK, null); |
||||
} |
||||
} |
||||
catch (Exception e) |
||||
{ |
||||
grpcsharp_metadata_credentials_notify_from_plugin(callbackPtr, userDataPtr, MetadataArraySafeHandle.Create(Metadata.Empty), StatusCode.Unknown, GetMetadataExceptionMsg); |
||||
Logger.Error(e, GetMetadataExceptionMsg); |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,97 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2015, Google Inc. |
||||
// All rights reserved. |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
#endregion |
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.IO; |
||||
using System.Linq; |
||||
using System.Threading; |
||||
using System.Threading.Tasks; |
||||
using Grpc.Core; |
||||
using Grpc.Core.Utils; |
||||
using Grpc.Testing; |
||||
using NUnit.Framework; |
||||
|
||||
namespace Grpc.IntegrationTesting |
||||
{ |
||||
public class MetadataCredentialsTest |
||||
{ |
||||
const string Host = "localhost"; |
||||
Server server; |
||||
Channel channel; |
||||
TestService.ITestServiceClient client; |
||||
|
||||
[TestFixtureSetUp] |
||||
public void Init() |
||||
{ |
||||
var serverCredentials = new SslServerCredentials(new[] { new KeyCertificatePair(File.ReadAllText(TestCredentials.ServerCertChainPath), File.ReadAllText(TestCredentials.ServerPrivateKeyPath)) }); |
||||
server = new Server |
||||
{ |
||||
Services = { TestService.BindService(new TestServiceImpl()) }, |
||||
Ports = { { Host, ServerPort.PickUnused, serverCredentials } } |
||||
}; |
||||
server.Start(); |
||||
|
||||
var options = new List<ChannelOption> |
||||
{ |
||||
new ChannelOption(ChannelOptions.SslTargetNameOverride, TestCredentials.DefaultHostOverride) |
||||
}; |
||||
|
||||
var asyncAuthInterceptor = new AsyncAuthInterceptor(async (authUri, metadata) => |
||||
{ |
||||
await Task.Delay(100); // make sure the operation is asynchronous. |
||||
metadata.Add("authorization", "SECRET_TOKEN"); |
||||
}); |
||||
|
||||
var clientCredentials = ChannelCredentials.Create( |
||||
new SslCredentials(File.ReadAllText(TestCredentials.ClientCertAuthorityPath)), |
||||
new MetadataCredentials(asyncAuthInterceptor)); |
||||
channel = new Channel(Host, server.Ports.Single().BoundPort, clientCredentials, options); |
||||
client = TestService.NewClient(channel); |
||||
} |
||||
|
||||
[TestFixtureTearDown] |
||||
public void Cleanup() |
||||
{ |
||||
channel.ShutdownAsync().Wait(); |
||||
server.ShutdownAsync().Wait(); |
||||
} |
||||
|
||||
[Test] |
||||
public void MetadataCredentials() |
||||
{ |
||||
var response = client.UnaryCall(new SimpleRequest { ResponseSize = 10 }); |
||||
Assert.AreEqual(10, response.Payload.Body.Length); |
||||
} |
||||
} |
||||
} |
@ -1,132 +0,0 @@ |
||||
|
||||
// Copyright 2015, Google Inc. |
||||
// All rights reserved. |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
// Message definitions to be used by integration test service definitions. |
||||
|
||||
syntax = "proto3"; |
||||
|
||||
package grpc.testing; |
||||
|
||||
// The type of payload that should be returned. |
||||
enum PayloadType { |
||||
// Compressable text format. |
||||
COMPRESSABLE = 0; |
||||
|
||||
// Uncompressable binary format. |
||||
UNCOMPRESSABLE = 1; |
||||
|
||||
// Randomly chosen from all other formats defined in this enum. |
||||
RANDOM = 2; |
||||
} |
||||
|
||||
// A block of data, to simply increase gRPC message size. |
||||
message Payload { |
||||
// The type of data in body. |
||||
PayloadType type = 1; |
||||
// Primary contents of payload. |
||||
bytes body = 2; |
||||
} |
||||
|
||||
// Unary request. |
||||
message SimpleRequest { |
||||
// Desired payload type in the response from the server. |
||||
// If response_type is RANDOM, server randomly chooses one from other formats. |
||||
PayloadType response_type = 1; |
||||
|
||||
// Desired payload size in the response from the server. |
||||
// If response_type is COMPRESSABLE, this denotes the size before compression. |
||||
int32 response_size = 2; |
||||
|
||||
// Optional input payload sent along with the request. |
||||
Payload payload = 3; |
||||
|
||||
// Whether SimpleResponse should include username. |
||||
bool fill_username = 4; |
||||
|
||||
// Whether SimpleResponse should include OAuth scope. |
||||
bool fill_oauth_scope = 5; |
||||
} |
||||
|
||||
// Unary response, as configured by the request. |
||||
message SimpleResponse { |
||||
// Payload to increase message size. |
||||
Payload payload = 1; |
||||
// The user the request came from, for verifying authentication was |
||||
// successful when the client expected it. |
||||
string username = 2; |
||||
// OAuth scope. |
||||
string oauth_scope = 3; |
||||
} |
||||
|
||||
// Client-streaming request. |
||||
message StreamingInputCallRequest { |
||||
// Optional input payload sent along with the request. |
||||
Payload payload = 1; |
||||
|
||||
// Not expecting any payload from the response. |
||||
} |
||||
|
||||
// Client-streaming response. |
||||
message StreamingInputCallResponse { |
||||
// Aggregated size of payloads received from the client. |
||||
int32 aggregated_payload_size = 1; |
||||
} |
||||
|
||||
// Configuration for a particular response. |
||||
message ResponseParameters { |
||||
// Desired payload sizes in responses from the server. |
||||
// If response_type is COMPRESSABLE, this denotes the size before compression. |
||||
int32 size = 1; |
||||
|
||||
// Desired interval between consecutive responses in the response stream in |
||||
// microseconds. |
||||
int32 interval_us = 2; |
||||
} |
||||
|
||||
// Server-streaming request. |
||||
message StreamingOutputCallRequest { |
||||
// Desired payload type in the response from the server. |
||||
// If response_type is RANDOM, the payload from each response in the stream |
||||
// might be of different types. This is to simulate a mixed type of payload |
||||
// stream. |
||||
PayloadType response_type = 1; |
||||
|
||||
// Configuration for each expected response message. |
||||
repeated ResponseParameters response_parameters = 2; |
||||
|
||||
// Optional input payload sent along with the request. |
||||
Payload payload = 3; |
||||
} |
||||
|
||||
// Server-streaming response, as configured by the request and parameters. |
||||
message StreamingOutputCallResponse { |
||||
// Payload to increase response size. |
||||
Payload payload = 1; |
||||
} |
@ -1,71 +0,0 @@ |
||||
|
||||
// Copyright 2015, Google Inc. |
||||
// All rights reserved. |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
// An integration test service that covers all the method signature permutations |
||||
// of unary/streaming requests/responses. |
||||
syntax = "proto3"; |
||||
|
||||
import "empty.proto"; |
||||
import "messages.proto"; |
||||
|
||||
package grpc.testing; |
||||
|
||||
// A simple service to test the various types of RPCs and experiment with |
||||
// performance with various types of payload. |
||||
service TestService { |
||||
// One empty request followed by one empty response. |
||||
rpc EmptyCall(grpc.testing.Empty) returns (grpc.testing.Empty); |
||||
|
||||
// One request followed by one response. |
||||
rpc UnaryCall(SimpleRequest) returns (SimpleResponse); |
||||
|
||||
// One request followed by a sequence of responses (streamed download). |
||||
// The server returns the payload with client desired type and sizes. |
||||
rpc StreamingOutputCall(StreamingOutputCallRequest) |
||||
returns (stream StreamingOutputCallResponse); |
||||
|
||||
// A sequence of requests followed by one response (streamed upload). |
||||
// The server returns the aggregated size of client payload as the result. |
||||
rpc StreamingInputCall(stream StreamingInputCallRequest) |
||||
returns (StreamingInputCallResponse); |
||||
|
||||
// A sequence of requests with each request served by the server immediately. |
||||
// As one request could lead to multiple responses, this interface |
||||
// demonstrates the idea of full duplexing. |
||||
rpc FullDuplexCall(stream StreamingOutputCallRequest) |
||||
returns (stream StreamingOutputCallResponse); |
||||
|
||||
// A sequence of requests followed by a sequence of responses. |
||||
// The server buffers all the client requests and then serves them in order. A |
||||
// stream of responses are returned to the client when the server starts with |
||||
// first request. |
||||
rpc HalfDuplexCall(stream StreamingOutputCallRequest) |
||||
returns (stream StreamingOutputCallResponse); |
||||
} |
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue