|
| 1 | +// Copyright 2024 Keyfactor |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. |
| 3 | +// At http://www.apache.org/licenses/LICENSE-2.0 |
| 4 | + |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using FluentAssertions; |
| 8 | +using Keyfactor.AnyGateway.Extensions; |
| 9 | +using Keyfactor.PKI.Enums.EJBCA; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +namespace Keyfactor.Extensions.CAPlugin.CERTInext.IntegrationTests |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// Integration tests for the DNS DCV enrollment path. |
| 16 | + /// |
| 17 | + /// DNS validator selection: |
| 18 | + /// • When <c>CERTINEXT_CF_API_TOKEN</c> and <c>CERTINEXT_CF_ZONE_ID</c> are set in |
| 19 | + /// <c>~/.env_certinext</c>, a <see cref="CloudflareDomainValidator"/> is used and |
| 20 | + /// a real TXT record is published and cleaned up around the enrollment. |
| 21 | + /// • Otherwise a <see cref="StubDomainValidator"/> is used. The plugin still |
| 22 | + /// exercises the full DCV orchestration path (Stage → propagation wait → VerifyDcv |
| 23 | + /// → Cleanup), but no real DNS record is published. Whether CERTInext's VerifyDcv |
| 24 | + /// succeeds in this mode depends on the sandbox environment. |
| 25 | + /// |
| 26 | + /// All tests skip when CERTInext credentials are absent (<see cref="IntegrationSkip"/>). |
| 27 | + /// Add the following to <c>~/.env_certinext</c> to run with real DNS: |
| 28 | + /// <code> |
| 29 | + /// CERTINEXT_CF_API_TOKEN=<your Cloudflare API token with DNS:Edit> |
| 30 | + /// CERTINEXT_CF_ZONE_ID=<Cloudflare Zone ID for your test domain> |
| 31 | + /// CERTINEXT_DCV_DOMAIN=<subdomain to use, e.g. dcv-test.example.com> |
| 32 | + /// </code> |
| 33 | + /// </summary> |
| 34 | + public class DcvLifecycleTests : IClassFixture<IntegrationTestFixture> |
| 35 | + { |
| 36 | + private readonly IntegrationTestFixture _fixture; |
| 37 | + |
| 38 | + public DcvLifecycleTests(IntegrationTestFixture fixture) |
| 39 | + { |
| 40 | + _fixture = fixture; |
| 41 | + } |
| 42 | + |
| 43 | + // --------------------------------------------------------------------------- |
| 44 | + // Helpers |
| 45 | + // --------------------------------------------------------------------------- |
| 46 | + |
| 47 | + private IDomainValidatorFactory BuildDnsFactory() => |
| 48 | + _fixture.IsCloudflareConfigured |
| 49 | + ? (IDomainValidatorFactory)new CloudflareDomainValidatorFactory( |
| 50 | + _fixture.CloudflareApiToken, _fixture.CloudflareZoneId) |
| 51 | + : new StubDomainValidatorFactory(); |
| 52 | + |
| 53 | + private CERTInextCAPlugin BuildPlugin(bool dcvEnabled, int propagationDelaySeconds = 5) |
| 54 | + { |
| 55 | + var config = new CERTInextConfig |
| 56 | + { |
| 57 | + ApiUrl = _fixture.Config.ApiUrl, |
| 58 | + AuthMode = _fixture.Config.AuthMode, |
| 59 | + ApiKey = _fixture.Config.ApiKey, |
| 60 | + AccountNumber = _fixture.Config.AccountNumber, |
| 61 | + GroupNumber = _fixture.Config.GroupNumber, |
| 62 | + RequestorName = _fixture.Config.RequestorName, |
| 63 | + RequestorEmail = _fixture.Config.RequestorEmail, |
| 64 | + RequestorIsdCode = _fixture.Config.RequestorIsdCode, |
| 65 | + RequestorMobileNumber = _fixture.Config.RequestorMobileNumber, |
| 66 | + SignerPlace = _fixture.Config.SignerPlace, |
| 67 | + SignerIp = _fixture.Config.SignerIp, |
| 68 | + DefaultProductCode = _fixture.Config.DefaultProductCode, |
| 69 | + PageSize = _fixture.Config.PageSize, |
| 70 | + DcvEnabled = dcvEnabled, |
| 71 | + DcvPropagationDelaySeconds = propagationDelaySeconds, |
| 72 | + DcvTimeoutMinutes = 3 |
| 73 | + }; |
| 74 | + |
| 75 | + return new CERTInextCAPlugin(_fixture.Client, BuildDnsFactory(), config); |
| 76 | + } |
| 77 | + |
| 78 | + // --------------------------------------------------------------------------- |
| 79 | + // Tests |
| 80 | + // --------------------------------------------------------------------------- |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// Enroll with DCV enabled. Uses a real Cloudflare DNS record when CF credentials |
| 84 | + /// are configured, otherwise uses <see cref="StubDomainValidator"/>. |
| 85 | + /// |
| 86 | + /// The test verifies that the plugin completes without throwing. The enrollment |
| 87 | + /// result status depends on whether the CERTInext sandbox auto-issues after DCV. |
| 88 | + /// </summary> |
| 89 | + [SkippableFact] |
| 90 | + public async Task DcvEnroll_CompletesWithoutThrowing() |
| 91 | + { |
| 92 | + IntegrationSkip.IfNotConfigured(_fixture); |
| 93 | + |
| 94 | + var plugin = BuildPlugin(dcvEnabled: true); |
| 95 | + |
| 96 | + var result = await plugin.Enroll( |
| 97 | + csr: IntegrationTestData.FakeCsrPem, |
| 98 | + subject: $"CN={IntegrationTestData.DcvTestDomain}", |
| 99 | + san: new Dictionary<string, string[]> |
| 100 | + { |
| 101 | + ["dns"] = new[] { IntegrationTestData.DcvTestDomain } |
| 102 | + }, |
| 103 | + productInfo: IntegrationTestData.DvSslProductInfo(_fixture.Config.DefaultProductCode), |
| 104 | + requestFormat: RequestFormat.PKCS10, |
| 105 | + enrollmentType: EnrollmentType.New); |
| 106 | + |
| 107 | + result.Should().NotBeNull(); |
| 108 | + |
| 109 | + if (_fixture.IsCloudflareConfigured) |
| 110 | + { |
| 111 | + // With real DNS, CERTInext should be able to verify — assert issuance or pending |
| 112 | + new[] { (int)EndEntityStatus.GENERATED, (int)EndEntityStatus.EXTERNALVALIDATION } |
| 113 | + .Should().Contain(result.Status, |
| 114 | + "enrollment with real DNS DCV should produce a valid terminal or pending status"); |
| 115 | + } |
| 116 | + else |
| 117 | + { |
| 118 | + // Without real DNS the VerifyDcv may fail; we only assert no unhandled exception |
| 119 | + // was thrown (the Enroll method handles the error gracefully). |
| 120 | + result.Should().NotBeNull("enrollment should return a result even when stub DNS is used"); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /// <summary> |
| 125 | + /// Enroll without DCV enabled — verifies the plugin skips the DCV path entirely |
| 126 | + /// and returns a result from the normal enrollment flow. |
| 127 | + /// </summary> |
| 128 | + [SkippableFact] |
| 129 | + public async Task EnrollWithoutDcv_DoesNotInvokeDnsProvider() |
| 130 | + { |
| 131 | + IntegrationSkip.IfNotConfigured(_fixture); |
| 132 | + |
| 133 | + // Use a plugin backed by the real client but DcvEnabled=false |
| 134 | + var plugin = BuildPlugin(dcvEnabled: false); |
| 135 | + |
| 136 | + var result = await plugin.Enroll( |
| 137 | + csr: IntegrationTestData.FakeCsrPem, |
| 138 | + subject: $"CN={IntegrationTestData.DcvTestDomain}", |
| 139 | + san: new Dictionary<string, string[]> |
| 140 | + { |
| 141 | + ["dns"] = new[] { IntegrationTestData.DcvTestDomain } |
| 142 | + }, |
| 143 | + productInfo: IntegrationTestData.DvSslProductInfo(_fixture.Config.DefaultProductCode), |
| 144 | + requestFormat: RequestFormat.PKCS10, |
| 145 | + enrollmentType: EnrollmentType.New); |
| 146 | + |
| 147 | + result.Should().NotBeNull(); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + /// <summary> |
| 152 | + /// Shared test data for DCV integration tests. |
| 153 | + /// </summary> |
| 154 | + internal static class IntegrationTestData |
| 155 | + { |
| 156 | + /// <summary> |
| 157 | + /// Domain used for DCV tests. Override via <c>CERTINEXT_DCV_DOMAIN</c> in |
| 158 | + /// <c>~/.env_certinext</c>. |
| 159 | + /// </summary> |
| 160 | + public static string DcvTestDomain => |
| 161 | + System.Environment.GetEnvironmentVariable("CERTINEXT_DCV_DOMAIN") |
| 162 | + ?? "dcv-test.example.com"; |
| 163 | + |
| 164 | + public const string FakeCsrPem = |
| 165 | + "-----BEGIN CERTIFICATE REQUEST-----\n" + |
| 166 | + "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2a2rwplBQLzHPZe5TNJF\n" + |
| 167 | + "-----END CERTIFICATE REQUEST-----"; |
| 168 | + |
| 169 | + public static EnrollmentProductInfo DvSslProductInfo(string productCode = null) => |
| 170 | + new EnrollmentProductInfo |
| 171 | + { |
| 172 | + ProductID = productCode ?? Constants.Products.DvSsl, |
| 173 | + ProductParameters = new Dictionary<string, string> |
| 174 | + { |
| 175 | + ["ProfileId"] = productCode ?? Constants.Products.DvSsl, |
| 176 | + ["ValidityYears"] = "1" |
| 177 | + } |
| 178 | + }; |
| 179 | + } |
| 180 | +} |
0 commit comments