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

feat: add agent header to auth client, add client identifiers to all agent headers #559

Merged
merged 1 commit into from
Jun 18, 2024
Merged
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
2 changes: 1 addition & 1 deletion src/Momento.Sdk/AuthClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public AuthClient(IAuthConfiguration config, ICredentialProvider authProvider)
{
scsTokenClient = new ScsTokenClient(config, authProvider.AuthToken, authProvider.TokenEndpoint);
}

/// <inheritdoc />
public async Task<GenerateDisposableTokenResponse> GenerateDisposableTokenAsync(DisposableTokenScope scope, ExpiresIn expiresIn, string? tokenId = null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Momento.Sdk/Internal/ScsDataClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected Metadata MetadataWithCache(string cacheName)
this.hasSentOnetimeHeaders = true;
string sdkVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
string runtimeVer = System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription;
return new Metadata() { { "cache", cacheName }, { "Agent", $"dotnet:{sdkVersion}" }, { "Runtime-Version", runtimeVer } };
return new Metadata() { { "cache", cacheName }, { "Agent", $"dotnet:cache:{sdkVersion}" }, { "Runtime-Version", runtimeVer } };
}
protected DateTime CalculateDeadline()
{
Expand Down
16 changes: 15 additions & 1 deletion src/Momento.Sdk/Internal/ScsTokenClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member

using System;
using System.Reflection;
using System.Threading.Tasks;
using Grpc.Core;
using Microsoft.Extensions.Logging;
Expand All @@ -21,6 +22,7 @@ internal sealed class ScsTokenClient : IDisposable
private readonly string authToken;
private readonly TimeSpan authClientOperationTimeout;
private readonly ILogger _logger;
private bool hasSentOnetimeHeaders = false;
private readonly CacheExceptionMapper _exceptionMapper;
public ScsTokenClient(IAuthConfiguration config, string authToken, string endpoint)
{
Expand All @@ -30,6 +32,17 @@ public ScsTokenClient(IAuthConfiguration config, string authToken, string endpoi
this._logger = config.LoggerFactory.CreateLogger<ScsTokenClient>();
this._exceptionMapper = new CacheExceptionMapper(config.LoggerFactory);
}

private Metadata Metadata()
{
if (this.hasSentOnetimeHeaders) {
return new Metadata();
}
this.hasSentOnetimeHeaders = true;
string sdkVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
string runtimeVer = System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription;
return new Metadata() { { "Agent", $"dotnet:auth:{sdkVersion}" }, { "Runtime-Version", runtimeVer } };
}

private DateTime CalculateDeadline()
{
Expand Down Expand Up @@ -65,9 +78,10 @@ public async Task<GenerateDisposableTokenResponse> GenerateDisposableToken(
Permissions = permissions,
TokenId = tokenId ?? ""
};
var metadata = Metadata();
_logger.LogTraceExecutingGenericRequest(RequestTypeAuthGenerateDisposableToken);
var response = await grpcManager.Client.generateDisposableToken(
request, new CallOptions(deadline: CalculateDeadline())
request, new CallOptions(headers: metadata, deadline: CalculateDeadline())
);
return _logger.LogTraceGenericRequestSuccess(RequestTypeAuthGenerateDisposableToken,
new GenerateDisposableTokenResponse.Success(response));
Expand Down
4 changes: 2 additions & 2 deletions src/Momento.Sdk/Internal/ScsTopicClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ public ScsTopicClientBase(ITopicConfiguration config, string authToken, string e
this._exceptionMapper = new CacheExceptionMapper(config.LoggerFactory);
}

protected Metadata MetadataWithCache(string cacheName)
private Metadata MetadataWithCache(string cacheName)
{
if (this.hasSentOnetimeHeaders) {
return new Metadata() { { "cache", cacheName } };
}
this.hasSentOnetimeHeaders = true;
string sdkVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
string runtimeVer = System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription;
return new Metadata() { { "cache", cacheName }, { "Agent", $"dotnet:{sdkVersion}" }, { "Runtime-Version", runtimeVer } };
return new Metadata() { { "cache", cacheName }, { "Agent", $"dotnet:topic:{sdkVersion}" }, { "Runtime-Version", runtimeVer } };
}

protected DateTime CalculateDeadline()
Expand Down
Loading