Skip to content

Commit bba90ac

Browse files
authored
Update all HTTP actions with authentication type ManagedServiceIdentity to use None as the authentication type (#51)
1 parent aa3f53a commit bba90ac

File tree

5 files changed

+140
-0
lines changed

5 files changed

+140
-0
lines changed

src/LogicAppUnit.Samples.LogicApps.Tests/Constants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public static class Constants
1515
public static readonly string FLUENT_REQUEST_MATCHING_WORKFLOW = "fluent-workflow";
1616
public static readonly string HTTP_WORKFLOW = "http-workflow";
1717
public static readonly string HTTP_ASYNC_WORKFLOW = "http-async-workflow";
18+
public static readonly string HTTP_WITH_MANAGED_IDENTITY_WORKFLOW = "http-with-managed-identity-workflow";
1819
public static readonly string INLINE_SCRIPT_WORKFLOW = "inline-script-workflow";
1920
public static readonly string INVOKE_WORKFLOW = "invoke-workflow";
2021
public static readonly string LOOP_WORKFLOW = "loop-workflow";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Net;
5+
using System.Net.Http;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using LogicAppUnit.Helper;
9+
using LogicAppUnit.Mocking;
10+
using Microsoft.VisualStudio.TestTools.UnitTesting;
11+
12+
namespace LogicAppUnit.Samples.LogicApps.Tests.HttpWithManagedIdentityWorkflow
13+
{
14+
/// <summary>
15+
/// Test cases for the <i>http-with-managed-identity-workflow</i> workflow which has an HTTP action with Managed Identity as the authentication type.
16+
/// </summary>
17+
[TestClass]
18+
public class HttpWithManagedIdentityWorkflowTest : WorkflowTestBase
19+
{
20+
[TestInitialize]
21+
public void TestInitialize()
22+
{
23+
Initialize(Constants.LOGIC_APP_TEST_EXAMPLE_BASE_PATH, Constants.HTTP_WITH_MANAGED_IDENTITY_WORKFLOW);
24+
}
25+
26+
[ClassCleanup]
27+
public static void CleanResources()
28+
{
29+
Close();
30+
}
31+
32+
/// <summary>
33+
/// Tests that the correct response is returned when the HTTP call to the Service to get the customers is successful.
34+
/// </summary>
35+
[TestMethod]
36+
public void HttpWithManagedIdentityWorkflowTest_When_Successful()
37+
{
38+
using (ITestRunner testRunner = CreateTestRunner())
39+
{
40+
// Configure mock responses
41+
testRunner
42+
.AddMockResponse(
43+
MockRequestMatcher.Create()
44+
.UsingGet()
45+
.WithPath(PathMatchType.Exact, "/api/v1/customers"))
46+
.RespondWith(
47+
MockResponseBuilder.Create()
48+
.WithSuccess());
49+
50+
// Run the workflow
51+
var workflowResponse = testRunner.TriggerWorkflow(HttpMethod.Post);
52+
53+
// Check workflow run status
54+
Assert.AreEqual(WorkflowRunStatus.Succeeded, testRunner.WorkflowRunStatus);
55+
56+
// Check workflow response
57+
testRunner.ExceptionWrapper(() => Assert.AreEqual(HttpStatusCode.OK, workflowResponse.StatusCode));
58+
59+
// Check action result
60+
Assert.AreEqual(ActionStatus.Succeeded, testRunner.GetWorkflowActionStatus("Get_Customers_from_Service_One"));
61+
Assert.AreEqual(ActionStatus.Succeeded, testRunner.GetWorkflowActionStatus("Success_Response"));
62+
}
63+
}
64+
}
65+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"definition": {
3+
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
4+
"actions": {
5+
"Get_Customers_from_Service_One": {
6+
"type": "Http",
7+
"description": "Track some properties in this action",
8+
"inputs": {
9+
"uri": "@{parameters('ServiceOne-Url')}/customers",
10+
"method": "GET",
11+
"headers": {
12+
"x-api-key": "ApiKey @{parameters('ServiceOne-Authentication-APIKey')}"
13+
},
14+
"authentication": {
15+
"type": "ManagedServiceIdentity",
16+
"audience": "api://sample-audience"
17+
}
18+
},
19+
"runAfter": {},
20+
"operationOptions": "DisableAsyncPattern"
21+
},
22+
"Success_Response": {
23+
"type": "Response",
24+
"kind": "Http",
25+
"inputs": {
26+
"statusCode": 200
27+
},
28+
"runAfter": {
29+
"Get_Customers_from_Service_One": [
30+
"SUCCEEDED"
31+
]
32+
}
33+
}
34+
},
35+
"contentVersion": "1.0.0.0",
36+
"outputs": {},
37+
"triggers": {
38+
"Receive_HTTP_request": {
39+
"type": "Request",
40+
"kind": "Http",
41+
"inputs": {
42+
"method": "POST"
43+
},
44+
"operationOptions": "SuppressWorkflowHeadersOnResponse"
45+
}
46+
}
47+
},
48+
"kind": "Stateful"
49+
}

src/LogicAppUnit/WorkflowTestBase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ private void ProcessWorkflowDefinitionFile(string logicAppBasePath, string workf
267267
_workflowDefinition.ReplaceInvokeWorkflowActionsWithHttp();
268268
_workflowDefinition.ReplaceCallLocalFunctionActionsWithHttp();
269269
_workflowDefinition.ReplaceBuiltInConnectorActionsWithHttp(_testConfig.Workflow.BuiltInConnectorsToMock);
270+
_workflowDefinition.ReplaceManagedIdentityAuthenticationTypeWithNone();
270271
}
271272

272273
/// <summary>

src/LogicAppUnit/Wrapper/WorkflowDefinitionWrapper.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,5 +296,29 @@ public void ReplaceCallLocalFunctionActionsWithHttp()
296296
});
297297
}
298298
}
299+
300+
/// <summary>
301+
/// Update all HTTP actions with authentication type <i>ManagedServiceIdentity</i> to use <i>None</i> as the authentication type.
302+
/// </summary>
303+
/// <remarks>
304+
/// The <i>ManagedServiceIdentity</i> is not supported.
305+
/// </remarks>
306+
public void ReplaceManagedIdentityAuthenticationTypeWithNone()
307+
{
308+
var httpActionsWithManagedIdentityAuthenticationType = _jObjectWorkflow.SelectTokens("$..actions.*").Where(x => x["type"].ToString() == "Http")
309+
.Where(x => x["inputs"]?["authentication"]?["type"].ToString() == "ManagedServiceIdentity").Select(x => x["inputs"]?["authentication"] as JObject).ToList();
310+
311+
if (httpActionsWithManagedIdentityAuthenticationType.Count > 0)
312+
{
313+
Console.WriteLine("Updating workflow HTTP actions to replace authentication type `ManagedServiceIdentity` with `None`:");
314+
315+
httpActionsWithManagedIdentityAuthenticationType.ForEach(x =>
316+
{
317+
x["type"] = "None";
318+
319+
Console.WriteLine($" {((JProperty)x.Parent.Parent.Parent.Parent.Parent).Name}");
320+
});
321+
}
322+
}
299323
}
300324
}

0 commit comments

Comments
 (0)