Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add interceptors grpc #690

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Client.UnitTests/ZeebeAuthTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using System.Threading.Tasks;
using GatewayProtocol;
using Grpc.Core;
using Grpc.Core.Interceptors;
using Grpc.Core.Logging;
using NUnit.Framework;
using Zeebe.Client.Api.Builder;
Expand Down Expand Up @@ -103,6 +104,30 @@ public async Task ShouldUseTransportEncryptionWithServerCert()
Assert.NotNull(publishMessageResponse);
}

class MyInterceptor : Interceptor { }

[Test]
public async Task ShouldUseTransportEncryptionWithServerCertAndNewInterceptor()
{
// given
var zeebeClient = ZeebeClient.Builder()
.UseGatewayAddress("localhost:26505")
.UseTransportEncryption(ServerCertPath)
.AllowUntrustedCertificates()
.UseInterceptors(new MyInterceptor())
.Build();

// when
var publishMessageResponse = await zeebeClient
.NewPublishMessageCommand()
.MessageName("messageName")
.CorrelationKey("p-1")
.Send();

// then
Assert.NotNull(publishMessageResponse);
}

[Test]
public async Task ShouldFailOnWrongCert()
{
Expand Down
4 changes: 4 additions & 0 deletions Client/Api/Builder/IZeebeClientBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Runtime.CompilerServices;
using Grpc.Core.Interceptors;
using Microsoft.Extensions.Logging;

namespace Zeebe.Client.Api.Builder
Expand Down Expand Up @@ -50,6 +52,8 @@ public interface IZeebeClientTransportBuilder

public interface IZeebeSecureClientBuilder : IZeebeClientFinalBuildStep
{
IZeebeSecureClientBuilder UseInterceptors(params Interceptor[] interceptors);

/// <summary>
/// DANGER: This allows untrusted certificates for the gRPC connection with Zeebe.
///
Expand Down
7 changes: 7 additions & 0 deletions Client/IZeebeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// 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.
using Grpc.Core.Interceptors;
using System;
using Zeebe.Client.Api.Commands;
using Zeebe.Client.Api.Worker;
Expand Down Expand Up @@ -306,5 +307,11 @@ public interface IZeebeClient : IJobClient, IDisposable
/// the request where you must call <see cref="IFinalCommandStep{T}.Send"/>
/// </returns>
ITopologyRequestStep1 TopologyRequest();

/// <summary>
/// Add user intereceptors to channel grpc
/// </summary>
/// <param name="interceptors">Interceptors</param>
void AddInterceptor(params Interceptor[] interceptors);
}
}
15 changes: 14 additions & 1 deletion Client/Impl/Builder/ZeebeClientBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using Grpc.Auth;
using Grpc.Core;
using Grpc.Core.Interceptors;
using Microsoft.Extensions.Logging;
using Zeebe.Client.Api.Builder;

Expand Down Expand Up @@ -87,6 +89,7 @@ internal class ZeebeSecureClientBuilder : IZeebeSecureClientBuilder
private Func<int, TimeSpan> sleepDurationProvider;
private X509Certificate2 certificate;
private bool allowUntrusted = false;
private Interceptor[] interceptors;

private string Address { get; }

Expand Down Expand Up @@ -125,6 +128,13 @@ public IZeebeSecureClientBuilder AllowUntrustedCertificates()
return this;
}

public IZeebeSecureClientBuilder UseInterceptors(params Interceptor[] interceptors)
{
this.interceptors = interceptors;
return this;
}


public IZeebeClientFinalBuildStep UseAccessToken(string accessToken)
{
Credentials = ChannelCredentials.Create(Credentials, GoogleGrpcCredentials.FromAccessToken(accessToken));
Expand All @@ -151,7 +161,10 @@ public IZeebeClientFinalBuildStep UseRetrySleepDurationProvider(Func<int, TimeSp

public IZeebeClient Build()
{
return new ZeebeClient(Address, Credentials, keepAlive, sleepDurationProvider, loggerFactory, certificate, allowUntrusted);
var client = new ZeebeClient(Address, Credentials, keepAlive, sleepDurationProvider, loggerFactory, certificate, allowUntrusted);
if (interceptors != null && interceptors.Any())
client.AddInterceptor(this.interceptors);
return client;
}
}
}
14 changes: 14 additions & 0 deletions Client/ZeebeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,20 @@ internal ZeebeClient(string address,
DefaultWaitTimeProvider);
}

public void AddInterceptor(params Interceptor[] interceptors)
{
if (interceptors == null || !interceptors.Any()) return;

List<Interceptor> list = new List<Interceptor>(interceptors)
{
new UserAgentInterceptor()
};

var callInvoker = channelToGateway.Intercept([.. list]);
gatewayClient = new Gateway.GatewayClient(callInvoker);
}


public async Task Connect()
{
await channelToGateway.ConnectAsync();
Expand Down