Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
99 changes: 99 additions & 0 deletions CERTInext.Tests/ClientPayloadLogRedactionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,5 +289,104 @@ public async Task TrackOrder_TracePayload_FlagOn_IncludesPii()
lines.Single(l => l.Message.StartsWith("TrackOrderAsync response payload")).Message
.Should().Contain(RequestorName).And.Contain(RequestorEmail).And.Contain(EmailSan);
}


// ---------------------------------------------------------------------------
// V2 PlaceOrderV2Async: OV organization representative in the Trace dumps and failure log
// ---------------------------------------------------------------------------

private const string RepName = "Rita Representative";
private const string RepEmail = "rita.rep@example.com";
private const string MaskedRepEmail = "r***@example.com";
private const string RepEmployeeId = "EMP-77231";

private CERTInextClient BuildV2Client(bool logSensitiveRequestData)
{
_server.Given(Request.Create().WithPath("/oauth/token").UsingPost())
.RespondWith(Response.Create().WithStatusCode(200)
.WithHeader("Content-Type", "application/json").WithBody(MockCertificateData.V2TokenResponseJson()));
return new CERTInextClient(new CERTInextConfig
{
ApiUrl = _server.Urls[0], UseV2Api = true, OAuthClientId = "v2-client", OAuthClientSecret = "v2-secret",
AccountNumber = "9988776655", LogSensitiveRequestData = logSensitiveRequestData
});
}

private static Keyfactor.Extensions.CAPlugin.CERTInext.API.V2.V2CreateSslOrderRequest BuildOvOrderWithRepresentative(string domain) =>
new Keyfactor.Extensions.CAPlugin.CERTInext.API.V2.V2CreateSslOrderRequest
{
ProductVariant = "ov",
Organization = new Keyfactor.Extensions.CAPlugin.CERTInext.API.V2.V2OrganizationParams
{
OrganizationNumber = "1234567",
UseNewOrgRepresentative = true,
OrganizationRepresentative = new Keyfactor.Extensions.CAPlugin.CERTInext.API.V2.V2OrganizationRepresentative
{
Name = RepName, Email = RepEmail, Phone = "+15550100199", Designation = "CSO", EmployeeId = RepEmployeeId
}
},
Certificate = new Keyfactor.Extensions.CAPlugin.CERTInext.API.V2.V2CertificateParams { Domain = domain }
};

private void StubV2Create(int status, string body) =>
_server.Given(Request.Create().WithPath("/api/certinext/v2/ssl-certificates").UsingPost())
.RespondWith(Response.Create().WithStatusCode(status)
.WithHeader("Content-Type", status >= 400 ? "application/problem+json" : "application/json").WithBody(body));

private static string PendingRepresentativeResponse(string domain) =>
"{\"orderId\":\"1234567890\",\"status\":\"pending-organization-verification\",\"domain\":\"" + domain + "\"," +
"\"organizationRepresentativeStatus\":\"pending-approval\"," +
"\"pendingOrganizationRepresentative\":{\"name\":\"" + RepName + "\",\"email\":\"" + RepEmail + "\"," +
"\"employeeId\":\"" + RepEmployeeId + "\"}}";

[Fact]
public async Task V2PlaceOrder_TraceDumps_FlagOff_RedactRepresentative()
{
string domain = "rep-" + Guid.NewGuid().ToString("N") + ".example.com";
StubV2Create(201, PendingRepresentativeResponse(domain));
using var client = BuildV2Client(logSensitiveRequestData: false);

var lines = await CaptureAsync(domain, () => client.PlaceOrderV2Async(
Constants.ApiV2.FamilySsl, null, BuildOvOrderWithRepresentative(domain)));

lines.Should().Contain(l => l.Message.StartsWith("PlaceOrderV2Async request payload"));
lines.Should().Contain(l => l.Message.StartsWith("PlaceOrderV2Async response"));
lines.Should().NotContain(l => l.Message.Contains(RepName) || l.Message.Contains(RepEmail)
|| l.Message.Contains(RepEmployeeId) || l.Message.Contains("5550100199"),
"no client log line may carry the representative's details with the flag off");
lines.Single(l => l.Message.StartsWith("PlaceOrderV2Async request payload")).Message
.Should().Contain(MaskedRepEmail).And.Contain("\"useNewOrgRepresentative\":true");
}

[Fact]
public async Task V2PlaceOrder_TraceDumps_FlagOn_IncludeRepresentative()
{
string domain = "rep-" + Guid.NewGuid().ToString("N") + ".example.com";
StubV2Create(201, PendingRepresentativeResponse(domain));
using var client = BuildV2Client(logSensitiveRequestData: true);

var lines = await CaptureAsync(domain, () => client.PlaceOrderV2Async(
Constants.ApiV2.FamilySsl, null, BuildOvOrderWithRepresentative(domain)));

lines.Single(l => l.Message.StartsWith("PlaceOrderV2Async request payload")).Message
.Should().Contain(RepName).And.Contain(RepEmail).And.Contain(RepEmployeeId);
}

[Fact]
public async Task V2PlaceOrder_Ems1317EchoingEmail_FlagOff_NoUnmaskedEmailLogged()
{
string domain = "rep-" + Guid.NewGuid().ToString("N") + ".example.com";
StubV2Create(422,
"{\"type\":\"about:blank\",\"title\":\"Unprocessable Entity\",\"status\":422," +
"\"detail\":\"[EMS-1317] " + RepEmail + " is already a representative of this organization (" + domain + ")\"}");
using var client = BuildV2Client(logSensitiveRequestData: false);

var lines = await CaptureAsync(domain, () => client.PlaceOrderV2Async(
Constants.ApiV2.FamilySsl, null, BuildOvOrderWithRepresentative(domain)));

lines.Should().Contain(l => l.Message.Contains("EMS-1317"));
lines.Should().NotContain(l => l.Message.Contains(RepEmail),
"an EMS-1317 error that echoes the representative's email must not log it unmasked");
}
}
}
63 changes: 63 additions & 0 deletions CERTInext.Tests/RedactPersonalDataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -889,5 +889,68 @@ public void RedactPersonalData_NonJsonBody_IsReturnedUnchanged()
string input = "additionalDomains=alice@example.com&x=1";
CERTInextClient.RedactPersonalData(input).Should().Be(input);
}

// ---------------------------------------------------------------------------
// V2 OV organization representative (create body, create response)
// ---------------------------------------------------------------------------

private const string RepName = "Rita Representative";
private const string RepEmail = "rita.rep@example.com";
private const string RepPhone = "+1 (555) 010-0199";
private const string RepDesignation = "Chief Security Officer";
private const string RepEmployeeId = "EMP-77231";

[Fact]
public void RedactPersonalData_V2OrganizationRepresentativeBlock_RedactsEveryField()
{
string body = System.Text.Json.JsonSerializer.Serialize(new Keyfactor.Extensions.CAPlugin.CERTInext.API.V2.V2CreateSslOrderRequest
{
ProductVariant = "ov",
Organization = new Keyfactor.Extensions.CAPlugin.CERTInext.API.V2.V2OrganizationParams
{
OrganizationNumber = "1234567",
UseNewOrgRepresentative = true,
OrganizationRepresentative = new Keyfactor.Extensions.CAPlugin.CERTInext.API.V2.V2OrganizationRepresentative
{
Name = RepName, Email = RepEmail, Phone = RepPhone, Designation = RepDesignation, EmployeeId = RepEmployeeId
}
},
Certificate = new Keyfactor.Extensions.CAPlugin.CERTInext.API.V2.V2CertificateParams { Domain = "example.com" }
});

string output = CERTInextClient.RedactPersonalData(body);

output.Should().NotContain(RepName).And.NotContain(RepEmail).And.NotContain(RepDesignation)
.And.NotContain(RepEmployeeId).And.NotContain("010-0199");
output.Should().Contain("\"email\":\"r***@example.com\"");
output.Should().Contain("\"employeeId\":\"***REDACTED***\"");
output.Should().Contain("\"useNewOrgRepresentative\":true").And.Contain("\"organizationNumber\":\"1234567\"");
}

[Fact]
public void RedactPersonalData_V2CreateResponse_PendingRepresentative_RedactsEveryField()
{
// The pending block's shape is undocumented; any of these key spellings must be caught.
string body =
"{\"orderId\":\"1234567890\",\"status\":\"pending-organization-verification\"," +
"\"organizationRepresentativeStatus\":\"pending-approval\"," +
"\"pendingOrganizationRepresentative\":{\"name\":\"" + RepName + "\",\"email\":\"" + RepEmail + "\"," +
"\"phone\":\"" + RepPhone + "\",\"designation\":\"" + RepDesignation + "\",\"employeeId\":\"" + RepEmployeeId + "\"}}";

string output = CERTInextClient.RedactPersonalData(body);

output.Should().NotContain(RepName).And.NotContain(RepEmail).And.NotContain(RepDesignation)
.And.NotContain(RepEmployeeId).And.NotContain("010-0199");
output.Should().Contain("\"organizationRepresentativeStatus\":\"pending-approval\"");
}

[Fact]
public void RedactPersonalData_RepresentativeBlockAsString_IsRedactedWhole()
{
string output = CERTInextClient.RedactPersonalData(
"{\"pendingOrganizationRepresentative\":\"" + RepName + " <" + RepEmail + ">\"}");

output.Should().Be("{\"pendingOrganizationRepresentative\":\"***REDACTED***\"}");
}
}
}
Loading