Skip to content

Commit 1674e65

Browse files
committed
fix(Runner): Fixed the AuthorizationInfo to resolve referenced authentication policies defined at top level
Signed-off-by: Charles d'Avernas <[email protected]>
1 parent b860edb commit 1674e65

File tree

6 files changed

+18
-8
lines changed

6 files changed

+18
-8
lines changed

src/core/Synapse.Core/SynapseDefaults.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ public static class Secrets
766766
/// <summary>
767767
/// Gets the prefix for all secrets related environment variables
768768
/// </summary>
769-
public const string Prefix = EnvironmentVariables.Prefix + "SECRETS";
769+
public const string Prefix = EnvironmentVariables.Prefix + "SECRETS_";
770770
/// <summary>
771771
/// Gets the name of the environment variable used to configure the path to the directory that contains secrets files
772772
/// </summary>

src/runner/Synapse.Runner/AuthorizationInfo.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,22 @@ public class AuthorizationInfo(string scheme, string parameter)
4242
/// <summary>
4343
/// Creates a new <see cref="AuthorizationInfo"/> based on the specified <see cref="AuthenticationPolicyDefinition"/>
4444
/// </summary>
45+
/// <param name="workflow">The <see cref="WorkflowDefinition"/> that defines the <see cref="AuthenticationPolicyDefinition"/> to create a new <see cref="AuthorizationInfo"/> for</param>
4546
/// <param name="authentication">The <see cref="AuthenticationPolicyDefinition"/> to create a new <see cref="AuthorizationInfo"/> for</param>
4647
/// <param name="serviceProvider">The current <see cref="IServiceProvider"/></param>
4748
/// <param name="cancellationToken">A <see cref="CancellationToken"/></param>
4849
/// <returns>A new <see cref="AuthorizationInfo"/> based on the specified <see cref="AuthenticationPolicyDefinition"/></returns>
49-
public static async Task<AuthorizationInfo> CreateAsync(AuthenticationPolicyDefinition authentication, IServiceProvider serviceProvider, CancellationToken cancellationToken = default)
50+
public static async Task<AuthorizationInfo> CreateAsync(WorkflowDefinition workflow, AuthenticationPolicyDefinition authentication, IServiceProvider serviceProvider, CancellationToken cancellationToken = default)
5051
{
5152
ArgumentNullException.ThrowIfNull(nameof(authentication));
5253
ArgumentNullException.ThrowIfNull(nameof(serviceProvider));
5354
string scheme, parameter;
5455
var logger = serviceProvider.GetRequiredService<ILoggerFactory>().CreateLogger("AuthenticationPolicyHandler");
56+
if (!string.IsNullOrWhiteSpace(authentication.Use))
57+
{
58+
if (workflow.Use?.Authentications?.TryGetValue(authentication.Use, out AuthenticationPolicyDefinition? referencedAuthentication) != true || referencedAuthentication == null) throw new NullReferenceException($"Failed to find the specified authentication policy '{authentication.Use}'");
59+
else authentication = referencedAuthentication;
60+
}
5561
var isSecretBased = authentication.TryGetBaseSecret(out var secretName);
5662
object? authenticationProperties = null;
5763
if (isSecretBased && !string.IsNullOrWhiteSpace(secretName))

src/runner/Synapse.Runner/Extensions/HttpClientExtensions.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@ public static class HttpClientExtensions
2525
/// Configures the <see cref="HttpClient"/> to use the specified authentication mechanism
2626
/// </summary>
2727
/// <param name="httpClient">The <see cref="HttpClient"/> to configure</param>
28+
/// <param name="workflow">The <see cref="WorkflowDefinition"/> that defines the authentication to configure</param>
2829
/// <param name="authentication">An object that describes the authentication mechanism to use</param>
2930
/// <param name="serviceProvider">The current <see cref="IServiceProvider"/></param>
3031
/// <param name="cancellationToken">A <see cref="CancellationToken"/></param>
3132
/// <returns>A new awaitable <see cref="Task"/></returns>
32-
public static async Task ConfigureAuthenticationAsync(this HttpClient httpClient, AuthenticationPolicyDefinition? authentication, IServiceProvider serviceProvider, CancellationToken cancellationToken = default)
33+
public static async Task ConfigureAuthenticationAsync(this HttpClient httpClient, WorkflowDefinition workflow, AuthenticationPolicyDefinition? authentication, IServiceProvider serviceProvider, CancellationToken cancellationToken = default)
3334
{
35+
ArgumentNullException.ThrowIfNull(workflow);
3436
if (authentication == null) return;
35-
var authorization = await AuthorizationInfo.CreateAsync(authentication, serviceProvider, cancellationToken).ConfigureAwait(false);
37+
var authorization = await AuthorizationInfo.CreateAsync(workflow, authentication, serviceProvider, cancellationToken).ConfigureAwait(false);
3638
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(authorization.Scheme, authorization.Parameter);
3739
}
3840

src/runner/Synapse.Runner/Services/Executors/HttpCallExecutor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ protected override async Task DoInitializeAsync(CancellationToken cancellationTo
5656
{
5757
this.Http = (HttpCallDefinition)this.JsonSerializer.Convert(this.Task.Definition.With, typeof(HttpCallDefinition))!;
5858
var authentication = this.Http.Endpoint.Authentication == null ? null : await this.Task.Workflow.Expressions.EvaluateAsync<AuthenticationPolicyDefinition>(this.Http.Endpoint.Authentication, this.Task.Input, this.Task.Arguments, cancellationToken).ConfigureAwait(false);
59-
await this.HttpClient.ConfigureAuthenticationAsync(authentication, this.ServiceProvider, cancellationToken).ConfigureAwait(false);
59+
await this.HttpClient.ConfigureAuthenticationAsync(this.Task.Workflow.Definition, authentication, this.ServiceProvider, cancellationToken).ConfigureAwait(false);
6060
}
6161
catch(Exception ex)
6262
{

src/runner/Synapse.Runner/Services/Executors/OpenApiCallExecutor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ protected override async Task DoInitializeAsync(CancellationToken cancellationTo
109109
{
110110
this.OpenApi = (OpenApiCallDefinition)this.JsonSerializer.Convert(this.Task.Definition.With, typeof(OpenApiCallDefinition))!;
111111
using var httpClient = this.HttpClientFactory.CreateClient();
112-
await httpClient.ConfigureAuthenticationAsync(this.OpenApi.Document.Endpoint.Authentication, this.ServiceProvider, cancellationToken).ConfigureAwait(false);
112+
await httpClient.ConfigureAuthenticationAsync(this.Task.Workflow.Definition, this.OpenApi.Document.Endpoint.Authentication, this.ServiceProvider, cancellationToken).ConfigureAwait(false);
113113
using var request = new HttpRequestMessage(HttpMethod.Get, this.OpenApi.Document.EndpointUri);
114114
using var response = await httpClient.SendAsync(request, cancellationToken).ConfigureAwait(false);
115115
if (!response.IsSuccessStatusCode)
@@ -226,7 +226,7 @@ protected override async Task DoExecuteAsync(CancellationToken cancellationToken
226226
}
227227
}
228228
using var httpClient = this.HttpClientFactory.CreateClient();
229-
await httpClient.ConfigureAuthenticationAsync(this.OpenApi.Authentication, this.ServiceProvider, cancellationToken).ConfigureAwait(false);
229+
await httpClient.ConfigureAuthenticationAsync(this.Task.Workflow.Definition, this.OpenApi.Authentication, this.ServiceProvider, cancellationToken).ConfigureAwait(false);
230230
using var response = await httpClient.SendAsync(request, cancellationToken);
231231
if (response.StatusCode == HttpStatusCode.ServiceUnavailable) continue;
232232
var rawContent = await response.Content.ReadAsByteArrayAsync(cancellationToken)!;

src/runner/Synapse.Runner/Services/ExternalResourceProvider.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ public virtual async Task<Stream> ReadAsync(WorkflowDefinition workflow, Externa
5454
/// <returns>The specified <see cref="ExternalResourceDefinition"/>'s content <see cref="Stream"/></returns>
5555
protected virtual async Task<Stream> ReadOverHttpAsync(WorkflowDefinition workflow, ExternalResourceDefinition resource, CancellationToken cancellationToken = default)
5656
{
57+
ArgumentNullException.ThrowIfNull(workflow);
58+
ArgumentNullException.ThrowIfNull(resource);
5759
using var httpClient = this.HttpClientFactory.CreateClient();
58-
await httpClient.ConfigureAuthenticationAsync(resource.Endpoint.Authentication, this.ServiceProvider, cancellationToken).ConfigureAwait(false);
60+
await httpClient.ConfigureAuthenticationAsync(workflow, resource.Endpoint.Authentication, this.ServiceProvider, cancellationToken).ConfigureAwait(false);
5961
return await httpClient.GetStreamAsync(resource.EndpointUri, cancellationToken).ConfigureAwait(false);
6062
}
6163

0 commit comments

Comments
 (0)