Skip to content
Open
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
27 changes: 25 additions & 2 deletions CERTInext.Tests/CERTInextCAPluginTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,10 @@ public void GetProductIds_ReturnsStaticProductList()
ids.Should().Contain(Constants.Products.DvSsl);
ids.Should().Contain(Constants.Products.OvSsl);
ids.Should().Contain(Constants.Products.EvSsl);
// Ten products total (DV/OV/EV × single/wildcard/UCC variants)
ids.Should().HaveCount(10);
ids.Should().Contain(Constants.Products.PrivatePkiIntranetSsl);
ids.Should().Contain(Constants.Products.PrivatePkiIgtfHost);
// Ten SSL products (DV/OV/EV × single/wildcard/UCC variants) plus the two V2 Private PKI products
ids.Should().HaveCount(12);
mock.VerifyNoOtherCalls();
}

Expand Down Expand Up @@ -794,6 +796,27 @@ await act.Should().ThrowAsync<Exception>()
.WithMessage("*ProfileId*required*");
}

[Theory]
[InlineData(Constants.Products.PrivatePkiIntranetSsl)]
[InlineData(Constants.Products.PrivatePkiIgtfHost)]
public async Task Enroll_V1_PrivatePkiProduct_WithoutProductCode_Throws(string productId)
{
// The Private PKI products have no V1 default code, so a V1 template must set ProductCode.
var mock = NewMock();
var plugin = BuildPlugin(mock.Object);

Func<Task> act = () => plugin.Enroll(
csr: MockCertificateData.FakeCsrPem,
subject: "CN=test.example.com",
san: null,
productInfo: new EnrollmentProductInfo { ProductID = productId, ProductParameters = new Dictionary<string, string>() },
requestFormat: RequestFormat.PKCS10,
enrollmentType: EnrollmentType.New);

await act.Should().ThrowAsync<Exception>().WithMessage("*ProfileId*required*");
mock.Invocations.Should().BeEmpty();
}

[Fact]
public async Task Enroll_New_ThreadsTemplateSignerParamsOntoRequest()
{
Expand Down
45 changes: 45 additions & 0 deletions CERTInext.Tests/EnrollmentParamsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,50 @@ public void HasExplicitProductVariant_DistinguishesUnsetFromExplicit(string conf
ep.HasExplicitProductVariant.Should().Be(expectedExplicit);
ep.ProductVariant.Should().Be(expectedVariant, "the ProductVariant getter's own default is unchanged");
}

// A Private PKI product sets the family and variant defaults; SSL products keep ssl/dv.
[Theory]
[InlineData(Constants.Products.PrivatePkiIntranetSsl, Constants.ApiV2.FamilyPrivatePki, "intranet-ssl", true)]
[InlineData(Constants.Products.PrivatePkiIgtfHost, Constants.ApiV2.FamilyPrivatePki, "igtf-host", true)]
[InlineData("private pki intranet ssl", Constants.ApiV2.FamilyPrivatePki, "intranet-ssl", true)]
[InlineData(Constants.Products.OvSsl, Constants.ApiV2.FamilySsl, "dv", false)]
[InlineData("", Constants.ApiV2.FamilySsl, "dv", false)]
public void ProductFamilyAndVariantDefaults_FollowTheProduct(
string productId, string expectedFamilySlug, string expectedVariant, bool expectedPrivatePki)
{
var ep = new EnrollmentParams(MakeProductInfo(productId));

ep.IsPrivatePkiProduct.Should().Be(expectedPrivatePki);
ep.ProductFamilySlug.Should().Be(expectedFamilySlug);
ep.ProductVariant.Should().Be(expectedVariant);
ep.HasExplicitProductVariant.Should().BeFalse();
}

[Fact]
public void ExplicitFamilyAndVariant_OverrideThePrivatePkiProductDefaults()
{
var ep = new EnrollmentParams(MakeProductInfo(Constants.Products.PrivatePkiIntranetSsl, new Dictionary<string, string>
{
[Constants.EnrollmentParam.ProductFamily] = "ssl",
[Constants.EnrollmentParam.ProductVariant] = "igtf-host"
}));

ep.ProductFamilySlug.Should().Be(Constants.ApiV2.FamilySsl);
ep.ProductVariant.Should().Be("igtf-host");
}

[Fact]
public void ResolveSslProductVariant_RejectsAPrivatePkiProduct()
{
var ep = new EnrollmentParams(MakeProductInfo(Constants.Products.PrivatePkiIgtfHost, new Dictionary<string, string>
{
[Constants.EnrollmentParam.ProductFamily] = "ssl"
}));

string error = CERTInextCAPlugin.ResolveSslProductVariant(ep, out string resolved);

error.Should().Contain("Private PKI product").And.Contain("'ssl'");
resolved.Should().BeNull();
}
}
}
5 changes: 4 additions & 1 deletion CERTInext.Tests/SyncProductIdTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,14 @@ public void ProductNameForTypeId_MapsPluginProductTypes(string typeId, string ex
}

[Fact]
public void EveryAdvertisedProductHasATypeId_AndMapsBackToItself()
public void EveryAdvertisedSslProductHasATypeId_AndMapsBackToItself()
{
// The Private PKI products have no known catalog productTypeID, so sync can't name them.
var plugin = new CERTInextCAPlugin();
foreach (string name in plugin.GetProductIds())
{
if (Constants.Products.PrivatePkiVariantsV2.ContainsKey(name))
continue;
Constants.Products.ProductTypeIdsV2.Should().ContainKey(name);
CERTInextCAPlugin.ProductNameForTypeId(Constants.Products.ProductTypeIdsV2[name]).Should().Be(name);
Constants.Products.SslComProductTypeIdsV2.Should().ContainKey(name);
Expand Down
104 changes: 104 additions & 0 deletions CERTInext.Tests/V2PrivatePkiEnrollmentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -620,5 +620,109 @@ public void ValidatePrivatePkiEnrollmentParams_UnsetVariant_SaysNotSetRatherThan
error.Should().Contain("not set");
normalized.Should().BeNull();
}

// ---------------------------------------------------------------------------
// Private PKI product names: the product sets ProductFamily and ProductVariant
// ---------------------------------------------------------------------------

private static EnrollmentProductInfo MakePrivatePkiNamedProductInfo(
string productId, string family = null, string variant = null)
{
var parameters = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
["DomainName"] = Hostname
};
if (family != null) parameters["ProductFamily"] = family;
if (variant != null) parameters["ProductVariant"] = variant;
return new EnrollmentProductInfo { ProductID = productId, ProductParameters = parameters };
}

[Theory]
[InlineData(Constants.Products.PrivatePkiIntranetSsl, "intranet-ssl")]
[InlineData(Constants.Products.PrivatePkiIgtfHost, "igtf-host")]
public async Task Enroll_V2_PrivatePkiProduct_PlacesPrivatePkiOrder_WithNoFamilyOrVariantParams(
string productId, string expectedVariant)
{
var mock = NewMock();
var captured = StubPrivatePkiOrder(mock);

var result = await EnrollAsync(BuildV2Plugin(mock.Object), MakePrivatePkiNamedProductInfo(productId),
new Dictionary<string, string[]> { ["dnsname"] = new[] { Hostname } });

result.CARequestID.Should().Be(OrderId);
captured().Should().NotBeNull("the product name alone must route the order to the Private PKI endpoint");
captured().Variant.Should().Be(expectedVariant);
mock.Verify(c => c.PlaceOrderV2Async(
It.IsAny<string>(), It.IsAny<string>(), It.IsAny<V2CreateSslOrderRequest>(), It.IsAny<CancellationToken>()),
Times.Never);
}

[Fact]
public async Task Enroll_V2_PrivatePkiProduct_MatchingExplicitParams_StillPlacesOrder()
{
var mock = NewMock();
var captured = StubPrivatePkiOrder(mock);

await EnrollAsync(BuildV2Plugin(mock.Object),
MakePrivatePkiNamedProductInfo(Constants.Products.PrivatePkiIgtfHost, family: "private-pki", variant: "IGTF-HOST"),
new Dictionary<string, string[]> { ["dnsname"] = new[] { Hostname } });

captured().Variant.Should().Be("igtf-host");
}

[Fact]
public async Task Enroll_V2_PrivatePkiProduct_ContradictingVariant_FailsFastWithoutAnyCaCall()
{
var mock = NewMock();

var result = await EnrollAsync(BuildV2Plugin(mock.Object),
MakePrivatePkiNamedProductInfo(Constants.Products.PrivatePkiIntranetSsl, variant: "igtf-host"),
new Dictionary<string, string[]> { ["dnsname"] = new[] { Hostname } });

result.Status.Should().Be((int)EndEntityStatus.FAILED);
result.StatusMessage.Should().Contain("ProductVariant").And.Contain("'intranet-ssl'");
mock.Invocations.Should().BeEmpty("validation must happen before any CA call");
}

[Theory]
[InlineData("ssl")]
[InlineData("unknown-family")] // unrecognized families are treated as ssl
public async Task Enroll_V2_PrivatePkiProduct_NonPrivatePkiFamily_FailsFastWithoutAnyCaCall(string family)
{
var mock = NewMock();

var result = await EnrollAsync(BuildV2Plugin(mock.Object),
MakePrivatePkiNamedProductInfo(Constants.Products.PrivatePkiIntranetSsl, family: family),
new Dictionary<string, string[]> { ["dnsname"] = new[] { Hostname } });

result.Status.Should().Be((int)EndEntityStatus.FAILED);
result.StatusMessage.Should().Contain("Private PKI product").And.Contain("ProductFamily");
mock.Invocations.Should().BeEmpty("an SSL order must not be placed for a Private PKI product");
}

[Fact]
public async Task ValidateProductInfo_V2_PrivatePkiProduct_WithSslFamily_IsRejectedAtSave()
{
using var server = WireMockServer.Start();
StubToken(server);

Func<Task> act = () => BuildV2Plugin(NewMock().Object).ValidateProductInfo(
MakePrivatePkiNamedProductInfo(Constants.Products.PrivatePkiIgtfHost, family: "ssl"),
V2ConnectionInfo(server.Urls[0]));

await act.Should().ThrowAsync<AnyCAValidationException>().WithMessage("*Private PKI product*ProductFamily*");
}

[Fact]
public async Task ValidateProductInfo_V2_PrivatePkiProduct_WithNoParams_Succeeds()
{
using var server = WireMockServer.Start();
StubToken(server);

Func<Task> act = () => BuildV2Plugin(NewMock().Object).ValidateProductInfo(
MakePrivatePkiNamedProductInfo(Constants.Products.PrivatePkiIntranetSsl), V2ConnectionInfo(server.Urls[0]));

await act.Should().NotThrowAsync();
}
}
}
32 changes: 28 additions & 4 deletions CERTInext/CERTInextCAPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -425,9 +425,10 @@ public List<string> GetProductIds()
// on GetProductDetailsAsync would risk deadlock in certain synchronization contexts.
// 2. The Keyfactor integration-manifest doc tool requires a known list at reflection
// time (a live API call at that point returned empty results).
// 3. CERTInext product names are stable; operators select the correct product and
// then provide the numeric ProductCode template parameter to map it to the actual
// CERTInext API code for their account (sandbox vs. production).
// 3. The names are the plugin's own, not CERTInext's. On V2 the name sets the variant
// (and, for the Private PKI names, the family), and CERTInext picks the product unless
// the template sets a ProductCode. On V1 the name maps to a default production code
// (DefaultProductCodes), which a template's ProductCode overrides.
return new List<string>
{
Constants.Products.DvSsl,
Expand All @@ -440,6 +441,8 @@ public List<string> GetProductIds()
Constants.Products.OvSslWildcardUcc,
Constants.Products.EvSsl,
Constants.Products.EvSslUcc,
Constants.Products.PrivatePkiIntranetSsl,
Constants.Products.PrivatePkiIgtfHost,
};
}

Expand Down Expand Up @@ -1040,7 +1043,9 @@ public async Task<EnrollmentResult> Enroll(
LogSanitizer.MaskEmail(LogSanitizer.Strip(ep.RequesterEmail)));
}

if (string.IsNullOrWhiteSpace(ep.ProfileId))
// V1 only: a V2 order needs no product code (CERTInext resolves it when the template sets none),
// and the Private PKI products have no V1 default code to fall back on.
if (!_config.UseV2Api && string.IsNullOrWhiteSpace(ep.ProfileId))
{
_logger.LogError(
"Enrollment rejected — ProfileId parameter is missing. Subject={Subject}, EnrollmentType={EnrollmentType}",
Expand Down Expand Up @@ -1771,6 +1776,15 @@ internal static string ValidatePrivatePkiEnrollmentParams(EnrollmentParams ep, o
$"{Constants.ApiV2.PrivatePkiVariantIgtfHost}. Current value: {configured}.";
}

// A Private PKI product name fixes the variant; an explicit ProductVariant must agree with it.
if (Constants.Products.PrivatePkiVariantsV2.TryGetValue(ep.ProductId ?? string.Empty, out string productVariant)
&& !string.Equals(variant, productVariant, StringComparison.OrdinalIgnoreCase))
{
return $"Template parameter '{Constants.EnrollmentParam.ProductVariant}' is set to '{variant}', but " +
$"the selected product '{ep.ProductId}' is '{productVariant}'. Remove the override, or " +
$"select the product for '{variant}'.";
}

normalizedVariant = variant.ToLowerInvariant();
return null;
}
Expand Down Expand Up @@ -1800,6 +1814,16 @@ internal static string ValidatePrivatePkiEnrollmentParams(EnrollmentParams ep, o
/// </summary>
internal static string ResolveSslProductVariant(EnrollmentParams ep, out string resolvedVariant)
{
// A Private PKI product only reaches the SSL path when the template sets ProductFamily
// to something other than private-pki; ordering it as SSL would buy the wrong product.
if (ep.IsPrivatePkiProduct)
{
resolvedVariant = null;
return $"The selected product '{ep.ProductId}' is a Private PKI product, but template parameter " +
$"'{Constants.EnrollmentParam.ProductFamily}' is set to '{ep.ProductFamily}'. Set it to " +
$"'private-pki' or remove it.";
}

bool hasMapping = Constants.Products.ProductVariantsV2.TryGetValue(
ep.ProductId ?? string.Empty, out string derivedVariant);

Expand Down
25 changes: 14 additions & 11 deletions CERTInext/CERTInextCAPluginConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,8 @@ public static Dictionary<string, PropertyConfigInfo> GetTemplateParameterAnnotat
[Constants.EnrollmentParam.ProductCode] = new PropertyConfigInfo
{
Comments = "OPTIONAL: Override the numeric CERTInext product code for this template. When omitted: " +
"on V1, the default production code for the selected product is used (e.g. DV SSL → 838); " +
"on V1, the default production code for the selected product is used (e.g. DV SSL → 838; " +
"the Private PKI products have none, so on V1 they need this set); " +
"on V2, no X-Product-Code header is sent and CERTInext resolves the product from the " +
"variant, the wildcard shape of the domain and any additional domains. On V2 an explicit " +
"value is an override, sent as X-Product-Code to choose between several matching " +
Expand Down Expand Up @@ -654,22 +655,24 @@ public static Dictionary<string, PropertyConfigInfo> GetTemplateParameterAnnotat

[Constants.EnrollmentParam.ProductFamily] = new PropertyConfigInfo
{
Comments = "V2 ONLY: Product family for this template. Accepted values: 'ssl' (default) or " +
"'private-pki'. 'private-pki' requires a Private PKI ProductVariant. 'signature' " +
"(Document Signer) is accepted by the parameter, but Document " +
"Signer enrollment is not supported.",
Comments = "V2 ONLY: Product family for this template. Accepted values: 'ssl' or 'private-pki'. " +
"If omitted, 'private-pki' for the Private PKI products and 'ssl' for the others; a " +
"Private PKI product with any other value fails enrollment. 'private-pki' with an SSL " +
"product requires a Private PKI ProductVariant. 'signature' (Document Signer) is " +
"accepted by the parameter, but Document Signer enrollment is not supported.",
Hidden = false,
DefaultValue = "ssl",
DefaultValue = string.Empty,
Type = "String"
},
[Constants.EnrollmentParam.ProductVariant] = new PropertyConfigInfo
{
Comments = "V2 ONLY: Product variant sent in the V2 order body. ProductFamily 'ssl': 'dv', 'ov', or " +
"'ev' — if omitted, derived from the selected product; an explicit value that contradicts " +
"the product fails enrollment. ProductFamily 'private-pki': 'intranet-ssl' or 'igtf-host' " +
"(required; no default).",
Comments = "V2 ONLY: Product variant sent in the V2 order body. If omitted, derived from the " +
"selected product ('dv', 'ov' or 'ev' for SSL; 'intranet-ssl' or 'igtf-host' for the " +
"Private PKI products); an explicit value that contradicts the product fails enrollment. " +
"With ProductFamily 'private-pki' and an SSL product it is required: 'intranet-ssl' or " +
"'igtf-host'.",
Hidden = false,
DefaultValue = "dv",
DefaultValue = string.Empty,
Type = "String"
},
[Constants.EnrollmentParam.OrgRepresentativeName] = new PropertyConfigInfo
Expand Down
Loading
Loading