Skip to content

Commit cf88b79

Browse files
authored
Fixed api validation error (#911)
1 parent 968cffc commit cf88b79

File tree

6 files changed

+33
-18
lines changed

6 files changed

+33
-18
lines changed

KeyVault.Acmebot/Internal/AcmeProtocolClientFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public async Task<AcmeProtocolClient> CreateClientAsync()
6666
throw new PreconditionException("This ACME endpoint requires External Account Binding.");
6767
}
6868

69-
account = await acmeProtocolClient.CreateAccountAsync(new[] { $"mailto:{_options.Contacts}" }, true, externalAccountBinding);
69+
account = await acmeProtocolClient.CreateAccountAsync([$"mailto:{_options.Contacts}"], true, externalAccountBinding);
7070

7171
accountKey = new AccountKey
7272
{
@@ -82,7 +82,7 @@ public async Task<AcmeProtocolClient> CreateClientAsync()
8282

8383
if (acmeProtocolClient.Account.Payload.Contact is { Length: > 0 } && acmeProtocolClient.Account.Payload.Contact[0] != $"mailto:{_options.Contacts}")
8484
{
85-
account = await acmeProtocolClient.UpdateAccountAsync(new[] { $"mailto:{_options.Contacts}" });
85+
account = await acmeProtocolClient.UpdateAccountAsync([$"mailto:{_options.Contacts}"]);
8686

8787
SaveState(account, "account.json");
8888

KeyVault.Acmebot/Internal/CertificateExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static CertificateItem ToCertificateItem(this KeyVaultCertificateWithPoli
2424
{
2525
Id = certificate.Id,
2626
Name = certificate.Name,
27-
DnsNames = dnsNames is { Length: > 0 } ? dnsNames : new[] { certificate.Policy.Subject[3..] },
27+
DnsNames = dnsNames is { Length: > 0 } ? dnsNames : [certificate.Policy.Subject[3..]],
2828
DnsProviderName = certificate.Properties.Tags.TryGetDnsProvider(out var dnsProviderName) ? dnsProviderName : "",
2929
CreatedOn = certificate.Properties.CreatedOn.Value,
3030
ExpiresOn = certificate.Properties.ExpiresOn.Value,
@@ -46,7 +46,7 @@ public static CertificatePolicyItem ToCertificatePolicyItem(this KeyVaultCertifi
4646
return new CertificatePolicyItem
4747
{
4848
CertificateName = certificate.Name,
49-
DnsNames = dnsNames.Length > 0 ? dnsNames : new[] { certificate.Policy.Subject[3..] },
49+
DnsNames = dnsNames.Length > 0 ? dnsNames : [certificate.Policy.Subject[3..]],
5050
DnsProviderName = certificate.Properties.Tags.TryGetDnsProvider(out var dnsProviderName) ? dnsProviderName : "",
5151
KeyType = certificate.Policy.KeyType?.ToString(),
5252
KeySize = certificate.Policy.KeySize,

KeyVault.Acmebot/Internal/DnsProvidersExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static async Task<IReadOnlyList<DnsZone>> ListZonesAsync(this IEnumerable
3131

3232
if (dnsProvider is null)
3333
{
34-
return Array.Empty<DnsZone>();
34+
return [];
3535
}
3636

3737
var dnsZones = await dnsProvider.ListZonesAsync();

KeyVault.Acmebot/Models/CertificatePolicyItem.cs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,53 @@ public class CertificatePolicyItem : IValidatableObject
88
{
99
[JsonProperty("certificateName")]
1010
[RegularExpression("^[0-9a-zA-Z-]+$")]
11-
public string CertificateName { get; set; }
11+
public string? CertificateName { get; set; }
1212

1313
[JsonProperty("dnsNames")]
14-
public string[] DnsNames { get; set; }
14+
public required string[] DnsNames { get; set; }
1515

1616
[JsonProperty("dnsProviderName")]
17-
public string DnsProviderName { get; set; }
17+
public string? DnsProviderName { get; set; }
1818

1919
[JsonProperty("keyType")]
2020
[RegularExpression("^(RSA|EC)$")]
21-
public string KeyType { get; set; }
21+
public required string KeyType { get; set; }
2222

2323
[JsonProperty("keySize")]
2424
public int? KeySize { get; set; }
2525

2626
[JsonProperty("keyCurveName")]
2727
[RegularExpression(@"^P\-(256|384|521|256K)$")]
28-
public string KeyCurveName { get; set; }
28+
public string? KeyCurveName { get; set; }
2929

3030
[JsonProperty("reuseKey")]
3131
public bool? ReuseKey { get; set; }
3232

3333
[JsonProperty("dnsAlias")]
34-
public string DnsAlias { get; set; }
34+
public string? DnsAlias { get; set; }
3535

36-
public IEnumerable<string> AliasedDnsNames => string.IsNullOrEmpty(DnsAlias) ? DnsNames : new[] { DnsAlias };
36+
public IEnumerable<string> AliasedDnsNames => string.IsNullOrEmpty(DnsAlias) ? DnsNames : [DnsAlias];
3737

3838
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
3939
{
40-
if (DnsNames is null || DnsNames.Length == 0)
40+
if (DnsNames.Length == 0)
4141
{
42-
yield return new ValidationResult($"The {nameof(DnsNames)} is required.", new[] { nameof(DnsNames) });
42+
yield return new ValidationResult($"The {nameof(DnsNames)} is required.", [nameof(DnsNames)]);
43+
}
44+
45+
if (KeyType == "RSA")
46+
{
47+
if (KeySize is not (2048 or 3072 or 4096))
48+
{
49+
yield return new ValidationResult($"The {nameof(KeySize)} must be 2048, 3072, or 4096 when {nameof(KeyType)} is RSA.", [nameof(KeySize)]);
50+
}
51+
}
52+
else if (KeyType == "EC")
53+
{
54+
if (KeyCurveName is not ("P-256" or "P-384" or "P-521" or "P-256K"))
55+
{
56+
yield return new ValidationResult($"The {nameof(KeyCurveName)} must be P-256, P-384, P-521, or P-256K when {nameof(KeyType)} is EC.", [nameof(KeyCurveName)]);
57+
}
4358
}
4459
}
4560
}

KeyVault.Acmebot/Providers/GoogleDnsProvider.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,16 @@ public Task CreateTxtRecordAsync(DnsZone zone, string relativeRecordName, IEnume
5757

5858
var change = new Change
5959
{
60-
Additions = new[]
61-
{
60+
Additions =
61+
[
6262
new ResourceRecordSet
6363
{
6464
Name = recordName,
6565
Type = "TXT",
6666
Ttl = 60,
6767
Rrdatas = values.ToArray()
6868
}
69-
}
69+
]
7070
};
7171

7272
return _dnsService.Changes.Create(change, _credsParameters.ProjectId, zone.Id).ExecuteAsync();

KeyVault.Acmebot/Providers/Route53Provider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public Task CreateTxtRecordAsync(DnsZone zone, string relativeRecordName, IEnume
5858
}
5959
};
6060

61-
var request = new ChangeResourceRecordSetsRequest(zone.Id, new ChangeBatch(new List<Change> { change }));
61+
var request = new ChangeResourceRecordSetsRequest(zone.Id, new ChangeBatch([change]));
6262

6363
return _amazonRoute53Client.ChangeResourceRecordSetsAsync(request);
6464
}

0 commit comments

Comments
 (0)