Skip to content

Commit 1811971

Browse files
authored
Merge pull request #13 from microsoft/restrict-links-to-platform-deployment
Restrict links to platform deployment and add global exception handling
2 parents e84ac1e + c1bcb63 commit 1811971

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

src/MessageHandlers/LinkRequestHandler.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,22 @@ private void LinkRequestHandler(MessageFormats.HostServices.Link.LinkRequest? me
2929
// Update the request if our plugins changed it
3030
if (pluginResult == null) {
3131
_logger.LogInformation("Plugins nullified '{messageType}'. Dropping Message (trackingId: '{trackingId}' / correlationId: '{correlationId}')", message.GetType().Name, message.RequestHeader.TrackingId, message.RequestHeader.CorrelationId);
32+
returnResponse.ResponseHeader.Message = "LinkRequest rejected by plugins. For more information, see the logs and/or contact your cluster administrator.";
33+
returnResponse.ResponseHeader.Status = MessageFormats.Common.StatusCodes.Rejected;
34+
_client.DirectToApp(appId: fullMessage.SourceAppId, message: returnResponse);
3235
return;
3336
}
3437

3538
returnResponse.LinkRequest = pluginResult;
3639

40+
if (string.Equals(returnResponse.LinkRequest.DestinationAppId, "platform-deployment", StringComparison.OrdinalIgnoreCase) && !_appConfig.ALLOW_LINKS_TO_DEPLOYMENT_SVC) {
41+
_logger.LogWarning("LinkRequest to deployment service (platform-deployment) is disabled by configuration. Rejecting link request (trackingId: '{trackingId}' / correlationId: '{correlationId}')", message.RequestHeader.TrackingId, message.RequestHeader.CorrelationId);
42+
returnResponse.ResponseHeader.Message = "LinkRequest to deployment service (platform-deployment) is disabled by configuration.";
43+
returnResponse.ResponseHeader.Status = MessageFormats.Common.StatusCodes.Unauthorized;
44+
_client.DirectToApp(appId: fullMessage.SourceAppId, message: returnResponse);
45+
return;
46+
}
47+
3748
_logger.LogDebug("Passing '{messageType}' to FileMoverService for processing. (trackingId: '{trackingId}' / correlationId: '{correlationId}')", message.GetType().Name, message.RequestHeader.TrackingId, message.RequestHeader.CorrelationId);
3849

3950
_fileMoverService.QueueFileMove(returnResponse);

src/Models/AppConfig.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ public PLUG_IN() {
3232
public int FILEMOVER_POLLING_MS { get; set; }
3333
public string LEAVE_SOURCE_FILE_PROPERTY_VALUE { get; set; }
3434
public string ALL_XFER_DIR { get; set; }
35+
public bool ALLOW_LINKS_TO_DEPLOYMENT_SVC { get; set; }
3536

3637
public APP_CONFIG() : base() {
3738
FILEMOVER_POLLING_MS = int.Parse(Core.GetConfigSetting("filemoverpollingms").Result);
3839
LEAVE_SOURCE_FILE_PROPERTY_VALUE = Core.GetConfigSetting("leavesourcefilepropertyvalue").Result;
3940
ALL_XFER_DIR = Path.Combine(Core.GetConfigSetting("spacefx_cache").Result, Core.GetConfigSetting("allxferdirectory").Result);
41+
ALLOW_LINKS_TO_DEPLOYMENT_SVC = bool.Parse(Core.GetConfigSetting("allowlinkstodeploymentsvc").Result);
4042
}
4143
}
4244
}

src/Program.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,18 @@ public static void Main(string[] args) {
3535
});
3636
});
3737
app.Run();
38+
39+
// Add a middleware to catch exceptions and stop the host gracefully
40+
app.Use(async (context, next) => {
41+
try {
42+
await next.Invoke();
43+
} catch (Exception ex) {
44+
Console.Error.WriteLine($"Triggering shutdown due to exception caught in global exception handler. Error: {ex.Message}. Stack Trace: {ex.StackTrace}");
45+
46+
// Stop the host gracefully so it triggers the pod to error
47+
var lifetime = context.RequestServices.GetService<IHostApplicationLifetime>();
48+
lifetime?.StopApplication();
49+
}
50+
});
3851
}
3952
}

src/Services/FileMoverService.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) {
4141
continue;
4242
}
4343

44+
if (string.Equals(linkResponse.LinkRequest.DestinationAppId, "platform-deployment", StringComparison.OrdinalIgnoreCase) && !_appConfig.ALLOW_LINKS_TO_DEPLOYMENT_SVC) {
45+
_logger.LogWarning("LinkRequest to deployment service (platform-deployment) is disabled by configuration. Rejecting link request (trackingId: '{trackingId}' / correlationId: '{correlationId}')", linkResponse.ResponseHeader.TrackingId, linkResponse.ResponseHeader.CorrelationId);
46+
linkResponse.ResponseHeader.Message = "LinkRequest to deployment service (platform-deployment) is disabled by configuration.";
47+
linkResponse.ResponseHeader.Status = MessageFormats.Common.StatusCodes.Unauthorized;
48+
await SendResponseToApps(linkResponse);
49+
continue;
50+
}
51+
52+
4453
linkResponse.LinkRequest.DestinationAppId = linkResponse.LinkRequest.DestinationAppId.ToLower();
4554

4655

0 commit comments

Comments
 (0)