Skip to content

Commit f8e77ff

Browse files
Add ServicePulse-specific OIDC configuration and endpoint
1 parent 03f7777 commit f8e77ff

File tree

3 files changed

+86
-3
lines changed

3 files changed

+86
-3
lines changed

src/ServiceControl/App.config

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,20 @@ These settings are only here so that we can debug ServiceControl while developin
3131
<!-- Authentication Settings (JWT with OpenID Connect) -->
3232
<!-- Uncomment and configure to enable authentication -->
3333
<!-- Leaving 'Authentication.Enabled' commented out defaults authentication to 'false'-->
34-
<!--<add key="ServiceControl/Authentication.Enabled" value="false" />
35-
<add key="ServiceControl/Authentication.Authority" value="" />
36-
<add key="ServiceControl/Authentication.Audience" value="" />-->
34+
<add key="ServiceControl/Authentication.Enabled" value="true" />
35+
<add key="ServiceControl/Authentication.Authority" value="https://login.microsoftonline.com/bf54c0f1-b0c5-406c-9d9a-013ab0361764" />
36+
<add key="ServiceControl/Authentication.Audience" value="api://78726835-cedc-4ec1-b452-6b8983d31495" />
3737
<!-- Optional Authentication Settings (defaults shown) -->
3838
<!--<add key="ServiceControl/Authentication.ValidateIssuer" value="true" />
3939
<add key="ServiceControl/Authentication.ValidateAudience" value="true" />
4040
<add key="ServiceControl/Authentication.ValidateLifetime" value="true" />
4141
<add key="ServiceControl/Authentication.ValidateIssuerSigningKey" value="true" />
4242
<add key="ServiceControl/Authentication.RequireHttpsMetadata" value="true" />-->
43+
<!-- ServicePulse Authentication Settings -->
44+
<add key="ServiceControl/Authentication.ServicePulse.Enabled" value="true" />
45+
<add key="ServiceControl/Authentication.ServicePulse.ClientId" value="1ca862df-9a0a-4f7d-87a7-d84732d83dfe" />
46+
<add key="ServiceControl/Authentication.ServicePulse.Authority" value="https://login.microsoftonline.com/bf54c0f1-b0c5-406c-9d9a-013ab0361764/v2.0" />
47+
<add key="ServiceControl/Authentication.ServicePulse.ApiScope" value="api://78726835-cedc-4ec1-b452-6b8983d31495/api.access" />
4348
</appSettings>
4449
<connectionStrings>
4550
<!-- DEVS - Pick a transport connection string to match chosen transport above -->
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
namespace ServiceControl.Authentication
2+
{
3+
using Microsoft.AspNetCore.Authorization;
4+
using Microsoft.AspNetCore.Mvc;
5+
using ServiceBus.Management.Infrastructure.Settings;
6+
7+
[ApiController]
8+
[Route("api/authentication")]
9+
public class AuthenticationController(Settings settings) : ControllerBase
10+
{
11+
[HttpGet]
12+
[AllowAnonymous]
13+
[Route("configuration")]
14+
public ActionResult<AuthConfig> Configuration()
15+
{
16+
var info = new AuthConfig
17+
{
18+
Enabled = settings.OpenIdConnectSettings.ServicePulseEnabled,
19+
ClientId = settings.OpenIdConnectSettings.ServicePulseClientId,
20+
Authority = settings.OpenIdConnectSettings.ServicePulseAuthority,
21+
ApiScope = settings.OpenIdConnectSettings.ServicePulseApiScope
22+
};
23+
24+
return Ok(info);
25+
}
26+
}
27+
28+
public class AuthConfig
29+
{
30+
public bool Enabled { get; set; }
31+
public string ClientId { get; set; }
32+
public string Authority { get; set; }
33+
public string ApiScope { get; set; }
34+
}
35+
}

src/ServiceControl/Infrastructure/Settings/OpenIdConnectSettings.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ public OpenIdConnectSettings(bool validateConfiguration)
2727
ValidateIssuerSigningKey = SettingsReader.Read(Settings.SettingsRootNamespace, "Authentication.ValidateIssuerSigningKey", true);
2828
RequireHttpsMetadata = SettingsReader.Read(Settings.SettingsRootNamespace, "Authentication.RequireHttpsMetadata", true);
2929

30+
ServicePulseEnabled = SettingsReader.Read(Settings.SettingsRootNamespace, "Authentication.ServicePulse.Enabled", false);
31+
32+
if (ServicePulseEnabled)
33+
{
34+
ServicePulseClientId = SettingsReader.Read<string>(Settings.SettingsRootNamespace, "Authentication.ServicePulse.ClientId");
35+
ServicePulseApiScope = SettingsReader.Read<string>(Settings.SettingsRootNamespace, "Authentication.ServicePulse.ApiScope");
36+
ServicePulseAuthority = SettingsReader.Read<string>(Settings.SettingsRootNamespace, "Authentication.ServicePulse.Authority");
37+
}
38+
3039
if (validateConfiguration)
3140
{
3241
Validate();
@@ -57,6 +66,18 @@ public OpenIdConnectSettings(bool validateConfiguration)
5766
[JsonPropertyName("requireHttpsMetadata")]
5867
public bool RequireHttpsMetadata { get; }
5968

69+
[JsonPropertyName("servicePulseEnabled")]
70+
public bool ServicePulseEnabled { get; }
71+
72+
[JsonPropertyName("servicePulseAuthority")]
73+
public string ServicePulseAuthority { get; }
74+
75+
[JsonPropertyName("servicePulseClientId")]
76+
public string ServicePulseClientId { get; }
77+
78+
[JsonPropertyName("servicePulseApiScope")]
79+
public string ServicePulseApiScope { get; }
80+
6081
void Validate()
6182
{
6283
if (!Enabled)
@@ -112,6 +133,24 @@ void Validate()
112133
logger.LogWarning("Authentication.ValidateIssuerSigningKey is set to false. This is a serious security risk and should only be used in development environments");
113134
}
114135

136+
if (ServicePulseEnabled)
137+
{
138+
if (string.IsNullOrWhiteSpace(ServicePulseClientId))
139+
{
140+
throw new Exception("Authentication.ServicePulse.ClientId is required when Authentication.ServicePulse.Enabled is true.");
141+
}
142+
143+
if (string.IsNullOrWhiteSpace(ServicePulseApiScope))
144+
{
145+
throw new Exception("Authentication.ServicePulse.ApiScope is required when Authentication.ServicePulse.Enabled is true.");
146+
}
147+
148+
if (ServicePulseAuthority != null && !Uri.TryCreate(ServicePulseAuthority, UriKind.Absolute, out _))
149+
{
150+
throw new Exception("Authentication.ServicePulse.Authority must be a valid absolute URI if provided.");
151+
}
152+
}
153+
115154
logger.LogInformation("Authentication configuration validated successfully");
116155
logger.LogInformation(" Authority: {Authority}", Authority);
117156
logger.LogInformation(" Audience: {Audience}", Audience);
@@ -120,6 +159,10 @@ void Validate()
120159
logger.LogInformation(" ValidateLifetime: {ValidateLifetime}", ValidateLifetime);
121160
logger.LogInformation(" ValidateIssuerSigningKey: {ValidateIssuerSigningKey}", ValidateIssuerSigningKey);
122161
logger.LogInformation(" RequireHttpsMetadata: {RequireHttpsMetadata}", RequireHttpsMetadata);
162+
logger.LogInformation(" ServicePulseEnabled: {ServicePulseEnabled}", ServicePulseEnabled);
163+
logger.LogInformation(" ServicePulseClientId: {ServicePulseClientId}", ServicePulseClientId);
164+
logger.LogInformation(" ServicePulseAuthority: {ServicePulseAuthority}", ServicePulseAuthority);
165+
logger.LogInformation(" ServicePulseApiScope: {ServicePulseApiScope}", ServicePulseApiScope);
123166
}
124167
}
125168
}

0 commit comments

Comments
 (0)