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

Add kudu log in Linux Consumption middleware #66

Merged
merged 2 commits into from
Jul 1, 2019
Merged
Changes from 1 commit
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
using Kudu.Services.Infrastructure.Authentication;
using System;
using System.Text.RegularExpressions;
using Kudu.Core.Tracing;
using Kudu.Core.Infrastructure;
using Kudu.Contracts.Settings;
using Kudu.Services.Infrastructure;
using Microsoft.AspNetCore.Http.Extensions;

namespace Kudu.Services.LinuxConsumptionInstanceAdmin
{
Expand Down Expand Up @@ -57,6 +62,8 @@ public LinuxConsumptionRouteMiddleware(RequestDelegate next)
/// <returns>Response be set to 404 if the route is not whitelisted</returns>
public async Task Invoke(HttpContext context, IAuthorizationService authorizationService = null)
{
DateTime requestTime = DateTime.UtcNow;

// Step 1: if disguise host exists, replace the request header HOST to DISGUISED-HOST
// if disguist host does not exist, check and replace ~1 with regex
if (context.Request.Headers.TryGetValue(DisguisedHostHeader, out StringValues value))
Expand All @@ -78,21 +85,51 @@ public async Task Invoke(HttpContext context, IAuthorizationService authorizatio
if (IsHomePageRoute(context.Request.Path))
{
context.Response.StatusCode = 200;
KuduEventGenerator.Log().ApiEvent(
ServerConfiguration.GetApplicationName(),
"LinuxConsumptionEndpoint",
context.Request.GetEncodedPathAndQuery(),
context.Request.Method,
System.Environment.GetEnvironmentVariable("x-ms-request-id") ?? string.Empty,
context.Response.StatusCode,
(DateTime.UtcNow - requestTime).Milliseconds,
context.Request.GetUserAgent()
);
Hazhzeng marked this conversation as resolved.
Show resolved Hide resolved
return;
}

// Step 3: check if the request endpoint is enabled in Linux Consumption
if (!IsRouteWhitelisted(context.Request.Path))
{
context.Response.StatusCode = 404;
KuduEventGenerator.Log().ApiEvent(
ServerConfiguration.GetApplicationName(),
"BlacklistedLinuxConsumptionEndpoint",
context.Request.GetEncodedPathAndQuery(),
context.Request.Method,
System.Environment.GetEnvironmentVariable("x-ms-request-id") ?? string.Empty,
context.Response.StatusCode,
(DateTime.UtcNow - requestTime).Milliseconds,
context.Request.GetUserAgent()
);
return;
}

// Step 3: check if the request matches authorization policy
// Step 4: check if the request matches authorization policy
AuthenticateResult authenticateResult = await context.AuthenticateAsync(ArmAuthenticationDefaults.AuthenticationScheme);
if (!authenticateResult.Succeeded)
{
context.Response.StatusCode = 401;
KuduEventGenerator.Log().ApiEvent(
ServerConfiguration.GetApplicationName(),
"UnauthenticatedLinuxConsumptionEndpoint",
context.Request.GetEncodedPathAndQuery(),
context.Request.Method,
System.Environment.GetEnvironmentVariable("x-ms-request-id") ?? string.Empty,
context.Response.StatusCode,
(DateTime.UtcNow - requestTime).Milliseconds,
context.Request.GetUserAgent()
);
return;
}

Expand All @@ -102,6 +139,16 @@ public async Task Invoke(HttpContext context, IAuthorizationService authorizatio
if (!authorizeResult.Succeeded)
Hazhzeng marked this conversation as resolved.
Show resolved Hide resolved
{
context.Response.StatusCode = 401;
KuduEventGenerator.Log().ApiEvent(
ServerConfiguration.GetApplicationName(),
"UnauthorizedLinuxConsumptionEndpoint",
context.Request.GetEncodedPathAndQuery(),
context.Request.Method,
System.Environment.GetEnvironmentVariable("x-ms-request-id") ?? string.Empty,
context.Response.StatusCode,
(DateTime.UtcNow - requestTime).Milliseconds,
context.Request.GetUserAgent()
);
return;
}
}
Expand Down