|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +#nullable enable |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.IO; |
| 7 | +using System.Net; |
| 8 | +using System.Net.Http; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using Azure.DataApiBuilder.Config.ObjectModel; |
| 11 | +using Azure.DataApiBuilder.Core.Authorization; |
| 12 | +using Microsoft.AspNetCore.TestHost; |
| 13 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 14 | + |
| 15 | +namespace Azure.DataApiBuilder.Service.Tests.Configuration |
| 16 | +{ |
| 17 | + [TestClass] |
| 18 | + public class HealthEndpointRolesTests |
| 19 | + { |
| 20 | + private const string STARTUP_CONFIG_ROLE = "authenticated"; |
| 21 | + |
| 22 | + private const string CUSTOM_CONFIG_FILENAME = "custom-config.json"; |
| 23 | + |
| 24 | + [TestCleanup] |
| 25 | + public void CleanupAfterEachTest() |
| 26 | + { |
| 27 | + if (File.Exists(CUSTOM_CONFIG_FILENAME)) |
| 28 | + { |
| 29 | + File.Delete(CUSTOM_CONFIG_FILENAME); |
| 30 | + } |
| 31 | + |
| 32 | + TestHelper.UnsetAllDABEnvironmentVariables(); |
| 33 | + } |
| 34 | + |
| 35 | + [TestMethod] |
| 36 | + [TestCategory(TestCategory.MSSQL)] |
| 37 | + [DataRow(null, null, DisplayName = "Validate Health Report when roles is not configured and HostMode is null.")] |
| 38 | + [DataRow(null, HostMode.Development, DisplayName = "Validate Health Report when roles is not configured and HostMode is Development.")] |
| 39 | + [DataRow(null, HostMode.Production, DisplayName = "Validate Health Report when roles is not configured and HostMode is Production.")] |
| 40 | + [DataRow("authenticated", HostMode.Production, DisplayName = "Validate Health Report when roles is configured to 'authenticated' and HostMode is Production.")] |
| 41 | + [DataRow("temp-role", HostMode.Production, DisplayName = "Validate Health Report when roles is configured to 'temp-role' which is not in token and HostMode is Production.")] |
| 42 | + [DataRow("authenticated", HostMode.Development, DisplayName = "Validate Health Report when roles is configured to 'authenticated' and HostMode is Development.")] |
| 43 | + [DataRow("temp-role", HostMode.Development, DisplayName = "Validate Health Report when roles is configured to 'temp-role' which is not in token and HostMode is Development.")] |
| 44 | + public async Task ComprehensiveHealthEndpoint_RolesTests(string role, HostMode hostMode) |
| 45 | + { |
| 46 | + // Arrange |
| 47 | + // At least one entity is required in the runtime config for the engine to start. |
| 48 | + // Even though this entity is not under test, it must be supplied enable successful |
| 49 | + // config file creation. |
| 50 | + Entity requiredEntity = new( |
| 51 | + Health: new(Enabled: true), |
| 52 | + Source: new("books", EntitySourceType.Table, null, null), |
| 53 | + Rest: new(Enabled: true), |
| 54 | + GraphQL: new("book", "books", true), |
| 55 | + Permissions: new[] { ConfigurationTests.GetMinimalPermissionConfig(AuthorizationResolver.ROLE_ANONYMOUS) }, |
| 56 | + Relationships: null, |
| 57 | + Mappings: null); |
| 58 | + |
| 59 | + Dictionary<string, Entity> entityMap = new() |
| 60 | + { |
| 61 | + { "Book", requiredEntity } |
| 62 | + }; |
| 63 | + |
| 64 | + CreateCustomConfigFile(entityMap, role, hostMode); |
| 65 | + |
| 66 | + string[] args = new[] |
| 67 | + { |
| 68 | + $"--ConfigFileName={CUSTOM_CONFIG_FILENAME}" |
| 69 | + }; |
| 70 | + |
| 71 | + using (TestServer server = new(Program.CreateWebHostBuilder(args))) |
| 72 | + using (HttpClient client = server.CreateClient()) |
| 73 | + { |
| 74 | + // Sends a GET request to a protected entity which requires a specific role to access. |
| 75 | + // Authorization checks |
| 76 | + HttpRequestMessage message = new(method: HttpMethod.Get, requestUri: $"/health"); |
| 77 | + string swaTokenPayload = AuthTestHelper.CreateStaticWebAppsEasyAuthToken( |
| 78 | + addAuthenticated: true, |
| 79 | + specificRole: STARTUP_CONFIG_ROLE); |
| 80 | + message.Headers.Add(AuthenticationOptions.CLIENT_PRINCIPAL_HEADER, swaTokenPayload); |
| 81 | + message.Headers.Add(AuthorizationResolver.CLIENT_ROLE_HEADER, STARTUP_CONFIG_ROLE); |
| 82 | + HttpResponseMessage authorizedResponse = await client.SendAsync(message); |
| 83 | + |
| 84 | + switch (role) |
| 85 | + { |
| 86 | + case null: |
| 87 | + if (hostMode == HostMode.Development) |
| 88 | + { |
| 89 | + Assert.AreEqual(expected: HttpStatusCode.OK, actual: authorizedResponse.StatusCode); |
| 90 | + } |
| 91 | + else |
| 92 | + { |
| 93 | + Assert.AreEqual(expected: HttpStatusCode.Forbidden, actual: authorizedResponse.StatusCode); |
| 94 | + } |
| 95 | + |
| 96 | + break; |
| 97 | + case "temp-role": |
| 98 | + Assert.AreEqual(expected: HttpStatusCode.Forbidden, actual: authorizedResponse.StatusCode); |
| 99 | + break; |
| 100 | + |
| 101 | + default: |
| 102 | + Assert.AreEqual(expected: HttpStatusCode.OK, actual: authorizedResponse.StatusCode); |
| 103 | + break; |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /// <summary> |
| 109 | + /// Helper function to write custom configuration file with minimal REST/GraphQL global settings |
| 110 | + /// using the supplied entities. |
| 111 | + /// </summary> |
| 112 | + /// <param name="entityMap">Collection of entityName -> Entity object.</param> |
| 113 | + /// <param name="role">Allowed Roles for comprehensive health endpoint.</param> |
| 114 | + private static void CreateCustomConfigFile(Dictionary<string, Entity> entityMap, string? role, HostMode hostMode = HostMode.Production) |
| 115 | + { |
| 116 | + DataSource dataSource = new( |
| 117 | + DatabaseType.MSSQL, |
| 118 | + ConfigurationTests.GetConnectionStringFromEnvironmentConfig(environment: TestCategory.MSSQL), |
| 119 | + Options: null, |
| 120 | + Health: new(true)); |
| 121 | + HostOptions hostOptions = new(Mode: hostMode, Cors: null, Authentication: new() { Provider = nameof(EasyAuthType.StaticWebApps) }); |
| 122 | + |
| 123 | + RuntimeConfig runtimeConfig = new( |
| 124 | + Schema: string.Empty, |
| 125 | + DataSource: dataSource, |
| 126 | + Runtime: new( |
| 127 | + Health: new(Enabled: true, Roles: role != null ? new HashSet<string> { role } : null), |
| 128 | + Rest: new(Enabled: true), |
| 129 | + GraphQL: new(Enabled: true), |
| 130 | + Host: hostOptions |
| 131 | + ), |
| 132 | + Entities: new(entityMap)); |
| 133 | + |
| 134 | + File.WriteAllText( |
| 135 | + path: CUSTOM_CONFIG_FILENAME, |
| 136 | + contents: runtimeConfig.ToJson()); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments