Skip to content

Commit

Permalink
enhancement: Metadata (headers) support
Browse files Browse the repository at this point in the history
Signed-off-by: Oğuzhan Durgun <oguzhandurgun95@gmail.com>
  • Loading branch information
oguzhand95 committed Dec 12, 2023
1 parent 7cdf828 commit 583df3e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
15 changes: 15 additions & 0 deletions src/Sdk/Cerbos/Sdk/Builder/CerbosClientBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public sealed class CerbosClientBuilder
private StreamReader TlsCertificate { get; set; }
private StreamReader TlsKey { get; set; }
private GrpcChannelOptions GrpcChannelOptions { get; set; }
private Metadata Metadata { get; set; }

private CerbosClientBuilder(string target) {
Target = target;
Expand All @@ -30,6 +31,12 @@ public static CerbosClientBuilder ForTarget(string target)
return new CerbosClientBuilder(target);
}

public CerbosClientBuilder WithMetadata(Metadata headers)
{
Metadata = headers;
return this;
}

public CerbosClientBuilder WithPlaintext() {
Plaintext = true;
return this;
Expand Down Expand Up @@ -87,6 +94,14 @@ public CerbosClient Build()
callCredentials = CallCredentials.FromInterceptor((context, metadata) =>
{
metadata.Add(PlaygroundInstanceHeader, PlaygroundInstanceId.Trim());
if (Metadata != null)
{
foreach (var m in Metadata)
{
metadata.Add(m);
}
}

return Task.CompletedTask;
});
}
Expand Down
17 changes: 9 additions & 8 deletions src/Sdk/Cerbos/Sdk/CerbosClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Threading.Tasks;
using Cerbos.Sdk.Response;
using Grpc.Core;

namespace Cerbos.Sdk
{
Expand All @@ -22,11 +23,11 @@ public CerbosClient(Api.V1.Svc.CerbosService.CerbosServiceClient cerbosServiceCl
/// <summary>
/// Send a request consisting of a principal, resource(s) & action(s) to see if the principal is authorized to do the action(s) on the resource(s).
/// </summary>
public CheckResourcesResponse CheckResources(Builder.CheckResourcesRequest request)
public CheckResourcesResponse CheckResources(Builder.CheckResourcesRequest request, Metadata headers = null)
{
try
{
return new CheckResourcesResponse(CerbosServiceClient.CheckResources(request.ToCheckResourcesRequest()));
return new CheckResourcesResponse(CerbosServiceClient.CheckResources(request.ToCheckResourcesRequest(), headers));
}
catch (Exception e)
{
Expand All @@ -37,12 +38,12 @@ public CheckResourcesResponse CheckResources(Builder.CheckResourcesRequest reque
/// <summary>
/// Send an async request consisting of a principal, resource(s) & action(s) to see if the principal is authorized to do the action(s) on the resource(s).
/// </summary>
public Task<CheckResourcesResponse> CheckResourcesAsync(Builder.CheckResourcesRequest request)
public Task<CheckResourcesResponse> CheckResourcesAsync(Builder.CheckResourcesRequest request, Metadata headers = null)
{
try
{
return CerbosServiceClient
.CheckResourcesAsync(request.ToCheckResourcesRequest())
.CheckResourcesAsync(request.ToCheckResourcesRequest(), headers)
.ResponseAsync
.ContinueWith(
r => new CheckResourcesResponse(r.Result)
Expand All @@ -57,11 +58,11 @@ public Task<CheckResourcesResponse> CheckResourcesAsync(Builder.CheckResourcesRe
/// <summary>
/// Obtain a query plan for performing the given action on the given resource kind.
/// </summary>
public PlanResourcesResponse PlanResources(Builder.PlanResourcesRequest request)
public PlanResourcesResponse PlanResources(Builder.PlanResourcesRequest request, Metadata headers = null)
{
try
{
return new PlanResourcesResponse(CerbosServiceClient.PlanResources(request.ToPlanResourcesRequest()));
return new PlanResourcesResponse(CerbosServiceClient.PlanResources(request.ToPlanResourcesRequest(), headers));
}
catch (Exception e)
{
Expand All @@ -72,12 +73,12 @@ public PlanResourcesResponse PlanResources(Builder.PlanResourcesRequest request)
/// <summary>
/// Obtain a query plan for performing the given action on the given resource kind.
/// </summary>
public Task<PlanResourcesResponse> PlanResourcesAsync(Builder.PlanResourcesRequest request)
public Task<PlanResourcesResponse> PlanResourcesAsync(Builder.PlanResourcesRequest request, Metadata headers = null)
{
try
{
return CerbosServiceClient
.PlanResourcesAsync(request.ToPlanResourcesRequest())
.PlanResourcesAsync(request.ToPlanResourcesRequest(), headers)
.ResponseAsync
.ContinueWith(
r => new PlanResourcesResponse(r.Result)
Expand Down

0 comments on commit 583df3e

Please sign in to comment.