Skip to content

Commit

Permalink
feat: Introduce new Azurite module feature OAuth
Browse files Browse the repository at this point in the history
  • Loading branch information
vlaskal committed Feb 19, 2023
1 parent 397a0c8 commit 19f22e4
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 9 deletions.
43 changes: 41 additions & 2 deletions src/Testcontainers.Azurite/AzuriteBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public override AzuriteContainer Build()
commandArgs.Add(this.GetSkipApiVersionCheckEnabled());
commandArgs.Add(this.GetProductStyleUrlDisabled());
commandArgs.AddRange(this.GetHttps());
commandArgs.AddRange(this.GetOauthLevel());

var builder = this
.WithEntrypoint(this.GetExecutable())
Expand Down Expand Up @@ -151,8 +152,15 @@ public AzuriteBuilder WithHttpsDefinedByPfxFile(string pfxFilePath, string pfxFi
/// </summary>
public AzuriteBuilder WithHttpsDefinedByPemFiles(string certificateFilePath, string keyFilePath)
{
return this.Merge(this.DockerResourceConfiguration,
new AzuriteConfiguration(certificate: certificateFilePath, key: keyFilePath));
return this.Merge(this.DockerResourceConfiguration, new AzuriteConfiguration(certificate: certificateFilePath, key: keyFilePath));
}

/// <summary>
/// Sets an OAuth level.
/// </summary>
public AzuriteBuilder WithOauth(AzuriteOauthLevels level = AzuriteOauthLevels.Basic)
{
return this.Merge(this.DockerResourceConfiguration, new AzuriteConfiguration(oauthLevel: level));
}

/// <inheritdoc />
Expand Down Expand Up @@ -189,6 +197,27 @@ protected override AzuriteBuilder Merge(AzuriteConfiguration oldValue, AzuriteCo
return new AzuriteBuilder(new AzuriteConfiguration(oldValue, newValue));
}

/// <inheritdoc />
protected override void Validate()
{
base.Validate();

if (!string.IsNullOrEmpty(this.DockerResourceConfiguration.Certificate))
{
if (string.IsNullOrEmpty(this.DockerResourceConfiguration.Key) && string.IsNullOrEmpty(this.DockerResourceConfiguration.Password))
{
const string certificateMessage = "Cannot setup HTTPS configuration certificate without provided key or password. Set key pem file or set password for PFX file.\nhttps://github.com/Azure/Azurite#certificate-configuration-https";
throw new ArgumentException(certificateMessage, nameof(AzuriteConfiguration.Certificate));
}
}

if (this.DockerResourceConfiguration.OauthLevel.HasValue && string.IsNullOrEmpty(this.DockerResourceConfiguration.Certificate))
{
const string oauthMessage = "Cannot setup OAuth configuration without configured HTTPS. Set HTTPS configuration for Azurite or do not use OAuth configuration. More info:\nhttps://github.com/Azure/Azurite#oauth-configuration";
throw new ArgumentException(oauthMessage, nameof(AzuriteConfiguration.OauthLevel));
}
}

private string GetExecutable()
{
return "azurite";
Expand Down Expand Up @@ -265,5 +294,15 @@ private string[] GetHttps()

return args.ToArray();
}

private string[] GetOauthLevel()
{
if (!this.DockerResourceConfiguration.OauthLevel.HasValue)
{
return Array.Empty<string>();
}

return new[] { "--oauth", this.DockerResourceConfiguration.OauthLevel.ToString().ToLowerInvariant() };
}
}
}
23 changes: 16 additions & 7 deletions src/Testcontainers.Azurite/AzuriteConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public sealed class AzuriteConfiguration : ContainerConfiguration
/// <param name="certificate">A value to certificate pem or PFX file to use by https endpoints.</param>
/// <param name="password">A value with password for PFX file.</param>
/// <param name="key">A value to key pem file.</param>
/// <param name="oauthLevel">A value indicating whether the Azurite should support specified OAuth level.</param>
public AzuriteConfiguration(
bool? debugModeEnabled = null,
bool? silentModeEnabled = null,
Expand All @@ -23,7 +24,8 @@ public AzuriteConfiguration(
bool? skipApiVersionCheckEnabled = null,
string certificate = null,
string password = null,
string key = null)
string key = null,
AzuriteOauthLevels? oauthLevel = null)
{
this.DebugModeEnabled = debugModeEnabled;
this.SilentModeEnabled = silentModeEnabled;
Expand All @@ -33,6 +35,7 @@ public AzuriteConfiguration(
this.Certificate = certificate;
this.Password = password;
this.Key = key;
this.OauthLevel = oauthLevel;
}

/// <summary>
Expand Down Expand Up @@ -81,6 +84,7 @@ public AzuriteConfiguration(AzuriteConfiguration oldValue, AzuriteConfiguration
this.Certificate = BuildConfiguration.Combine(oldValue.Certificate, newValue.Certificate);
this.Password = BuildConfiguration.Combine(oldValue.Password, newValue.Password);
this.Key = BuildConfiguration.Combine(oldValue.Key, newValue.Key);
this.OauthLevel = BuildConfiguration.Combine(oldValue.OauthLevel, newValue.OauthLevel);
}

/// <summary>
Expand Down Expand Up @@ -116,6 +120,15 @@ public AzuriteConfiguration(AzuriteConfiguration oldValue, AzuriteConfiguration
/// </remarks>
public bool? SkipApiVersionCheckEnabled { get; }

/// <summary>
/// Gets a value indicating whether product style URL is enabled or not.
/// </summary>
/// <remarks>
/// Parses storage account name from the URI path, instead of the URI host.
/// Default value is false.
/// </remarks>
public bool? ProductStyleUrlDisabled { get; }

/// <summary>
/// Gets a PFX file path or certificate PEM file path used by https endpoints.
/// </summary>
Expand All @@ -132,12 +145,8 @@ public AzuriteConfiguration(AzuriteConfiguration oldValue, AzuriteConfiguration
public string Key { get; }

/// <summary>
/// Gets a value indicating whether product style URL is enabled or not.
/// Gets a value of OAuth level.
/// </summary>
/// <remarks>
/// Parses storage account name from the URI path, instead of the URI host.
/// Default value is false.
/// </remarks>
public bool? ProductStyleUrlDisabled { get; }
public AzuriteOauthLevels? OauthLevel { get; }
}
}
7 changes: 7 additions & 0 deletions src/Testcontainers.Azurite/AzuriteOauthLevels.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Testcontainers.Azurite
{
public enum AzuriteOauthLevels
{
Basic,
}
}

0 comments on commit 19f22e4

Please sign in to comment.