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

enable credential based authentication #514

Merged
merged 24 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
2895c53
utilise AzureComponentFactory to enable using the standard configurat…
PhilBastian May 20, 2024
29912fc
cleanup unused namespaces
PhilBastian May 20, 2024
cc3cffe
Fixup
danielmarbach May 22, 2024
74c6b20
Try workaround some of the DI issues for spiking purposes
danielmarbach May 21, 2024
e861810
Cleverer approach that doesn't require cloning
danielmarbach May 22, 2024
c86557a
Using
danielmarbach May 22, 2024
39c52a4
Use the transport extension in the serverless transport directly
danielmarbach May 22, 2024
93c6ec8
Cleanup after rebase
danielmarbach May 22, 2024
7501504
Switch the FunctionEndpointComponent to using a hostbuilder to be abl…
danielmarbach May 24, 2024
a1765be
Switching the sendonly test to using the FunctionEndpointComponent
danielmarbach May 24, 2024
339ecc1
Add a TODO to one of the test
danielmarbach May 24, 2024
faf0c2a
Use the scenario runner in When_no_connection_string_is_provided
danielmarbach May 24, 2024
994fb8f
Delete the installer test since that is implicitly tested by running …
danielmarbach May 24, 2024
a4d0bdd
Switch When_starting_the_function_without_installers to the scenario …
danielmarbach May 24, 2024
003d488
Reading with fallback is no longer required because IConfiguration do…
danielmarbach May 24, 2024
8ecd952
enable user to specify a custom configuration section name
PhilBastian May 27, 2024
3b3d6a0
include supplied connection in generated code
PhilBastian May 28, 2024
9e973fd
add new property to approval test result
PhilBastian May 28, 2024
ba161de
Align generated code
danielmarbach May 28, 2024
513f0c5
Add props file and service bus extensions
danielmarbach May 28, 2024
33d13ef
Fix the routing
danielmarbach May 28, 2024
733628b
Cleanup the HttpSender
danielmarbach May 28, 2024
8fb446a
Simple main
danielmarbach May 28, 2024
487d57a
Add a comment to why we are not exposing the connectionName parameter.
danielmarbach May 29, 2024
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
9 changes: 1 addition & 8 deletions src/ManualTests.HostV4/HttpSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,8 @@
using NServiceBus;


class HttpSender
class HttpSender(IFunctionEndpoint functionEndpoint)
{
readonly IFunctionEndpoint functionEndpoint;

public HttpSender(IFunctionEndpoint functionEndpoint)
{
this.functionEndpoint = functionEndpoint;
}

[Function("HttpSenderV4")]
public async Task<HttpResponseData> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestData req,
Expand Down
3 changes: 3 additions & 0 deletions src/ManualTests.HostV4/ManualTests.HostV4.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GeneratedFiles</CompilerGeneratedFilesOutputPath>
</PropertyGroup>

<Import Project="..\NServiceBus.AzureFunctions.Worker.ServiceBus\NServiceBus.AzureFunctions.Worker.ServiceBus.props" />

<ItemGroup>
<ProjectReference Include="..\NServiceBus.AzureFunctions.Worker.Analyzer\NServiceBus.AzureFunctions.Worker.Analyzer.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\NServiceBus.AzureFunctions.Worker.ServiceBus\NServiceBus.AzureFunctions.Worker.ServiceBus.csproj" />
Expand All @@ -16,6 +18,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.22.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.2.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.ServiceBus" Version="5.18.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.2" />
<PackageReference Include="NServiceBus.Transport.AzureServiceBus" Version="4.0.1" />
</ItemGroup>
Expand Down
23 changes: 8 additions & 15 deletions src/ManualTests.HostV4/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,13 @@

[assembly: NServiceBusTriggerFunction("FunctionsTestEndpoint2", TriggerFunctionName = "MyFunctionName")]

public class Program
{
public static void Main()
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.UseNServiceBus(c =>
{
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.UseNServiceBus(c =>
{
c.Routing.RouteToEndpoint(typeof(TriggerMessage), "some-queue");
c.AdvancedConfiguration.EnableInstallers();
})
.Build();
c.Routing.RouteToEndpoint(typeof(TriggerMessage), "FunctionsTestEndpoint2");
c.AdvancedConfiguration.EnableInstallers();
})
.Build();

host.Run();
}

}
host.Run();
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ using System.Threading.Tasks;
using Microsoft.Azure.Functions.Worker;
using NServiceBus;

[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
public class FunctionEndpointTrigger
{
IFunctionEndpoint endpoint;
readonly IFunctionEndpoint endpoint;

public FunctionEndpointTrigger(IFunctionEndpoint endpoint)
{
this.endpoint = endpoint;
}


[Function("trigger")]
public async Task Run(
[ServiceBusTrigger("endpoint")] byte[] messageBody,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// <autogenerated/>
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Azure.Functions.Worker;
using NServiceBus;

[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
public class FunctionEndpointTrigger
{
readonly IFunctionEndpoint endpoint;

public FunctionEndpointTrigger(IFunctionEndpoint endpoint)
{
this.endpoint = endpoint;
}

[Function("NServiceBusFunctionEndpointTrigger-endpoint")]
public async Task Run(
[ServiceBusTrigger("endpoint", Connection="FooBar")] byte[] messageBody,
IDictionary<string, object> userProperties,
string messageId,
int deliveryCount,
string correlationId,
FunctionContext context,
string replyTo = null)
{
await endpoint.Process(messageBody, userProperties, messageId, deliveryCount, replyTo, correlationId, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ using System.Threading.Tasks;
using Microsoft.Azure.Functions.Worker;
using NServiceBus;

[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
public class FunctionEndpointTrigger
{
IFunctionEndpoint endpoint;
readonly IFunctionEndpoint endpoint;

public FunctionEndpointTrigger(IFunctionEndpoint endpoint)
{
this.endpoint = endpoint;
}


[Function("NServiceBusFunctionEndpointTrigger-endpoint")]
public async Task Run(
[ServiceBusTrigger("endpoint")] byte[] messageBody,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ using System.Threading.Tasks;
using Microsoft.Azure.Functions.Worker;
using NServiceBus;

[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
public class FunctionEndpointTrigger
{
IFunctionEndpoint endpoint;
readonly IFunctionEndpoint endpoint;

public FunctionEndpointTrigger(IFunctionEndpoint endpoint)
{
this.endpoint = endpoint;
}


[Function("NServiceBusFunctionEndpointTrigger-endpoint")]
public async Task Run(
[ServiceBusTrigger("endpoint")] byte[] messageBody,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ using System.Threading.Tasks;
using Microsoft.Azure.Functions.Worker;
using NServiceBus;

[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
public class FunctionEndpointTrigger
{
IFunctionEndpoint endpoint;
readonly IFunctionEndpoint endpoint;

public FunctionEndpointTrigger(IFunctionEndpoint endpoint)
{
this.endpoint = endpoint;
}


[Function("NServiceBusFunctionEndpointTrigger-endpoint")]
public async Task Run(
[ServiceBusTrigger("endpoint")] byte[] messageBody,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,22 @@ public void Invalid_trigger_function_name_should_cause_an_error()
Assert.True(diagnostics.Any(d => d.Severity == DiagnosticSeverity.Error && d.Id == AzureFunctionsDiagnostics.InvalidTriggerFunctionNameErrorId));
}

[Test]
public void Can_supply_connection_name()
{
var source =
@"using NServiceBus;

[assembly: NServiceBusTriggerFunction(""endpoint"", Connection = ""FooBar"")]

public class Startup
{
}";
var (output, _) = GetGeneratedOutput(source);

Approver.Verify(output);
}

[OneTimeSetUp]
public void Init()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace NServiceBus.AzureFunctions.Worker.Analyzer
{
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Expand All @@ -17,6 +18,7 @@ class SyntaxReceiver : ISyntaxContextReceiver
{
internal string endpointName;
internal string triggerFunctionName;
internal string connection;
internal bool attributeFound;
internal bool isInvalidBindingExpression = false;

Expand All @@ -42,8 +44,17 @@ public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
return;
}

// 2nd parameter was triggerFunctionName
triggerFunctionName = AttributeParameterAtPosition(1);
var triggerFunctionNameAttribute = attributeSyntax.ArgumentList.Arguments.FirstOrDefault(arg => arg.GetFirstToken().ValueText == "TriggerFunctionName");
if (triggerFunctionNameAttribute != null)
{
triggerFunctionName = context.SemanticModel.GetConstantValue(triggerFunctionNameAttribute.Expression).Value?.ToString();
}

var connectionAttribute = attributeSyntax.ArgumentList.Arguments.FirstOrDefault(arg => arg.GetFirstToken().ValueText == "Connection");
if (connectionAttribute != null)
{
connection = context.SemanticModel.GetConstantValue(connectionAttribute.Expression).Value?.ToString();
}
}

bool IsNServiceBusEndpointNameAttribute(string value) => value?.Equals("NServiceBus.NServiceBusTriggerFunctionAttribute") ?? false;
Expand Down Expand Up @@ -88,26 +99,29 @@ public void Execute(GeneratorExecutionContext context)
return;
}

var connectionParam = string.IsNullOrWhiteSpace(syntaxReceiver.connection)
? ""
: $", Connection=\"{syntaxReceiver.connection}\"";
var source =
$@"// <autogenerated/>
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Azure.Functions.Worker;
using NServiceBus;

[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
public class FunctionEndpointTrigger
{{
IFunctionEndpoint endpoint;
readonly IFunctionEndpoint endpoint;

public FunctionEndpointTrigger(IFunctionEndpoint endpoint)
{{
this.endpoint = endpoint;
}}


[Function(""{syntaxReceiver.triggerFunctionName}"")]
public async Task Run(
[ServiceBusTrigger(""{syntaxReceiver.endpointName}"")] byte[] messageBody,
[ServiceBusTrigger(""{syntaxReceiver.endpointName}""{connectionParam})] byte[] messageBody,
IDictionary<string, object> userProperties,
string messageId,
int deliveryCount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ namespace NServiceBus
public sealed class NServiceBusTriggerFunctionAttribute : System.Attribute
{
public NServiceBusTriggerFunctionAttribute(string endpointName) { }
public string Connection { get; set; }
public string EndpointName { get; }
public string TriggerFunctionName { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using NServiceBus;
using NServiceBus.AcceptanceTesting.Customization;
using NServiceBus.AcceptanceTesting.Support;
using NServiceBus.AzureFunctions.Worker.ServiceBus;

class DefaultEndpoint : IEndpointSetupTemplate
{
Expand All @@ -27,7 +28,7 @@ public async Task<EndpointConfiguration> GetConfiguration(
recoverability.Immediate(immediate => immediate.NumberOfRetries(0));
configuration.SendFailedMessagesTo("error");

var connectionString = Environment.GetEnvironmentVariable(ServiceBusTriggeredEndpointConfiguration.DefaultServiceBusConnectionName);
var connectionString = Environment.GetEnvironmentVariable(ServerlessTransport.DefaultServiceBusConnectionName);
var azureServiceBusTransport = new AzureServiceBusTransport(connectionString)
{
SubscriptionRuleNamingConvention = type =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,10 @@
using System;
using System.Collections.Generic;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

sealed class FakeFunctionContext : FunctionContext
{
public FakeFunctionContext()
{
var sc = new ServiceCollection();
sc.AddSingleton<ILoggerFactory>(new TestLoggingFactory());

InstanceServices = sc.BuildServiceProvider();
}

public override string InvocationId { get; }
public override string FunctionId { get; }
public override TraceContext TraceContext { get; }
Expand All @@ -35,10 +26,7 @@ public void Dispose()
{
}

public ILogger CreateLogger(string categoryName)
{
return new DummyLogger();
}
public ILogger CreateLogger(string categoryName) => new DummyLogger();

public void AddProvider(ILoggerProvider provider)
{
Expand Down
Loading