Skip to content

Commit

Permalink
v0.14.2: Update all refs (#1)
Browse files Browse the repository at this point in the history
* Exponential backoff + Cosmos DB serializer

* Update libs and use new lang features
  • Loading branch information
evanverneyfink authored Aug 23, 2022
1 parent 48bfe95 commit e9a059c
Show file tree
Hide file tree
Showing 426 changed files with 8,417 additions and 8,884 deletions.
125 changes: 62 additions & 63 deletions AWS/Mcma.Api.Aws.ApiGateway/ApiGatewayApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,82 +8,81 @@
using Mcma.Api.Http;
using Mcma.Logging;

namespace Mcma.Api.Aws.ApiGateway
namespace Mcma.Api.Aws.ApiGateway;

public class ApiGatewayApiController : IApiGatewayApiController
{
public class ApiGatewayApiController : IApiGatewayApiController
public ApiGatewayApiController(ILoggerProvider loggerProvider, IMcmaApiController controller)
{
public ApiGatewayApiController(ILoggerProvider loggerProvider, IMcmaApiController controller)
{
LoggerProvider = loggerProvider ?? throw new ArgumentNullException(nameof(loggerProvider));
Controller = controller ?? throw new ArgumentNullException(nameof(controller));
}
LoggerProvider = loggerProvider ?? throw new ArgumentNullException(nameof(loggerProvider));
Controller = controller ?? throw new ArgumentNullException(nameof(controller));
}

private ILoggerProvider LoggerProvider { get; }
private ILoggerProvider LoggerProvider { get; }

private IMcmaApiController Controller { get; }
private IMcmaApiController Controller { get; }

public async Task<APIGatewayHttpApiV2ProxyResponse> HandleRequestAsync(APIGatewayHttpApiV2ProxyRequest request, ILambdaContext context)
{
var requestContext =
new McmaApiRequestContext(LoggerProvider,
new McmaApiRequest
{
Id = context.AwsRequestId,
Path = request.RequestContext.Http.Path.Substring(request.RequestContext.Stage.Length + 1),
HttpMethod = new HttpMethod(request.RequestContext.Http.Method),
Headers = request.Headers,
PathVariables = new Dictionary<string, object>(),
QueryStringParameters = request.QueryStringParameters ?? new Dictionary<string, string>(),
Body = !string.IsNullOrWhiteSpace(request.Body) ? Encoding.UTF8.GetBytes(request.Body) : null
});
public async Task<APIGatewayHttpApiV2ProxyResponse> HandleRequestAsync(APIGatewayHttpApiV2ProxyRequest request, ILambdaContext context)
{
var requestContext =
new McmaApiRequestContext(LoggerProvider,
new McmaApiRequest
{
Id = context.AwsRequestId,
Path = request.RequestContext.Http.Path.Substring(request.RequestContext.Stage.Length + 1),
HttpMethod = new HttpMethod(request.RequestContext.Http.Method),
Headers = request.Headers,
PathVariables = new Dictionary<string, object>(),
QueryStringParameters = request.QueryStringParameters ?? new Dictionary<string, string>(),
Body = !string.IsNullOrWhiteSpace(request.Body) ? Encoding.UTF8.GetBytes(request.Body) : null
});

await Controller.HandleRequestAsync(requestContext);
await Controller.HandleRequestAsync(requestContext);

var responseBodyString =
requestContext.Response.JsonBody?.ToString() ??
(requestContext.Response.Body != null ? Convert.ToBase64String(requestContext.Response.Body) : null);
var responseBodyString =
requestContext.Response.JsonBody?.ToString() ??
(requestContext.Response.Body != null ? Convert.ToBase64String(requestContext.Response.Body) : null);

var isBase64Encoded = responseBodyString != null && requestContext.Response.JsonBody == null;
var isBase64Encoded = responseBodyString != null && requestContext.Response.JsonBody == null;

return new APIGatewayHttpApiV2ProxyResponse
{
StatusCode = requestContext.Response.StatusCode,
Headers = requestContext.Response.Headers,
Body = responseBodyString,
IsBase64Encoded = isBase64Encoded
};
}

public async Task<APIGatewayProxyResponse> HandleRequestAsync(APIGatewayProxyRequest request, ILambdaContext context)
return new APIGatewayHttpApiV2ProxyResponse
{
var requestContext =
new McmaApiRequestContext(LoggerProvider,
new McmaApiRequest
{
Id = context.AwsRequestId,
Path = request.Path,
HttpMethod = new HttpMethod(request.HttpMethod),
Headers = request.Headers,
PathVariables = new Dictionary<string, object>(),
QueryStringParameters = request.QueryStringParameters ?? new Dictionary<string, string>(),
Body = !string.IsNullOrWhiteSpace(request.Body) ? Encoding.UTF8.GetBytes(request.Body) : null
});
StatusCode = requestContext.Response.StatusCode,
Headers = requestContext.Response.Headers,
Body = responseBodyString,
IsBase64Encoded = isBase64Encoded
};
}

public async Task<APIGatewayProxyResponse> HandleRequestAsync(APIGatewayProxyRequest request, ILambdaContext context)
{
var requestContext =
new McmaApiRequestContext(LoggerProvider,
new McmaApiRequest
{
Id = context.AwsRequestId,
Path = request.Path,
HttpMethod = new HttpMethod(request.HttpMethod),
Headers = request.Headers,
PathVariables = new Dictionary<string, object>(),
QueryStringParameters = request.QueryStringParameters ?? new Dictionary<string, string>(),
Body = !string.IsNullOrWhiteSpace(request.Body) ? Encoding.UTF8.GetBytes(request.Body) : null
});

await Controller.HandleRequestAsync(requestContext);
await Controller.HandleRequestAsync(requestContext);

var responseBodyString =
requestContext.Response.JsonBody?.ToString() ??
(requestContext.Response.Body != null ? Convert.ToBase64String(requestContext.Response.Body) : null);
var responseBodyString =
requestContext.Response.JsonBody?.ToString() ??
(requestContext.Response.Body != null ? Convert.ToBase64String(requestContext.Response.Body) : null);

var isBase64Encoded = responseBodyString != null && requestContext.Response.JsonBody == null;
var isBase64Encoded = responseBodyString != null && requestContext.Response.JsonBody == null;

return new APIGatewayProxyResponse
{
StatusCode = requestContext.Response.StatusCode,
Headers = requestContext.Response.Headers,
Body = responseBodyString,
IsBase64Encoded = isBase64Encoded
};
}
return new APIGatewayProxyResponse
{
StatusCode = requestContext.Response.StatusCode,
Headers = requestContext.Response.Headers,
Body = responseBodyString,
IsBase64Encoded = isBase64Encoded
};
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using System;
using Mcma.Api;
using Mcma.Api.Http;
using Microsoft.Extensions.DependencyInjection;

namespace Mcma.Api.Aws.ApiGateway
namespace Mcma.Api.Aws.ApiGateway;

public static class ApiGatewayServiceCollectionExtensions
{
public static class ApiGatewayServiceCollectionExtensions
{
public static IServiceCollection AddMcmaApiGatewayApi(this IServiceCollection services, Action<McmaApiBuilder> build)
=> services.AddMcmaApi(build)
.AddSingleton<IApiGatewayApiController, ApiGatewayApiController>();
}
public static IServiceCollection AddMcmaApiGatewayApi(this IServiceCollection services, Action<McmaApiBuilder> build)
=> services.AddMcmaApi(build)
.AddSingleton<IApiGatewayApiController, ApiGatewayApiController>();
}
11 changes: 5 additions & 6 deletions AWS/Mcma.Api.Aws.ApiGateway/IApiGatewayApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.Core;

namespace Mcma.Api.Aws.ApiGateway
namespace Mcma.Api.Aws.ApiGateway;

public interface IApiGatewayApiController
{
public interface IApiGatewayApiController
{
Task<APIGatewayHttpApiV2ProxyResponse> HandleRequestAsync(APIGatewayHttpApiV2ProxyRequest request, ILambdaContext context);
Task<APIGatewayHttpApiV2ProxyResponse> HandleRequestAsync(APIGatewayHttpApiV2ProxyRequest request, ILambdaContext context);

Task<APIGatewayProxyResponse> HandleRequestAsync(APIGatewayProxyRequest request, ILambdaContext context);
}
Task<APIGatewayProxyResponse> HandleRequestAsync(APIGatewayProxyRequest request, ILambdaContext context);
}
8 changes: 2 additions & 6 deletions AWS/Mcma.Api.Aws.ApiGateway/Mcma.Api.Aws.ApiGateway.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,12 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Base\Mcma.Core\Mcma.Core.csproj" />
<ProjectReference Include="..\..\Base\Mcma.Api\Mcma.Api.csproj" />
<ProjectReference Include="..\..\Base\Mcma.Data\Mcma.Data.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.2.0" />
<PackageReference Include="Amazon.Lambda.Core" Version="1.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Routing" Version="2.1.1" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.5.0" />
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
</ItemGroup>

</Project>
83 changes: 41 additions & 42 deletions AWS/Mcma.Client.Aws/Aws4AuthContext.cs
Original file line number Diff line number Diff line change
@@ -1,48 +1,47 @@
using Amazon.Runtime.CredentialManagement;

namespace Mcma.Client.Aws
namespace Mcma.Client.Aws;

public class Aws4AuthContext
{
public class Aws4AuthContext
public Aws4AuthContext()
{
}

public Aws4AuthContext(string accessKey, string secretKey, string region, string sessionToken = null)
{
AccessKey = accessKey;
SecretKey = secretKey;
Region = region;
SessionToken = sessionToken;
}

public string AccessKey { get; set; }

public string SecretKey { get; set; }

public string Region { get; set; }

public string SessionToken { get; set; }

public static Aws4AuthContext Global { get; } =
new(AwsEnvironmentVariables.AccessKey,
AwsEnvironmentVariables.SecretKey,
AwsEnvironmentVariables.Region,
AwsEnvironmentVariables.SessionToken);

public static Aws4AuthContext ForProfile(string profileName)
{
public Aws4AuthContext()
{
}

public Aws4AuthContext(string accessKey, string secretKey, string region, string sessionToken = null)
{
AccessKey = accessKey;
SecretKey = secretKey;
Region = region;
SessionToken = sessionToken;
}

public string AccessKey { get; set; }

public string SecretKey { get; set; }

public string Region { get; set; }

public string SessionToken { get; set; }

public static Aws4AuthContext Global { get; } =
new(AwsEnvironmentVariables.AccessKey,
AwsEnvironmentVariables.SecretKey,
AwsEnvironmentVariables.Region,
AwsEnvironmentVariables.SessionToken);

public static Aws4AuthContext ForProfile(string profileName)
{
var sharedCredentialsFile = new SharedCredentialsFile();
if (!sharedCredentialsFile.TryGetProfile(profileName, out var profile))
throw new McmaException("AWS profile with name '{profileName}' not found in shared credentials file.");

var awsCredentials = profile.GetAWSCredentials(sharedCredentialsFile);
var credentials = awsCredentials.GetCredentials();

return new Aws4AuthContext(credentials.AccessKey,
credentials.SecretKey,
profile.Region.SystemName,
credentials.UseToken ? credentials.Token : null);
}
var sharedCredentialsFile = new SharedCredentialsFile();
if (!sharedCredentialsFile.TryGetProfile(profileName, out var profile))
throw new McmaException("AWS profile with name '{profileName}' not found in shared credentials file.");

var awsCredentials = profile.GetAWSCredentials(sharedCredentialsFile);
var credentials = awsCredentials.GetCredentials();

return new Aws4AuthContext(credentials.AccessKey,
credentials.SecretKey,
profile.Region.SystemName,
credentials.UseToken ? credentials.Token : null);
}
}
32 changes: 15 additions & 17 deletions AWS/Mcma.Client.Aws/Aws4Authenticator.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Mcma.Client;
using Mcma.Client.Auth;

namespace Mcma.Client.Aws
{
public class Aws4Authenticator : IAuthenticator
namespace Mcma.Client.Aws;

public class Aws4Authenticator : IAuthenticator
{
public Aws4Authenticator(Aws4AuthContext authContext)
{
public Aws4Authenticator(Aws4AuthContext authContext)
{
Signer =
new Aws4Signer(
authContext.AccessKey,
authContext.SecretKey,
authContext.Region,
authContext.SessionToken);
}
Signer =
new Aws4Signer(
authContext.AccessKey,
authContext.SecretKey,
authContext.Region,
authContext.SessionToken);
}

private Aws4Signer Signer { get; }
private Aws4Signer Signer { get; }

public Task AuthenticateAsync(HttpRequestMessage request, CancellationToken cancellationToken = default)
=> Signer.SignAsync(request, cancellationToken: cancellationToken);
}
public Task AuthenticateAsync(HttpRequestMessage request, CancellationToken cancellationToken = default)
=> Signer.SignAsync(request, cancellationToken: cancellationToken);
}
25 changes: 11 additions & 14 deletions AWS/Mcma.Client.Aws/Aws4AuthenticatorFactory.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
using System;
using System.Threading.Tasks;
using Mcma.Client;
using System.Threading.Tasks;
using Mcma.Client.Auth;
using Microsoft.Extensions.Options;

namespace Mcma.Client.Aws
namespace Mcma.Client.Aws;

internal class Aws4AuthenticatorFactory : AuthenticatorFactory<Aws4AuthContext>
{
internal class Aws4AuthenticatorFactory : AuthenticatorFactory<Aws4AuthContext>
public Aws4AuthenticatorFactory(IOptions<Aws4AuthenticatorFactoryOptions> options)
{
public Aws4AuthenticatorFactory(IOptions<Aws4AuthenticatorFactoryOptions> options)
{
Options = options.Value ?? new Aws4AuthenticatorFactoryOptions();
}
Options = options.Value ?? new Aws4AuthenticatorFactoryOptions();
}

private Aws4AuthenticatorFactoryOptions Options { get; }
private Aws4AuthenticatorFactoryOptions Options { get; }

protected override Aws4AuthContext DefaultAuthContext => Options.DefaultAuthContext;
protected override Aws4AuthContext DefaultAuthContext => Options.DefaultAuthContext;

protected override Task<IAuthenticator> GetAsync(Aws4AuthContext authContext)
=> Task.FromResult<IAuthenticator>(new Aws4Authenticator(authContext));
}
protected override Task<IAuthenticator> GetAsync(Aws4AuthContext authContext)
=> Task.FromResult<IAuthenticator>(new Aws4Authenticator(authContext));
}
9 changes: 4 additions & 5 deletions AWS/Mcma.Client.Aws/Aws4AuthenticatorFactoryOptions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace Mcma.Client.Aws
namespace Mcma.Client.Aws;

public class Aws4AuthenticatorFactoryOptions
{
public class Aws4AuthenticatorFactoryOptions
{
public Aws4AuthContext DefaultAuthContext { get; set; } = Aws4AuthContext.Global;
}
public Aws4AuthContext DefaultAuthContext { get; set; } = Aws4AuthContext.Global;
}
Loading

0 comments on commit e9a059c

Please sign in to comment.