Skip to content

Commit db8ffe2

Browse files
committed
simplify API surface leaving only essential ones.
1 parent ecb080a commit db8ffe2

File tree

3 files changed

+4
-225
lines changed

3 files changed

+4
-225
lines changed

src/Testcontainers.LowkeyVault/LowkeyVaultBuilder.cs

+3-181
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ public sealed class LowkeyVaultBuilder : ContainerBuilder<LowkeyVaultBuilder, Lo
1414

1515
private const string LowKeyVaultEnvVarKey = "LOWKEY_ARGS";
1616

17-
private readonly HashSet<string> NoAutoRegistration = ["-"];
18-
1917
/// <summary>
2018
/// Initializes a new instance of the <see cref="LowkeyVaultBuilder" /> class.
2119
/// </summary>
@@ -39,115 +37,6 @@ private LowkeyVaultBuilder(LowkeyVaultConfiguration dockerResourceConfiguration)
3937
protected override LowkeyVaultConfiguration DockerResourceConfiguration { get; }
4038

4139

42-
/// <summary>
43-
/// Sets the vault names.
44-
/// </summary>
45-
/// <param name="vaultNames">The vault names.</param>
46-
/// <returns>A configured instance of <see cref="LowkeyVaultBuilder" />.</returns>
47-
public LowkeyVaultBuilder WithVaultNames(HashSet<string> vaultNames)
48-
{
49-
return Merge(DockerResourceConfiguration, new LowkeyVaultConfiguration(vaultNames: vaultNames))
50-
.WithEnvironment(LowKeyVaultEnvVarKey, AddOrAppend($"--LOWKEY_VAULT_NAMES={string.Join(",", vaultNames)}"));
51-
}
52-
53-
/// <summary>
54-
/// Sets the vault aliases.
55-
/// </summary>
56-
/// <param name="aliasMap">The vault aliases.</param>
57-
/// <returns>A configured instance of <see cref="LowkeyVaultBuilder" />.</returns>
58-
public LowkeyVaultBuilder WithVaultAliases(Dictionary<string, HashSet<string>> aliasMap)
59-
{
60-
return Merge(DockerResourceConfiguration, new LowkeyVaultConfiguration(aliasMap: aliasMap))
61-
.WithEnvironment(LowKeyVaultEnvVarKey, AddOrAppend($"--LOWKEY_VAULT_ALIASES={ProcessVaultAliases(aliasMap)}"));
62-
}
63-
64-
/// <summary>
65-
/// Sets No Auto Registration.
66-
/// </summary>
67-
/// <returns>A configured instance of <see cref="LowkeyVaultBuilder" />.</returns>
68-
public LowkeyVaultBuilder WithNoAutoRegistration()
69-
{
70-
return WithVaultNames(NoAutoRegistration);
71-
}
72-
73-
/// <summary>
74-
/// Sets Import file.
75-
/// </summary>
76-
/// <param name="importFilePath">The import file path.</param>
77-
/// <returns>A configured instance of <see cref="LowkeyVaultBuilder" />.</returns>
78-
public LowkeyVaultBuilder WithImportFile(string importFilePath)
79-
{
80-
return WithEnvironment(LowKeyVaultEnvVarKey, AddOrAppend($"--LOWKEY_IMPORT_LOCATION={importFilePath}"))
81-
.WithResourceMapping(new FileInfo(importFilePath), new FileInfo("/import/vaults.json"));
82-
}
83-
84-
/// <summary>
85-
/// Sets External config file.
86-
/// </summary>
87-
/// <param name="externalConfigFilePath">The external config file path.</param>
88-
/// <returns>A configured instance of <see cref="LowkeyVaultBuilder" />.</returns>
89-
public LowkeyVaultBuilder WithExternalConfigFile(string externalConfigFilePath)
90-
{
91-
return Merge(DockerResourceConfiguration, new LowkeyVaultConfiguration(externalConfigFilePath: externalConfigFilePath))
92-
.WithResourceMapping(new FileInfo(externalConfigFilePath), new FileInfo("/config/application.properties"));
93-
}
94-
95-
/// <summary>
96-
/// Sets Custom Ssl Certificate file.
97-
/// </summary>
98-
/// <param name="keyStoreFilePath">The keyStore (custom Ssl Certificate) file path.</param>
99-
/// <param name="keyStorePassword">The keyStore password.</param>
100-
/// <param name="keyStoreType">The keyStore Type.</param>
101-
/// <returns>A configured instance of <see cref="LowkeyVaultBuilder" />.</returns>
102-
public LowkeyVaultBuilder WithCustomSslCertificate(string keyStoreFilePath, string keyStorePassword, StoreType keyStoreType = StoreType.PKCS12)
103-
{
104-
return WithEnvironment(LowKeyVaultEnvVarKey, AddOrAppend($"--server.ssl.key-store={keyStoreFilePath} "
105-
+ $"--server.ssl.key-store-type={keyStoreType} "
106-
+ $"--server.ssl.key-store-password={keyStorePassword ?? string.Empty}"))
107-
.WithResourceMapping(new FileInfo(keyStoreFilePath), new FileInfo("/config/cert.store"));
108-
}
109-
110-
/// <summary>
111-
/// Enable Or Disable Debug.
112-
/// </summary>
113-
/// <param name="debug">The flag to enable or disable debug.</param>
114-
/// <returns>A configured instance of <see cref="LowkeyVaultBuilder" />.</returns>
115-
public LowkeyVaultBuilder WithDebug(bool debug)
116-
{
117-
return WithEnvironment(LowKeyVaultEnvVarKey, AddOrAppend($"--LOWKEY_DEBUG_REQUEST_LOG={debug}"));
118-
}
119-
120-
/// <summary>
121-
/// Enable Or Disable Relaxed Ports.
122-
/// </summary>
123-
/// <param name="relaxedPorts">The flag to enable or disable relaxed ports.</param>
124-
/// <returns>A configured instance of <see cref="LowkeyVaultBuilder" />.</returns>
125-
public LowkeyVaultBuilder WithRelaxedPorts(bool relaxedPorts)
126-
{
127-
return WithEnvironment(LowKeyVaultEnvVarKey, AddOrAppend($"--LOWKEY_VAULT_RELAXED_PORTS={relaxedPorts}"));
128-
}
129-
130-
/// <summary>
131-
/// Sets The host used to replace host placeholder in import template.
132-
/// </summary>
133-
/// <param name="logicalHost">The logical host.</param>
134-
/// <returns>A configured instance of <see cref="LowkeyVaultBuilder" />.</returns>
135-
public LowkeyVaultBuilder WithLogicalHost(string logicalHost)
136-
{
137-
return WithEnvironment(LowKeyVaultEnvVarKey, AddOrAppend($"--LOWKEY_IMPORT_TEMPLATE_HOST={logicalHost}"));
138-
}
139-
140-
/// <summary>
141-
/// Sets The port used to replace host placeholder in import template.
142-
/// </summary>
143-
/// <param name="logicalPort">The logical port.</param>
144-
/// <returns>A configured instance of <see cref="LowkeyVaultBuilder" />.</returns>
145-
public LowkeyVaultBuilder WithLogicalPort(ushort logicalPort)
146-
{
147-
return WithEnvironment(LowKeyVaultEnvVarKey, AddOrAppend($"--LOWKEY_IMPORT_TEMPLATE_PORT={logicalPort}"));
148-
}
149-
150-
15140
/// <summary>
15241
/// Sets Additional Arguments.
15342
/// </summary>
@@ -163,12 +52,7 @@ public LowkeyVaultBuilder WithAdditionalArguments(List<string> additionalArgumen
16352
public override LowkeyVaultContainer Build()
16453
{
16554
Validate();
166-
167-
var waitStrategy = Wait.ForUnixContainer().UntilMessageIsLogged("(?s).*Started LowkeyVaultApp.*$");
168-
169-
var lowkeyVaultBuilder = DockerResourceConfiguration.WaitStrategies.Count() > 1 ? this : WithWaitStrategy(waitStrategy);
170-
171-
return new LowkeyVaultContainer(lowkeyVaultBuilder.DockerResourceConfiguration);
55+
return new LowkeyVaultContainer(DockerResourceConfiguration);
17256
}
17357

17458
/// <inheritdoc />
@@ -178,28 +62,15 @@ protected override LowkeyVaultBuilder Init()
17862
.WithImage(LowkeyVaultImage)
17963
.WithPortBinding(LowkeyVaultPort, true)
18064
.WithPortBinding(LowkeyVaultTokenPort, true)
181-
.WithRelaxedPorts(true);
65+
.WithAdditionalArguments([$"--LOWKEY_VAULT_RELAXED_PORTS={true}"])
66+
.WithWaitStrategy(Wait.ForUnixContainer().UntilMessageIsLogged("(?s).*Started LowkeyVaultApp.*$"));
18267
}
18368

18469
/// <inheritdoc />
18570
protected override void Validate()
18671
{
18772
base.Validate();
188-
189-
_ = Guard.Argument(DockerResourceConfiguration.VaultNames, nameof(DockerResourceConfiguration.VaultNames)).NotNull();
190-
_ = Guard.Argument(DockerResourceConfiguration.AliasMap, nameof(DockerResourceConfiguration.AliasMap)).NotNull();
19173
_ = Guard.Argument(DockerResourceConfiguration.AdditionalArguments, nameof(DockerResourceConfiguration.AdditionalArguments)).NotNull();
192-
_ = Guard.Argument(DockerResourceConfiguration, nameof(DockerResourceConfiguration.ExternalConfigFilePath))
193-
.ThrowIf(argument =>
194-
{
195-
var externalConfigFilePath = argument.Value.ExternalConfigFilePath;
196-
var fileName = Path.GetFileName(externalConfigFilePath);
197-
return !string.IsNullOrEmpty(externalConfigFilePath) && !Path.GetFileName(fileName).EndsWith(".properties", StringComparison.Ordinal);
198-
}, argument => throw new ArgumentException("External configuration file must be a *.properties file."));
199-
200-
ValidateVaultNames(DockerResourceConfiguration.VaultNames);
201-
202-
ValidateAliasMap(DockerResourceConfiguration.AliasMap);
20374
}
20475

20576
/// <inheritdoc />
@@ -282,53 +153,4 @@ static Dictionary<string, string> ParseToDictionary(string input)
282153
return dictionary;
283154
}
284155
}
285-
286-
private void ValidateVaultNames(HashSet<string> vaultNames)
287-
{
288-
if (!NoAutoRegistration.SetEquals(vaultNames))
289-
{
290-
var invalid = vaultNames.Where(s => !Regex.IsMatch(s ?? string.Empty, "^[0-9a-zA-Z-]+$", RegexOptions.Compiled)).ToList();
291-
292-
if (invalid.Count != 0)
293-
{
294-
throw new ArgumentException("VaultNames contains invalid values: " + string.Join(", ", invalid));
295-
}
296-
}
297-
}
298-
299-
private static void ValidateAliasMap(Dictionary<string, HashSet<string>> aliasMap)
300-
{
301-
foreach (var host in aliasMap.Keys)
302-
{
303-
if (!Regex.IsMatch(host, "^[0-9a-z\\-_.]+$"))
304-
{
305-
throw new ArgumentException($"Vault host names must match '^[0-9a-z\\-_.]+$'. Found: {host}");
306-
}
307-
}
308-
309-
foreach (var alias in aliasMap.Values)
310-
{
311-
foreach (var host in alias)
312-
{
313-
if (!Regex.IsMatch(host, "^[0-9a-z\\-_.]+(:[0-9]+|:<port>)?$"))
314-
{
315-
throw new ArgumentException($"Vault aliases must match '^[0-9a-z\\-_.]+(:[0-9]+|:<port>)?$'. Found: {host}");
316-
}
317-
}
318-
}
319-
}
320-
321-
private static string ProcessVaultAliases(Dictionary<string, HashSet<string>> aliasMap)
322-
{
323-
return aliasMap.OrderBy(pair => pair.Key) // Sort keys
324-
.SelectMany(pair => pair.Value.OrderBy(alias => alias) // Sort values
325-
.Select(alias => $"{pair.Key}={alias}"))
326-
.Aggregate((current, next) => $"{current},{next}"); // Join the pairs into a single string with commas
327-
}
328-
329-
public enum StoreType
330-
{
331-
JKS,
332-
PKCS12
333-
}
334156
}

src/Testcontainers.LowkeyVault/LowkeyVaultConfiguration.cs

+1-28
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,9 @@ public sealed class LowkeyVaultConfiguration : ContainerConfiguration
77
/// <summary>
88
/// Initializes a new instance of the <see cref="LowkeyVaultConfiguration" /> class.
99
/// </summary>
10-
/// <param name="vaultNames">The vault names.</param>
11-
/// <param name="aliasMap">The vault aliases.</param>
12-
/// <param name="externalConfigFilePath">The external config file path.</param>
1310
/// <param name="additionalArguments">The additional arguments to be passed via environment variables to the container.</param>
14-
public LowkeyVaultConfiguration(HashSet<string> vaultNames = null,
15-
Dictionary<string, HashSet<string>> aliasMap = null,
16-
string externalConfigFilePath = null,
17-
List<string> additionalArguments = null)
11+
public LowkeyVaultConfiguration(List<string> additionalArguments = null)
1812
{
19-
VaultNames = vaultNames;
20-
AliasMap = aliasMap;
21-
ExternalConfigFilePath = externalConfigFilePath;
2213
AdditionalArguments = additionalArguments;
2314
}
2415

@@ -60,27 +51,9 @@ public LowkeyVaultConfiguration(LowkeyVaultConfiguration resourceConfiguration)
6051
public LowkeyVaultConfiguration(LowkeyVaultConfiguration oldValue, LowkeyVaultConfiguration newValue)
6152
: base(oldValue, newValue)
6253
{
63-
VaultNames = BuildConfiguration.Combine(oldValue.VaultNames, newValue.VaultNames);
64-
AliasMap = BuildConfiguration.Combine(oldValue.AliasMap, newValue.AliasMap);
65-
ExternalConfigFilePath = BuildConfiguration.Combine(oldValue.ExternalConfigFilePath, newValue.ExternalConfigFilePath);
6654
AdditionalArguments = BuildConfiguration.Combine(oldValue.AdditionalArguments, newValue.AdditionalArguments);
6755
}
6856

69-
/// <summary>
70-
/// Gets the Vault names.
71-
/// </summary>
72-
internal HashSet<string> VaultNames { get; } = [];
73-
74-
/// <summary>
75-
/// Gets the Alias Map.
76-
/// </summary>
77-
internal Dictionary<string, HashSet<string>> AliasMap { get; } = [];
78-
79-
/// <summary>
80-
/// Gets the External Config File Path.
81-
/// </summary>
82-
internal string ExternalConfigFilePath { get; }
83-
8457
/// <summary>
8558
/// Gets Additional Arguments.
8659
/// </summary>

src/Testcontainers.LowkeyVault/LowkeyVaultContainer.cs

-16
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ namespace Testcontainers.LowkeyVault;
44
[PublicAPI]
55
public sealed class LowkeyVaultContainer : DockerContainer
66
{
7-
private const string LocalHost = "localhost";
8-
97
/// <summary>
108
/// Gets a configured HTTP client
119
/// </summary>
@@ -38,20 +36,6 @@ public string GetTokenEndpointUrl()
3836
return new UriBuilder(Uri.UriSchemeHttp, Hostname, GetMappedPublicPort(LowkeyVaultBuilder.LowkeyVaultTokenPort), LowkeyVaultBuilder.TokenEndPointPath).ToString();
3937
}
4038

41-
/// <summary>
42-
/// Gets the URL of the vault with a given name.
43-
/// <param name="vaultName">the name of the vault.</param>
44-
/// </summary>
45-
/// <returns>The vault base URL.</returns>
46-
public string GetVaultBaseUrl(string vaultName)
47-
{
48-
// Using `LocalHost` here instead of `Hostname` (which resolves to `127.0.0.1` in this environment)
49-
// to address a compatibility issue with the Java URI parser utilized by the Lowkey Vault client.
50-
// The parser fails to properly handle URIs containing a mix of DNS names and IP addresses, leading to errors.
51-
// For more details, refer to: https://github.com/nagyesta/lowkey-vault/issues/1319#issuecomment-2600214768
52-
return new UriBuilder(Uri.UriSchemeHttps, $"{vaultName}.{LocalHost}", GetMappedPublicPort(LowkeyVaultBuilder.LowkeyVaultPort)).ToString();
53-
}
54-
5539
/// <summary>
5640
/// Gets a <see cref="X509Certificate2Collection"/> containing the default certificate shipped with Lowkey Vault.
5741
/// </summary>

0 commit comments

Comments
 (0)