Skip to content

Commit 23e4008

Browse files
committed
Fixed "Enabled" flag for SAAS deployment.
1 parent c770d4a commit 23e4008

3 files changed

Lines changed: 67 additions & 9 deletions

File tree

aws-pca-caplugin/AWSPCACAPlugin.cs

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
66
// and limitations under the License.
77

8-
using System.Collections.Concurrent;
9-
using System.Security.Cryptography;
10-
using System.Security.Cryptography.X509Certificates;
11-
using System.Text;
12-
using System.Text.RegularExpressions;
138
using Amazon.ACMPCA;
149
using Keyfactor.AnyGateway.Extensions;
1510
using Keyfactor.Extensions.CAPlugin.AWS.Client;
@@ -19,6 +14,12 @@
1914
using Keyfactor.PKI.Enums.EJBCA;
2015
using Keyfactor.PKI.PEM;
2116
using Microsoft.Extensions.Logging;
17+
using System.Collections.Concurrent;
18+
using System.Security.Cryptography;
19+
using System.Security.Cryptography.X509Certificates;
20+
using System.Text;
21+
using System.Text.RegularExpressions;
22+
using static Org.BouncyCastle.Math.EC.ECCurve;
2223

2324
namespace Keyfactor.Extensions.CAPlugin.AWS;
2425

@@ -33,15 +34,23 @@ public AWSPCACAPlugin()
3334
}
3435

3536
private IAwsPcaClient AwsClient { get; set; }
36-
37+
private bool _enabled = false;
3738

3839
//done
3940
public void Initialize(IAnyCAPluginConfigProvider configProvider, ICertificateDataReader certificateDataReader)
4041
{
4142
Logger.MethodEntry(LogLevel.Debug);
4243
_certificateDataReader = certificateDataReader;
44+
var _enabled = bool.Parse(GetRequiredString(configProvider, "Enabled"));
45+
if (!_enabled)
46+
{
47+
Logger.LogWarning($"The CA is currently in the Disabled state. It must be Enabled to perform operations. Skipping config validation and AWS PCA Client creation...");
48+
Logger.MethodExit();
49+
return;
50+
}
4351
AwsClient = new AwsPcaClient(configProvider);
4452
Logger.MethodExit(LogLevel.Debug);
53+
4554
}
4655

4756
//done
@@ -455,6 +464,12 @@ public async Task<EnrollmentResult> Enroll(
455464
public async Task Ping()
456465
{
457466
Logger.MethodEntry();
467+
if (!_enabled)
468+
{
469+
Logger.LogWarning($"The CA is currently in the Disabled state. It must be Enabled to perform operations. Skipping config validation and AWS PCA Client creation...");
470+
Logger.MethodExit();
471+
return;
472+
}
458473
try
459474
{
460475
Logger.LogInformation("Ping request received");
@@ -470,12 +485,27 @@ public async Task Ping()
470485
}
471486

472487
//do
473-
public async Task ValidateCAConnectionInfo(Dictionary<string, object> connectionInfo)
488+
public Task ValidateCAConnectionInfo(Dictionary<string, object> connectionInfo)
474489
{
490+
try
491+
{
492+
if (!(bool)connectionInfo["Enabled"])
493+
{
494+
Logger.LogWarning($"The CA is currently in the Disabled state. It must be Enabled to perform operations. Skipping validation...");
495+
Logger.MethodExit(LogLevel.Trace);
496+
return Task.CompletedTask;
497+
}
498+
}
499+
catch (Exception ex)
500+
{
501+
Logger.LogError($"Exception: {LogHandler.FlattenException(ex)}");
502+
}
503+
504+
return Task.CompletedTask;
475505
}
476506

477507
//do
478-
public async Task ValidateProductInfo(EnrollmentProductInfo productInfo,
508+
public Task ValidateProductInfo(EnrollmentProductInfo productInfo,
479509
Dictionary<string, object> connectionInfo)
480510
{
481511
var certType = Constants.GetTemplateTypes().Find(x =>
@@ -484,6 +514,7 @@ public async Task ValidateProductInfo(EnrollmentProductInfo productInfo,
484514
if (certType == null) throw new ArgumentException($"Cannot find {productInfo.ProductID}", "ProductId");
485515

486516
Logger.LogInformation($"Validated {certType} ({certType})configured for AnyGateway");
517+
return Task.CompletedTask;
487518
}
488519

489520
//done
@@ -620,6 +651,13 @@ public Dictionary<string, PropertyConfigInfo> GetCAConnectorAnnotations()
620651
Hidden = false,
621652
DefaultValue = "",
622653
Type = "String"
654+
},
655+
[Constants.Enabled] = new ()
656+
{
657+
Comments = "Flag to Enable or Disable gateway functionality. Disabling is primarily used to allow creation of the CA prior to configuration information being available.",
658+
Hidden = false,
659+
DefaultValue = true,
660+
Type = "Boolean"
623661
}
624662
};
625663
}
@@ -1090,6 +1128,17 @@ private string PreparePemTextFromApi(string? base64)
10901128

10911129
return text;
10921130
}
1131+
private static string GetRequiredString(IAnyCAPluginConfigProvider provider, string key)
1132+
{
1133+
if (!provider.CAConnectionData.TryGetValue(key, out var obj) || obj == null)
1134+
throw new InvalidOperationException($"Missing required configuration value '{key}'.");
1135+
1136+
var str = obj.ToString();
1137+
if (string.IsNullOrWhiteSpace(str))
1138+
throw new InvalidOperationException($"Configuration value '{key}' is empty.");
1139+
1140+
return str!;
1141+
}
10931142

10941143
#endregion
10951144
}

aws-pca-caplugin/Client/ACMPCAClient.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,15 @@ public AwsPcaClient(IAnyCAPluginConfigProvider configProvider)
5858

5959
if (configProvider?.CAConnectionData == null)
6060
throw new ArgumentNullException(nameof(configProvider),
61-
"Config provider and CAConnectionData are required.");
61+
"Config provider and CAConnectionData are required.");
62+
63+
var enabled = bool.Parse(GetRequiredString(configProvider, "Enabled"));
64+
if (enabled)
65+
{
66+
Logger.LogWarning($"The CA is currently in the Disabled state. It must be Enabled to perform operations. Skipping config validation and AWS PCA Client creation...");
67+
Logger.MethodExit();
68+
return;
69+
}
6270

6371
CaArn = GetRequiredString(configProvider, ConfigKeys.CaArn);
6472
S3Bucket = GetRequiredString(configProvider, ConfigKeys.S3Bucket);

aws-pca-caplugin/Constants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public static class Constants
6161
public static string IAM_USER_ACCESS_KEY = "IAMUserAccessKey";
6262
public static string IAM_USER_ACCESS_SECRET = "IAMUserAccessSecret";
6363
public static string EXTERNAL_ID = "ExternalId";
64+
public static string Enabled = "Enabled";
6465

6566

6667
public static List<string> GetTemplateTypes()

0 commit comments

Comments
 (0)