-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from bacongobbler/connection-string
implement connection string support
- Loading branch information
Showing
6 changed files
with
235 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Deislabs.Bindle; | ||
|
||
public class ConnectionInfo | ||
{ | ||
static readonly string[] serverAliases = { | ||
"server", | ||
"host", | ||
"data source", | ||
"datasource", | ||
"address", | ||
"addr", | ||
"network address", | ||
}; | ||
|
||
static readonly string[] sslModeAliases = { | ||
"sslmode", | ||
"ssl mode" | ||
}; | ||
|
||
static readonly string[] usernameAliases = { | ||
"user", | ||
"username" | ||
}; | ||
|
||
static readonly string[] passwordAliases = { | ||
"pass", | ||
"passwd", | ||
"password" | ||
}; | ||
|
||
private readonly Dictionary<string, string> keyValuePairs; | ||
|
||
public string BaseUri; | ||
|
||
public string UserName; | ||
|
||
public string Password; | ||
|
||
public SslMode? SslMode; | ||
|
||
public ConnectionInfo() | ||
{ | ||
keyValuePairs = new Dictionary<string, string>(); | ||
BaseUri = "http://localhost:8080/v1/"; | ||
UserName = String.Empty; | ||
Password = String.Empty; | ||
} | ||
|
||
public ConnectionInfo(string connectionString) | ||
{ | ||
keyValuePairs = GetKeyValuePairs(connectionString); | ||
|
||
BaseUri = GetValue(serverAliases); | ||
|
||
UserName = GetValue(usernameAliases); | ||
|
||
Password = GetValue(passwordAliases); | ||
|
||
try | ||
{ | ||
SslMode = Enum.Parse<SslMode>(GetValue(sslModeAliases), true); | ||
} | ||
catch (Exception) | ||
{ | ||
SslMode = null; | ||
} | ||
} | ||
|
||
private Dictionary<string, string> GetKeyValuePairs(string connectionString) | ||
{ | ||
return connectionString.Split(';') | ||
.Where(kvp => kvp.Contains('=')) | ||
.Select(kvp => kvp.Split(new char[] { '=' }, 2)) | ||
.ToDictionary(kvp => kvp[0].Trim(), | ||
kvp => kvp[1].Trim(), | ||
StringComparer.InvariantCultureIgnoreCase); | ||
} | ||
|
||
private string GetValue(string[] keyAliases) | ||
{ | ||
foreach (var alias in keyAliases) | ||
{ | ||
string? value; | ||
if (keyValuePairs.TryGetValue(alias, out value)) | ||
return value; | ||
} | ||
return string.Empty; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
namespace Deislabs.Bindle; | ||
|
||
public enum SslMode | ||
{ | ||
// I don't care about security, and I don't want to pay the overhead of encryption. | ||
Disable, | ||
// I don't care about security, but I will pay the overhead of encryption if the server insists on it. | ||
// TODO(bacongobbler): not implemented | ||
Allow, | ||
// I don't care about encryption, but I wish to pay the overhead of encryption if the server supports it. | ||
// TODO(bacongobbler): not implemented | ||
Prefer, | ||
// I want my data to be encrypted, and I accept the overhead. I trust that the network will make sure I always connect to the server I want. | ||
// TODO(bacongobbler): not implemented | ||
Require, | ||
// I want my data encrypted, and I accept the overhead. I want to be sure that I connect to a server that I trust. | ||
// TODO(bacongobbler): not implemented | ||
VerifyCA, | ||
// I want my data encrypted, and I accept the overhead. I want to be sure that I connect to a server I trust, and that it's the one I specify. | ||
// TODO(bacongobbler): not implemented | ||
VerifyFull, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.