-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add server mode framework to be used for IDE integration
- Loading branch information
Showing
40 changed files
with
2,649 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using AWS.Deploy.CLI.ServerMode; | ||
using Microsoft.AspNetCore.Hosting; | ||
|
||
namespace AWS.Deploy.CLI.Commands | ||
{ | ||
public class ServerModeCommand | ||
{ | ||
private readonly IToolInteractiveService _interactiveService; | ||
private readonly int _port; | ||
private readonly int? _parentPid; | ||
|
||
public ServerModeCommand(IToolInteractiveService interactiveService, int port, int? parentPid) | ||
{ | ||
_interactiveService = interactiveService; | ||
_port = port; | ||
_parentPid = parentPid; | ||
} | ||
|
||
public async Task ExecuteAsync(CancellationToken cancellationToken = default(CancellationToken)) | ||
{ | ||
var url = $"http://localhost:{_port}"; | ||
|
||
var builder = new WebHostBuilder() | ||
.UseKestrel() | ||
.UseUrls(url) | ||
.UseStartup<Startup>(); | ||
|
||
var host = builder.Build(); | ||
|
||
await host.RunAsync(cancellationToken); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/AWS.Deploy.CLI/ServerMode/AwsCredentialsAuthenticationHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Security.Claims; | ||
using System.Text; | ||
using System.Text.Encodings.Web; | ||
using System.Threading.Tasks; | ||
|
||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using Newtonsoft.Json; | ||
|
||
namespace AWS.Deploy.CLI.ServerMode | ||
{ | ||
public class AwsCredentialsAuthenticationSchemeOptions | ||
: AuthenticationSchemeOptions | ||
{ } | ||
|
||
/// <summary> | ||
/// The ASP.NET Core Authentication handler. Verify the Authorization header has been correctly set with AWS Credentials. | ||
/// </summary> | ||
public class AwsCredentialsAuthenticationHandler : AuthenticationHandler<AwsCredentialsAuthenticationSchemeOptions> | ||
{ | ||
public const string SchemaName = "aws-deploy-tool-server-mode"; | ||
|
||
public const string ClaimAwsAccessKeyId = "awsAccessKeyId"; | ||
public const string ClaimAwsSecretKey = "awsSecretKey"; | ||
public const string ClaimAwsSessionToken = "awsSessionToken"; | ||
|
||
public AwsCredentialsAuthenticationHandler( | ||
IOptionsMonitor<AwsCredentialsAuthenticationSchemeOptions> options, | ||
ILoggerFactory logger, | ||
UrlEncoder encoder, | ||
ISystemClock clock) | ||
: base(options, logger, encoder, clock) | ||
{ | ||
} | ||
|
||
protected override Task<AuthenticateResult> HandleAuthenticateAsync() | ||
{ | ||
if (!Request.Headers.TryGetValue("Authorization", out var value)) | ||
{ | ||
return Task.FromResult(AuthenticateResult.Fail("Missing Authorization header")); | ||
} | ||
|
||
return Task.FromResult(ProcessAuthorizationHeader(value)); | ||
} | ||
|
||
public static AuthenticateResult ProcessAuthorizationHeader(string authorizationHeaderValue) | ||
{ | ||
var tokens = authorizationHeaderValue.Split(' '); | ||
if (tokens.Length != 2) | ||
{ | ||
return AuthenticateResult.Fail($"Incorrect format Authorization header. Format should be \"{SchemaName} <base-64-auth-parameters>\""); | ||
} | ||
if (!string.Equals(SchemaName, tokens[0])) | ||
{ | ||
return AuthenticateResult.Fail($"Unsupported authorization schema. Supported schema: {SchemaName}"); | ||
} | ||
|
||
try | ||
{ | ||
var base64Bytes = Convert.FromBase64String(tokens[1]); | ||
var json = Encoding.UTF8.GetString(base64Bytes); | ||
|
||
var authParameters = JsonConvert.DeserializeObject<Dictionary<string, string>>(json); | ||
|
||
var claimIdentity = new ClaimsIdentity(nameof(AwsCredentialsAuthenticationHandler)); | ||
foreach (var kvp in authParameters) | ||
{ | ||
claimIdentity.AddClaim(new Claim(kvp.Key, kvp.Value)); | ||
} | ||
|
||
var ticket = new AuthenticationTicket( | ||
new ClaimsPrincipal(claimIdentity), SchemaName); | ||
|
||
return AuthenticateResult.Success(ticket); | ||
} | ||
catch (Exception) | ||
{ | ||
return AuthenticateResult.Fail("Error decoding authorization value"); | ||
} | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.