Merge pull request #18652 from jtattermusch/client_api_prepare

Make CallCredentials implementation agnostic (prepare for move to Grpc.Core.Api)
pull/18707/head
Jan Tattermusch 6 years ago committed by GitHub
commit 4bce14921d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      src/csharp/Grpc.Core.Tests/FakeCredentials.cs
  2. 83
      src/csharp/Grpc.Core/CallCredentials.cs
  3. 38
      src/csharp/Grpc.Core/CallCredentialsConfiguratorBase.cs
  4. 85
      src/csharp/Grpc.Core/Internal/DefaultCallCredentialsConfigurator.cs

@ -42,9 +42,9 @@ namespace Grpc.Core.Tests
internal class FakeCallCredentials : CallCredentials
{
internal override CallCredentialsSafeHandle ToNativeCredentials()
public override void InternalPopulateConfiguration(CallCredentialsConfiguratorBase configurator, object state)
{
return null;
// not invoking the configurator on purpose
}
}
}

@ -16,9 +16,8 @@
#endregion
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using Grpc.Core.Internal;
using Grpc.Core.Utils;
@ -48,81 +47,43 @@ namespace Grpc.Core
/// <param name="interceptor">authentication interceptor</param>
public static CallCredentials FromInterceptor(AsyncAuthInterceptor interceptor)
{
return new MetadataCredentials(interceptor);
return new AsyncAuthInterceptorCredentials(interceptor);
}
/// <summary>
/// Creates native object for the credentials.
/// Populates this call credential instances.
/// You never need to invoke this, part of internal implementation.
/// </summary>
/// <returns>The native credentials.</returns>
internal abstract CallCredentialsSafeHandle ToNativeCredentials();
}
public abstract void InternalPopulateConfiguration(CallCredentialsConfiguratorBase configurator, object state);
/// <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>
internal sealed 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 = GrpcPreconditions.CheckNotNull(interceptor);
}
internal override CallCredentialsSafeHandle ToNativeCredentials()
private class CompositeCallCredentials : CallCredentials
{
NativeMetadataCredentialsPlugin plugin = new NativeMetadataCredentialsPlugin(interceptor);
return plugin.Credentials;
}
}
readonly IReadOnlyList<CallCredentials> 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;
public CompositeCallCredentials(CallCredentials[] credentials)
{
GrpcPreconditions.CheckArgument(credentials.Length >= 2, "Composite credentials object can only be created from 2 or more credentials.");
this.credentials = new List<CallCredentials>(credentials).AsReadOnly();
}
/// <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)
{
GrpcPreconditions.CheckArgument(credentials.Length >= 2, "Composite credentials object can only be created from 2 or more credentials.");
this.credentials = new List<CallCredentials>(credentials);
public override void InternalPopulateConfiguration(CallCredentialsConfiguratorBase configurator, object state)
{
configurator.SetCompositeCredentials(state, credentials);
}
}
internal override CallCredentialsSafeHandle ToNativeCredentials()
private class AsyncAuthInterceptorCredentials : CallCredentials
{
return ToNativeRecursive(0);
}
readonly AsyncAuthInterceptor interceptor;
// 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 CallCredentialsSafeHandle ToNativeRecursive(int startIndex)
{
if (startIndex == credentials.Count - 1)
public AsyncAuthInterceptorCredentials(AsyncAuthInterceptor interceptor)
{
return credentials[startIndex].ToNativeCredentials();
this.interceptor = GrpcPreconditions.CheckNotNull(interceptor);
}
using (var cred1 = credentials[startIndex].ToNativeCredentials())
using (var cred2 = ToNativeRecursive(startIndex + 1))
public override void InternalPopulateConfiguration(CallCredentialsConfiguratorBase configurator, object state)
{
var nativeComposite = CallCredentialsSafeHandle.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;
configurator.SetAsyncAuthInterceptorCredentials(state, interceptor);
}
}
}

@ -0,0 +1,38 @@
#region Copyright notice and license
// Copyright 2019 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.Collections.Generic;
namespace Grpc.Core
{
/// <summary>
/// Base class for objects that can consume configuration from <c>CallCredentials</c> objects.
/// </summary>
public abstract class CallCredentialsConfiguratorBase
{
/// <summary>
/// Consumes configuration for composite call credentials.
/// </summary>
public abstract void SetCompositeCredentials(object state, IReadOnlyList<CallCredentials> credentials);
/// <summary>
/// Consumes configuration for call credentials created from <c>AsyncAuthInterceptor</c>
/// </summary>
public abstract void SetAsyncAuthInterceptorCredentials(object state, AsyncAuthInterceptor interceptor);
}
}

@ -0,0 +1,85 @@
#region Copyright notice and license
// Copyright 2019 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.Collections.Generic;
using Grpc.Core.Utils;
namespace Grpc.Core.Internal
{
/// <summary>
/// Creates native call credential objects from instances of <c>CallCredentials</c>.
/// </summary>
internal class DefaultCallCredentialsConfigurator : CallCredentialsConfiguratorBase
{
CallCredentialsSafeHandle nativeCredentials;
public CallCredentialsSafeHandle NativeCredentials => nativeCredentials;
public override void SetAsyncAuthInterceptorCredentials(object state, AsyncAuthInterceptor interceptor)
{
GrpcPreconditions.CheckState(nativeCredentials == null);
var plugin = new NativeMetadataCredentialsPlugin(interceptor);
nativeCredentials = plugin.Credentials;
}
public override void SetCompositeCredentials(object state, IReadOnlyList<CallCredentials> credentials)
{
GrpcPreconditions.CheckState(nativeCredentials == null);
GrpcPreconditions.CheckArgument(credentials.Count >= 2);
nativeCredentials = CompositeToNativeRecursive(credentials, 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 CallCredentialsSafeHandle CompositeToNativeRecursive(IReadOnlyList<CallCredentials> credentials, int startIndex)
{
if (startIndex == credentials.Count - 1)
{
return credentials[startIndex].ToNativeCredentials();
}
using (var cred1 = credentials[startIndex].ToNativeCredentials())
using (var cred2 = CompositeToNativeRecursive(credentials, startIndex + 1))
{
var nativeComposite = CallCredentialsSafeHandle.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;
}
}
}
internal static class CallCredentialsExtensions
{
/// <summary>
/// Creates native object for the credentials.
/// </summary>
/// <returns>The native credentials.</returns>
public static CallCredentialsSafeHandle ToNativeCredentials(this CallCredentials credentials)
{
var configurator = new DefaultCallCredentialsConfigurator();
credentials.InternalPopulateConfiguration(configurator, credentials);
return configurator.NativeCredentials;
}
}
}
Loading…
Cancel
Save