Skip to content

Commit a259a29

Browse files
committed
[#8] [add] factory methods based impl
1 parent d0a0be4 commit a259a29

File tree

6 files changed

+233
-97
lines changed

6 files changed

+233
-97
lines changed

src/Etcd.Microsoft.Extensions.Configuration/Auth/Credentials.cs

Lines changed: 177 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,50 @@
33
namespace Etcd.Microsoft.Extensions.Configuration.Auth;
44

55
/// <summary>
6-
/// Provides credentials
6+
/// Provides credentials.
77
/// </summary>
88
/// <seealso cref="ICredentials" />
99
public class Credentials : ICredentials
1010
{
11+
private const string DefaultUserNameEnvironmentVariableName = "ETCD_CLIENT_USER_NAME";
12+
private const string DefaultPasswordEnvironmentVariableName = "ETCD_CLIENT_PASSWORD";
13+
1114
/// <summary>
1215
/// Initializes a new instance of the <see cref="Credentials"/> class.
1316
/// </summary>
1417
/// <param name="userName">Name of the user.</param>
1518
/// <param name="password">The password.</param>
19+
/// <param name="userNameSource">The source of the username.</param>
20+
/// <param name="passwordSource">The source of the password.</param>
21+
/// <param name="information">The information about the credentials.</param>
1622
/// <exception cref="ArgumentException">
1723
/// Value cannot be null or empty. - userName
1824
/// or
1925
/// Value cannot be null or empty. - password
2026
/// </exception>
21-
public Credentials(string userName, string password)
27+
public Credentials(string userName, string password,
28+
CredentialsSource userNameSource = CredentialsSource.Code,
29+
CredentialsSource passwordSource = CredentialsSource.Code,
30+
string? information = null)
2231
{
2332
if (string.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", nameof(userName));
2433
if (string.IsNullOrEmpty(password)) throw new ArgumentException("Value cannot be null or empty.", nameof(password));
2534

2635
UserName = userName;
2736
Password = password;
37+
UserNameSource = userNameSource;
38+
PasswordSource = passwordSource;
39+
Information = information;
2840
}
41+
/// <summary>
42+
/// Gets the source of the user name.
43+
/// </summary>
44+
public CredentialsSource UserNameSource { get; }
45+
46+
/// <summary>
47+
/// Gets the source of the password.
48+
/// </summary>
49+
public CredentialsSource PasswordSource { get; }
2950

3051
/// <summary>
3152
/// Gets the name of the user.
@@ -42,4 +63,158 @@ public Credentials(string userName, string password)
4263
/// The password.
4364
/// </value>
4465
public string Password { get; }
66+
67+
/// <summary>
68+
/// Gets the information about the credentials.
69+
/// </summary>
70+
public string? Information { get; private set; }
71+
72+
/// <summary>
73+
/// Gets the string representation of the credentials.
74+
/// </summary>
75+
override public string ToString() => Information ?? "Code based credentials";
76+
77+
/// <summary>
78+
/// Creates a new credentials instance overriding values from environment variables if they are exists.
79+
/// </summary>
80+
/// <param name="userName">The user name.</param>
81+
/// <param name="password">The password.</param>
82+
/// <param name="userNameEnvironmentVariableName">The name of the user name environment variable.</param>
83+
/// <param name="passwordEnvironmentVariableName">The name of the password environment variable.</param>
84+
/// <exception cref="EtcdException">The etcd user name or password are not provided via code and not found in the environment variable `{userNameEnvironmentVariableName}`.</exception>
85+
public static ICredentials WithOverrideFromEnvironmentVariables(
86+
string userName,
87+
string password,
88+
string userNameEnvironmentVariableName = DefaultUserNameEnvironmentVariableName,
89+
string passwordEnvironmentVariableName = DefaultPasswordEnvironmentVariableName)
90+
{
91+
var userNameSource = CredentialsSource.Code;
92+
var passwordSource = CredentialsSource.Code;
93+
94+
var environmentUserName = Environment.GetEnvironmentVariable(userNameEnvironmentVariableName);
95+
var environmentPassword = Environment.GetEnvironmentVariable(passwordEnvironmentVariableName);
96+
97+
if (!string.IsNullOrEmpty(environmentUserName))
98+
{
99+
userName = environmentUserName;
100+
101+
userNameSource = CredentialsSource.EnvironmentVariables;
102+
}
103+
104+
if (string.IsNullOrEmpty(userName))
105+
throw new EtcdException($"Etcd user name is not provided via code and not found in the environment variable `{userNameEnvironmentVariableName}`.");
106+
107+
if (!string.IsNullOrEmpty(environmentPassword))
108+
{
109+
password = environmentPassword;
110+
111+
passwordSource = CredentialsSource.EnvironmentVariables;
112+
}
113+
114+
if (string.IsNullOrEmpty(password))
115+
throw new EtcdException($"Etcd password is not provided via code and not found in the environment variable `{passwordEnvironmentVariableName}`.");
116+
117+
return new Credentials(userName, password, userNameSource, passwordSource,
118+
FormatInformation(
119+
userNameSource,
120+
passwordSource,
121+
userNameEnvironmentVariableName,
122+
passwordEnvironmentVariableName));
123+
}
124+
125+
/// <summary>
126+
/// Creates a new credentials instance overriding values from environment variables if they are exists.
127+
/// </summary>
128+
/// <param name="userName">The user name.</param>
129+
/// <param name="password">The password.</param>
130+
/// <param name="passwordEnvironmentVariableName">The name of the password environment variable.</param>
131+
/// <exception cref="EtcdException">The etcd user name or password are not provided via code and not found in the environment variable `{userNameEnvironmentVariableName}`.</exception>
132+
public static ICredentials WithOverrideFromEnvironmentVariables(
133+
string userName,
134+
string password,
135+
string passwordEnvironmentVariableName = DefaultPasswordEnvironmentVariableName)
136+
{
137+
var userNameSource = CredentialsSource.Code;
138+
var passwordSource = CredentialsSource.Code;
139+
140+
var environmentPassword = Environment.GetEnvironmentVariable(passwordEnvironmentVariableName);
141+
142+
if (string.IsNullOrEmpty(userName))
143+
throw new EtcdException($"Etcd user name is not provided.");
144+
145+
if (!string.IsNullOrEmpty(environmentPassword))
146+
{
147+
password = environmentPassword;
148+
149+
passwordSource = CredentialsSource.EnvironmentVariables;
150+
}
151+
152+
if (string.IsNullOrEmpty(password))
153+
throw new EtcdException($"Etcd password is not provided via code and not found in the environment variable `{passwordEnvironmentVariableName}`.");
154+
155+
return new Credentials(userName, password, userNameSource, passwordSource,
156+
FormatInformation(
157+
userNameSource,
158+
passwordSource,
159+
null,
160+
passwordEnvironmentVariableName));
161+
}
162+
163+
164+
/// <summary>
165+
/// Creates a new credentials instance from environment variables.
166+
/// </summary>
167+
/// <param name="userNameEnvironmentVariableName">The name of the user name environment variable.</param>
168+
/// <param name="passwordEnvironmentVariableName">The name of the password environment variable.</param>
169+
/// <exception cref="EtcdException">The etcd user name or password are not provided via code and not found in the environment variable `{userNameEnvironmentVariableName}`.</exception>
170+
public static ICredentials FromEnvironmentVariables(
171+
string userNameEnvironmentVariableName = DefaultUserNameEnvironmentVariableName,
172+
string passwordEnvironmentVariableName = DefaultPasswordEnvironmentVariableName)
173+
{
174+
var userNameSource = CredentialsSource.EnvironmentVariables;
175+
var passwordSource = CredentialsSource.EnvironmentVariables;
176+
177+
var userName = Environment.GetEnvironmentVariable(userNameEnvironmentVariableName);
178+
var password = Environment.GetEnvironmentVariable(passwordEnvironmentVariableName);
179+
180+
if (string.IsNullOrEmpty(userName))
181+
throw new EtcdException($"Etcd user name is not found in the environment variable `{userNameEnvironmentVariableName}`.");
182+
183+
if (string.IsNullOrEmpty(password))
184+
throw new EtcdException($"Etcd password is not found in the environment variable `{passwordEnvironmentVariableName}`.");
185+
186+
return new Credentials(userName, password, userNameSource, passwordSource,
187+
FormatInformation(
188+
userNameSource,
189+
passwordSource,
190+
userNameEnvironmentVariableName,
191+
passwordEnvironmentVariableName));
192+
}
193+
194+
private static string FormatInformation(
195+
CredentialsSource userNameSource,
196+
CredentialsSource passwordSource,
197+
string? userNameEnvironmentVariableName = null,
198+
string? passwordEnvironmentVariableName = null) =>
199+
FormatUserNameInformation(userNameSource, userNameEnvironmentVariableName) + ", " + FormatPassword(passwordSource, passwordEnvironmentVariableName);
200+
201+
private static string FormatUserNameInformation(CredentialsSource userNameSource, string? userNameEnvironmentVariableName = null)
202+
{
203+
var result = $"UserName source: {userNameSource}";
204+
205+
if (userNameSource == CredentialsSource.EnvironmentVariables)
206+
result += $", variable name: {userNameEnvironmentVariableName}";
207+
208+
return result;
209+
}
210+
211+
private static string FormatPassword(CredentialsSource passwordSource, string? passwordEnvironmentVariableName = null)
212+
{
213+
var result = $"password source: {passwordSource}";
214+
215+
if (passwordSource == CredentialsSource.EnvironmentVariables)
216+
result += $", variable name: {passwordEnvironmentVariableName}";
217+
218+
return result;
219+
}
45220
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace Etcd.Microsoft.Extensions.Configuration.Auth
2+
{
3+
/// <summary>
4+
/// Possible sources for credentials.
5+
/// </summary>
6+
public enum CredentialsSource
7+
{
8+
/// <summary>
9+
/// Credentials provided via code.
10+
/// </summary>
11+
Code = 0,
12+
/// <summary>
13+
/// Credentials provided via environment variables.
14+
/// </summary>
15+
EnvironmentVariables = 1
16+
}
17+
}

src/Etcd.Microsoft.Extensions.Configuration/Auth/EnvironmentCredentialsFactory.cs

Lines changed: 0 additions & 22 deletions
This file was deleted.

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

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

101-
credentials = EnvironmentCredentialsFactory.TryCreate() ?? credentials;
102-
103101
var clientFactory = new EtcdClientFactory(settings);
104102
var client = new EtcdKeyValueClient(clientFactory, credentials, enableWatch, unwatchOnDispose);
105103

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

Lines changed: 5 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -7,55 +7,12 @@ 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-
1510
/// <summary>
1611
/// The connection string environment variable name
1712
/// </summary>
18-
public static string ConnectionStringEnvironmentVariableName
19-
{
20-
get => _connectionStringEnvironmentVariableName;
21-
set
22-
{
23-
if (string.IsNullOrEmpty(value))
24-
throw new ArgumentNullException(nameof(value));
25-
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));
13+
public const string ConnectionStringEnvironmentVariableName = "ETCD_CLIENT_CONNECTION_STRING";
5514

56-
_passwordEnvironmentVariableName = value;
57-
}
58-
}
15+
private static string? _connectionString;
5916

6017
/// <summary>
6118
/// Gets or sets the connection string.
@@ -66,31 +23,10 @@ public static string PasswordEnvironmentVariableName
6623
/// <exception cref="ArgumentNullException">value</exception>
6724
public static string? ConnectionString
6825
{
69-
get => _connectionString ??= Environment.GetEnvironmentVariable(ConnectionStringEnvironmentVariableName);
70-
set
26+
get
7127
{
72-
if (string.IsNullOrEmpty(value))
73-
throw new ArgumentNullException(nameof(value));
74-
75-
_connectionString = value;
28+
return _connectionString ??= Environment.GetEnvironmentVariable(ConnectionStringEnvironmentVariableName);
7629
}
30+
set => _connectionString = value ?? throw new ArgumentNullException(nameof(value));
7731
}
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);
9632
}

0 commit comments

Comments
 (0)