Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Client secret from env var #178

Merged
merged 2 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Fauna.Test/Configuration.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,30 @@ public void ConstructorWorksFine()
Assert.IsTrue(b.DisposeHttpClient);
}

[Test]
public void ConstructorUsesEnvVar()
{
Environment.SetEnvironmentVariable("FAUNA_SECRET", "secret");
var b = new Configuration();

Assert.AreEqual("secret", b.Secret);
Assert.AreEqual(Endpoints.Default, b.Endpoint);
Assert.IsTrue(b.DisposeHttpClient);
}

[Test]
public void ConstructorThrowsWithNullSecret()
{
string? currentVal = Environment.GetEnvironmentVariable("FAUNA_SECRET");
Assert.Throws<ArgumentNullException>(() =>
{
Environment.SetEnvironmentVariable("FAUNA_SECRET", null);
var b = new Configuration();

});
Environment.SetEnvironmentVariable("FAUNA_SECRET", currentVal);
}

[Test]
public void ConstructorWithHttpClient()
{
Expand Down
6 changes: 6 additions & 0 deletions Fauna/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ public class Client : BaseClient, IDisposable
/// </summary>
public long LastSeenTxn { get; private set; }

/// <summary>
/// Initializes a new instance of a Client with the default configuration.
/// Assumes the environment variable FAUNA_SECRET is set.
/// </summary>
public Client() : this(new Configuration()) { }

/// <summary>
/// Initializes a new instance of a Client with a secret.
/// </summary>
Expand Down
28 changes: 15 additions & 13 deletions Fauna/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ public record class Configuration
/// <summary>
/// Whether the <see cref="Client"/> should dispose of the <see cref="HttpClient"/> on Dispose.
/// </summary>
public bool DisposeHttpClient { get; }
public bool DisposeHttpClient { get; } = true;

/// <summary>
/// The HTTP Client to use for requests.
/// </summary>
public HttpClient HttpClient { get; }
public HttpClient HttpClient { get; } = new HttpClient { Timeout = TimeSpan.FromSeconds(5) };

/// <summary>
/// The secret key used for authentication.
/// </summary>
public string Secret { get; init; }
public string Secret { get; init; } = Environment.GetEnvironmentVariable("FAUNA_SECRET") ?? string.Empty;

/// <summary>
/// The endpoint URL of the Fauna server.
Expand All @@ -44,26 +44,28 @@ public record class Configuration
/// </summary>
public IStatsCollector? StatsCollector { get; init; } = new StatsCollector();

public Configuration()
{
if (string.IsNullOrEmpty(Secret))
{
throw new ArgumentNullException(nameof(Secret), "Need to set FAUNA_SECRET environment variable or pass a secret as a parameter when creating the Client.");
}
}

/// <summary>
/// Initializes a new instance of the <see cref="Configuration"/> record with the specified secret key.
/// </summary>
/// <param name="secret">The secret key used for authentication.</param>
/// <param name="httpClient">The <see cref="HttpClient"/> to use.</param>
public Configuration(string secret, HttpClient? httpClient = null)
{
Secret = secret;
if (httpClient is null)
{
HttpClient = new HttpClient
{
Timeout = TimeSpan.FromSeconds(5)
};
DisposeHttpClient = true;
}
else
{
HttpClient = httpClient;
return;
}

Secret = secret;
HttpClient = httpClient;
DisposeHttpClient = false;
}
}
Loading