Skip to content

Commit 2ca32c9

Browse files
authored
feat(LocalStack): Require auth token for 4.15 and onwards (#1667)
1 parent 0aa181b commit 2ca32c9

13 files changed

Lines changed: 76 additions & 20 deletions

File tree

src/Testcontainers.Kafka/KafkaBuilder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,8 @@ protected override void Validate()
275275

276276
base.Validate();
277277

278-
Predicate<KafkaVendor?> vendorNotFound = value => value == null && !VendorConfigurations.Any(v => v.IsImageFromVendor(DockerResourceConfiguration.Image));
278+
Predicate<KafkaVendor?> vendorNotFound = value =>
279+
value == null && !VendorConfigurations.Any(v => v.IsImageFromVendor(DockerResourceConfiguration.Image));
279280

280281
_ = Guard.Argument(DockerResourceConfiguration.Vendor, nameof(DockerResourceConfiguration.Vendor))
281282
.ThrowIf(argument => vendorNotFound(argument.Value), argument => new ArgumentException(message, argument.Name));

src/Testcontainers.LocalStack/LocalStackBuilder.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,20 @@ protected override LocalStackBuilder Init()
8080
request.ForPath("/_localstack/health").ForPort(LocalStackPort)));
8181
}
8282

83+
/// <inheritdoc />
84+
protected override void Validate()
85+
{
86+
const string message = "The image '{0}' requires the LOCALSTACK_AUTH_TOKEN environment variable for LocalStack 4.15 and onwards. For more information, see https://blog.localstack.cloud/localstack-single-image-next-steps/.";
87+
88+
base.Validate();
89+
90+
Predicate<LocalStackConfiguration> requiresAuthToken = value =>
91+
!value.Environments.TryGetValue("LOCALSTACK_AUTH_TOKEN", out _) && (value.Image.MatchLatestOrNightly() || value.Image.MatchVersion(v => v.Major > 4 || v.Major == 4 && v.Minor > 14));
92+
93+
_ = Guard.Argument(DockerResourceConfiguration, nameof(DockerResourceConfiguration.Image))
94+
.ThrowIf(argument => requiresAuthToken(argument.Value), argument => new ArgumentException(string.Format(message, argument.Value.Image.FullName), argument.Name));
95+
}
96+
8397
/// <inheritdoc />
8498
protected override LocalStackBuilder Clone(IResourceConfiguration<CreateContainerParameters> resourceConfiguration)
8599
{

src/Testcontainers.LocalStack/Usings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
global using System;
22
global using System.Diagnostics.CodeAnalysis;
33
global using Docker.DotNet.Models;
4+
global using DotNet.Testcontainers;
45
global using DotNet.Testcontainers.Builders;
56
global using DotNet.Testcontainers.Configurations;
67
global using DotNet.Testcontainers.Containers;

src/Testcontainers.Neo4j/Neo4jBuilder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,11 @@ protected override void Validate()
145145

146146
base.Validate();
147147

148-
Predicate<Neo4jConfiguration> licenseAgreementNotAccepted = value => value.Image.Tag != null && value.Image.Tag.Contains("enterprise")
149-
&& (!value.Environments.TryGetValue(AcceptLicenseAgreementEnvVar, out var licenseAgreementValue) || !AcceptLicenseAgreement.Equals(licenseAgreementValue, StringComparison.Ordinal));
148+
Predicate<Neo4jConfiguration> licenseAgreementNotAccepted = value =>
149+
value.Image.Tag != null && value.Image.Tag.Contains("enterprise") && (!value.Environments.TryGetValue(AcceptLicenseAgreementEnvVar, out var licenseAgreementValue) || !AcceptLicenseAgreement.Equals(licenseAgreementValue, StringComparison.Ordinal));
150150

151151
_ = Guard.Argument(DockerResourceConfiguration, nameof(DockerResourceConfiguration.Image))
152-
.ThrowIf(argument => licenseAgreementNotAccepted(argument.Value), argument => new ArgumentException(string.Format(message, DockerResourceConfiguration.Image.FullName), argument.Name));
152+
.ThrowIf(argument => licenseAgreementNotAccepted(argument.Value), argument => new ArgumentException(string.Format(message, argument.Value.Image.FullName), argument.Name));
153153
}
154154

155155
/// <inheritdoc />

src/Testcontainers.Oracle/OracleBuilder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,15 @@ protected override OracleBuilder Init()
139139
/// <inheritdoc />
140140
protected override void Validate()
141141
{
142-
base.Validate();
143-
144142
const string message = "The image '{0}' does not support configuring the database. It is only supported on Oracle 18 and onwards.";
145143

144+
base.Validate();
145+
146146
Predicate<OracleConfiguration> databaseConfigurationNotSupported = value =>
147147
value.Database != null && value.Image.MatchVersion(v => v.Major < 18);
148148

149149
_ = Guard.Argument(DockerResourceConfiguration, nameof(DockerResourceConfiguration.Database))
150-
.ThrowIf(argument => databaseConfigurationNotSupported(argument.Value), _ => new NotSupportedException(string.Format(message, DockerResourceConfiguration.Image.FullName)));
150+
.ThrowIf(argument => databaseConfigurationNotSupported(argument.Value), argument => new NotSupportedException(string.Format(message, argument.Value.Image.FullName)));
151151

152152
_ = Guard.Argument(DockerResourceConfiguration.Username, nameof(DockerResourceConfiguration.Username))
153153
.NotNull()

src/Testcontainers/Builders/AbstractBuilder`4.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,14 @@ protected virtual TBuilderEntity Init()
140140
/// <exception cref="ArgumentException">Thrown when a mandatory Docker resource configuration is not set.</exception>
141141
protected virtual void Validate()
142142
{
143-
_ = Guard.Argument(DockerResourceConfiguration.Logger, nameof(IResourceConfiguration<TCreateResourceEntity>.Logger))
143+
_ = Guard.Argument(DockerResourceConfiguration.Logger, nameof(DockerResourceConfiguration.Logger))
144144
.NotNull();
145145

146-
_ = Guard.Argument(DockerResourceConfiguration.DockerEndpointAuthConfig, nameof(IResourceConfiguration<TCreateResourceEntity>.DockerEndpointAuthConfig))
146+
_ = Guard.Argument(DockerResourceConfiguration.DockerEndpointAuthConfig, nameof(DockerResourceConfiguration.DockerEndpointAuthConfig))
147147
.ThrowIf(argument => argument.Value == null, CreateDockerUnavailableException);
148148

149149
const string reuseNotSupported = "Reuse cannot be used in conjunction with WithCleanUp(true).";
150-
_ = Guard.Argument(DockerResourceConfiguration, nameof(IResourceConfiguration<TCreateResourceEntity>.Reuse))
150+
_ = Guard.Argument(DockerResourceConfiguration, nameof(DockerResourceConfiguration.Reuse))
151151
.ThrowIf(argument => argument.Value.Reuse.HasValue && argument.Value.Reuse.Value && !Guid.Empty.Equals(argument.Value.SessionId), argument => new ArgumentException(reuseNotSupported, argument.Name));
152152
}
153153

src/Testcontainers/Builders/ContainerBuilder`3.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -497,10 +497,10 @@ protected override void Validate()
497497
base.Validate();
498498

499499
const string reuseNotSupported = "Reuse cannot be used in conjunction with WithAutoRemove(true).";
500-
_ = Guard.Argument(DockerResourceConfiguration, nameof(IContainerConfiguration.Reuse))
500+
_ = Guard.Argument(DockerResourceConfiguration, nameof(DockerResourceConfiguration.Reuse))
501501
.ThrowIf(argument => argument.Value.Reuse.HasValue && argument.Value.Reuse.Value && argument.Value.AutoRemove.HasValue && argument.Value.AutoRemove.Value, argument => new ArgumentException(reuseNotSupported, argument.Name));
502502

503-
_ = Guard.Argument(DockerResourceConfiguration.Image, nameof(IContainerConfiguration.Image))
503+
_ = Guard.Argument(DockerResourceConfiguration.Image, nameof(DockerResourceConfiguration.Image))
504504
.NotNull();
505505
}
506506

@@ -516,7 +516,7 @@ protected virtual void ValidateLicenseAgreement()
516516
!value.Environments.TryGetValue(AcceptLicenseAgreementEnvVar, out var licenseAgreementValue) || !AcceptLicenseAgreement.Equals(licenseAgreementValue, StringComparison.Ordinal);
517517

518518
_ = Guard.Argument(DockerResourceConfiguration, nameof(DockerResourceConfiguration.Image))
519-
.ThrowIf(argument => licenseAgreementNotAccepted(argument.Value), argument => new ArgumentException(string.Format(message, DockerResourceConfiguration.Image.FullName), argument.Name));
519+
.ThrowIf(argument => licenseAgreementNotAccepted(argument.Value), argument => new ArgumentException(string.Format(message, argument.Value.Image.FullName), argument.Name));
520520
}
521521

522522
/// <summary>

src/Testcontainers/Builders/ImageFromDockerfileBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ protected override void Validate()
133133
base.Validate();
134134

135135
const string reuseNotSupported = "Building an image does not support the reuse feature. To keep the built image, disable the cleanup.";
136-
_ = Guard.Argument(DockerResourceConfiguration, nameof(IImageFromDockerfileConfiguration.Reuse))
136+
_ = Guard.Argument(DockerResourceConfiguration, nameof(DockerResourceConfiguration.Reuse))
137137
.ThrowIf(argument => argument.Value.Reuse.HasValue && argument.Value.Reuse.Value, argument => new ArgumentException(reuseNotSupported, argument.Name));
138138
}
139139

src/Testcontainers/Builders/NetworkBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ protected override void Validate()
8383
{
8484
base.Validate();
8585

86-
_ = Guard.Argument(DockerResourceConfiguration.Name, nameof(INetworkConfiguration.Name))
86+
_ = Guard.Argument(DockerResourceConfiguration.Name, nameof(DockerResourceConfiguration.Name))
8787
.NotNull()
8888
.NotEmpty();
8989
}

src/Testcontainers/Builders/VolumeBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ protected override void Validate()
6868
{
6969
base.Validate();
7070

71-
_ = Guard.Argument(DockerResourceConfiguration.Name, nameof(IVolumeConfiguration.Name))
71+
_ = Guard.Argument(DockerResourceConfiguration.Name, nameof(DockerResourceConfiguration.Name))
7272
.NotNull()
7373
.NotEmpty();
7474
}

0 commit comments

Comments
 (0)