Skip to content

Commit d0a0be4

Browse files
committed
[#8] [add] impl
1 parent 19d5b73 commit d0a0be4

File tree

3 files changed

+94
-6
lines changed

3 files changed

+94
-6
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace Etcd.Microsoft.Extensions.Configuration.Auth;
2+
3+
/// <summary>
4+
/// Provides a factory for creating environment-based credentials for etcd.
5+
/// </summary>
6+
public static class EnvironmentCredentialsFactory
7+
{
8+
/// <summary>
9+
/// Tries to create credentials from environment variables.
10+
/// </summary>
11+
/// <returns>Credentials if both username and password are set; otherwise, null.</returns>
12+
public static ICredentials? TryCreate()
13+
{
14+
var userName = EtcdApplicationEnvironment.UserName;
15+
var password = EtcdApplicationEnvironment.Password;
16+
17+
if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
18+
return null;
19+
20+
return new Credentials(userName, password);
21+
}
22+
}

src/Etcd.Microsoft.Extensions.Configuration/ConfigurationBuilderExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ public static IConfigurationBuilder AddEtcd(this IConfigurationBuilder configura
9898
{
9999
ArgumentNullException.ThrowIfNull(configurationBuilder);
100100

101+
credentials = EnvironmentCredentialsFactory.TryCreate() ?? credentials;
102+
101103
var clientFactory = new EtcdClientFactory(settings);
102104
var client = new EtcdKeyValueClient(clientFactory, credentials, enableWatch, unwatchOnDispose);
103105

src/Etcd.Microsoft.Extensions.Configuration/EtcdApplicationEnvironment.cs

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,90 @@ namespace Etcd.Microsoft.Extensions.Configuration;
77
/// </summary>
88
public static class EtcdApplicationEnvironment
99
{
10+
private static string _connectionStringEnvironmentVariableName = "ETCD_CLIENT_CONNECTION_STRING";
11+
private static string _userNameEnvironmentVariableName = "ETCD_CLIENT_USER_NAME";
12+
private static string _passwordEnvironmentVariableName = "ETCD_CLIENT_PASSWORD";
13+
private static string? _connectionString;
14+
1015
/// <summary>
1116
/// The connection string environment variable name
1217
/// </summary>
13-
public const string ConnectionStringEnvironmentVariableName = "ETCD_CLIENT_CONNECTION_STRING";
18+
public static string ConnectionStringEnvironmentVariableName
19+
{
20+
get => _connectionStringEnvironmentVariableName;
21+
set
22+
{
23+
if (string.IsNullOrEmpty(value))
24+
throw new ArgumentNullException(nameof(value));
1425

15-
private static string? _connectionString;
26+
_connectionStringEnvironmentVariableName = value;
27+
}
28+
}
29+
30+
/// <summary>
31+
/// The client user name environment variable name
32+
/// </summary>
33+
public static string UserNameEnvironmentVariableName
34+
{
35+
get => _userNameEnvironmentVariableName;
36+
set
37+
{
38+
if (string.IsNullOrEmpty(value))
39+
throw new ArgumentNullException(nameof(value));
40+
41+
_userNameEnvironmentVariableName = value;
42+
}
43+
}
44+
45+
/// <summary>
46+
/// The client password environment variable name
47+
/// </summary>
48+
public static string PasswordEnvironmentVariableName
49+
{
50+
get => _passwordEnvironmentVariableName;
51+
set
52+
{
53+
if (string.IsNullOrEmpty(value))
54+
throw new ArgumentNullException(nameof(value));
55+
56+
_passwordEnvironmentVariableName = value;
57+
}
58+
}
1659

1760
/// <summary>
1861
/// Gets or sets the connection string.
1962
/// </summary>
2063
/// <value>
2164
/// The connection string.
2265
/// </value>
23-
/// <exception cref="System.ArgumentNullException">value</exception>
66+
/// <exception cref="ArgumentNullException">value</exception>
2467
public static string? ConnectionString
2568
{
26-
get
69+
get => _connectionString ??= Environment.GetEnvironmentVariable(ConnectionStringEnvironmentVariableName);
70+
set
2771
{
28-
return _connectionString ??= Environment.GetEnvironmentVariable(ConnectionStringEnvironmentVariableName);
72+
if (string.IsNullOrEmpty(value))
73+
throw new ArgumentNullException(nameof(value));
74+
75+
_connectionString = value;
2976
}
30-
set => _connectionString = value ?? throw new ArgumentNullException(nameof(value));
3177
}
78+
79+
/// <summary>
80+
/// Gets or sets the user name.
81+
/// </summary>
82+
/// <value>
83+
/// The user name.
84+
/// </value>
85+
/// <exception cref="ArgumentNullException">value</exception>
86+
public static string? UserName => Environment.GetEnvironmentVariable(UserNameEnvironmentVariableName);
87+
88+
/// <summary>
89+
/// Gets or sets the password.
90+
/// </summary>
91+
/// <value>
92+
/// The password.
93+
/// </value>
94+
/// <exception cref="ArgumentNullException">value</exception>
95+
public static string? Password => Environment.GetEnvironmentVariable(PasswordEnvironmentVariableName);
3296
}

0 commit comments

Comments
 (0)