-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Handle authenticate certificate policy in emulator (#61)
- Loading branch information
Showing
6 changed files
with
165 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System.Security.Cryptography.X509Certificates; | ||
|
||
namespace Azure.ApiManagement.PolicyToolkit.Testing.Emulator.Data; | ||
|
||
public class CertificateStore | ||
{ | ||
internal readonly Dictionary<string, X509Certificate2> ById = new(); | ||
internal readonly Dictionary<string, X509Certificate2> ByThumbprint = new(); | ||
|
||
public CertificateStore WithCertificateById(string id, X509Certificate2 certificate) | ||
{ | ||
ById.Add(id, certificate); | ||
return this; | ||
} | ||
|
||
public CertificateStore WithCertificateByThumbprint(string thumbprint, X509Certificate2 certificate) | ||
{ | ||
ByThumbprint.Add(thumbprint, certificate); | ||
return this; | ||
} | ||
} |
34 changes: 33 additions & 1 deletion
34
src/Testing/Emulator/Policies/AuthenticationCertificateHandler.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 |
---|---|---|
@@ -1,17 +1,49 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System.Security.Cryptography.X509Certificates; | ||
|
||
using Azure.ApiManagement.PolicyToolkit.Authoring; | ||
|
||
namespace Azure.ApiManagement.PolicyToolkit.Testing.Emulator.Policies; | ||
|
||
[Section(nameof(IInboundContext))] | ||
internal class AuthenticationCertificateHandler : PolicyHandler<CertificateAuthenticationConfig> | ||
{ | ||
public List<Tuple< | ||
Func<GatewayContext, CertificateAuthenticationConfig, bool>, | ||
X509Certificate2 | ||
>> CertificateSetup { get; } = new(); | ||
|
||
public override string PolicyName => nameof(IInboundContext.AuthenticationCertificate); | ||
|
||
protected override void Handle(GatewayContext context, CertificateAuthenticationConfig config) | ||
{ | ||
throw new NotImplementedException(); | ||
var certificateFromCallback = CertificateSetup.Find(tuple => tuple.Item1(context, config))?.Item2; | ||
if (certificateFromCallback is not null) | ||
{ | ||
context.Request.Certificate = certificateFromCallback; | ||
return; | ||
} | ||
|
||
var certificateStore = context.CertificateStore; | ||
|
||
if (!string.IsNullOrWhiteSpace(config.Thumbprint)) | ||
{ | ||
context.Request.Certificate = certificateStore.ByThumbprint.GetValueOrDefault(config.Thumbprint); | ||
} | ||
else if (!string.IsNullOrWhiteSpace(config.CertificateId)) | ||
{ | ||
context.Request.Certificate = certificateStore.ById.GetValueOrDefault(config.CertificateId); | ||
} | ||
else if (config.Body is not null) | ||
{ | ||
context.Request.Certificate = new X509Certificate2(config.Body, config.Password); | ||
} | ||
else | ||
{ | ||
throw new InvalidOperationException( | ||
"AuthenticationCertificatePolicy doesn't have certificate source defined"); | ||
} | ||
} | ||
} |
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