Skip to content

Commit

Permalink
fix: allow '=' character in environment config values
Browse files Browse the repository at this point in the history
  • Loading branch information
jorge-ibm committed May 12, 2020
1 parent c9d2076 commit 89db5eb
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 1 deletion.
113 changes: 113 additions & 0 deletions Tests/CredentialUtilsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class CredentialUtilsTests
public void TestGetVcapCredentialsAsMap()
{
var apikey = "bogus-apikey";
var service1_apikey = "V4HXmoUtMjohnsnow=KotN";
var tempVcapCredential = new Dictionary<string, List<VcapCredential>>();
var vcapCredential = new VcapCredential()
{
Expand All @@ -41,7 +42,18 @@ public void TestGetVcapCredentialsAsMap()
ApiKey = apikey
}
};

var vcapCredential2 = new VcapCredential()
{
Credentials = new Credential()
{
ApiKey = service1_apikey
}
};

vcapCredential2.Name = "equals_sign_test";
tempVcapCredential.Add("assistant", new List<VcapCredential>() { vcapCredential });
tempVcapCredential.Add("equals_sign_test", new List<VcapCredential>() { vcapCredential2 });

var vcapString = JsonConvert.SerializeObject(tempVcapCredential);
Environment.SetEnvironmentVariable("VCAP_SERVICES", vcapString);
Expand All @@ -53,6 +65,13 @@ public void TestGetVcapCredentialsAsMap()
Authenticator.PropNameApikey,
out string extractedKey);
Assert.IsTrue(extractedKey == apikey);

vcapCredentaialsAsMap = CredentialUtils.GetVcapCredentialsAsMap("equals_sign_test");
Assert.IsNotNull(vcapCredentaialsAsMap);
vcapCredentaialsAsMap.TryGetValue(
Authenticator.PropNameApikey,
out string extractedKey2);
Assert.IsTrue(extractedKey2 == service1_apikey);
}

[Test]
Expand Down Expand Up @@ -414,5 +433,99 @@ public void TestGetServiceProperties()

Assert.IsNotNull(serviceProperties);
}

[Test]
public void TestGetFileCredentialsAsMapService1()
{
// store and clear user set env variable
string ibmCredFile = Environment.GetEnvironmentVariable("IBM_CREDENTIALS_FILE");
Environment.SetEnvironmentVariable("IBM_CREDENTIALS_FILE", "");

// create .env file in current directory
string[] linesWorking = { "SERVICE_1_AUTH_TYPE=iam",
"SERVICE_1_APIKEY=V4HXmoUtMjohnsnow=KotN",
"SERVICE_1_CLIENT_ID=somefake========id",
"SERVICE_1_CLIENT_SECRET===my-client-secret==",
"SERVICE_1_AUTH_URL=https://iamhost/iam/api=",
"SERVICE_1_AUTH_DISABLE_SSL=" };
var directoryPath = Directory.GetCurrentDirectory();
var credsFile = Path.Combine(directoryPath, "ibm-credentials.env");

using (StreamWriter outputFile = new StreamWriter(credsFile))
{
foreach (string line in linesWorking)
{
outputFile.WriteLine(line);
}
}

// get props
Dictionary<string, string> propsWorking = CredentialUtils.GetFileCredentialsAsMap("service_1");
Assert.IsNotNull(propsWorking);
Assert.AreEqual(propsWorking["AUTH_TYPE"], "iam");
Assert.AreEqual(propsWorking["APIKEY"], "V4HXmoUtMjohnsnow=KotN");
Assert.AreEqual(propsWorking["CLIENT_ID"], "somefake========id");
Assert.AreEqual(propsWorking["CLIENT_SECRET"], "==my-client-secret==");
Assert.AreEqual(propsWorking["AUTH_URL"], "https://iamhost/iam/api=");
Assert.IsFalse(propsWorking.ContainsKey("DISABLE_SSL"));
// delete created env files
if (File.Exists(credsFile))
{
File.Delete(credsFile);
}
// reset env variable
Environment.SetEnvironmentVariable("IBM_CREDENTIALS_FILE", ibmCredFile);
}

[Test]
public void TestGetEnvCredentialsAsMapService1()
{
var apikey = "V4HXmoUtMjohnsnow=KotN";
var authType = "iam";
var clientId = "somefake========id";
var clientIdSecret = "==my-client-secret==";
var authUrl = "https://iamhost/iam/api=";

Environment.SetEnvironmentVariable(
"SERVICE_1_" + Authenticator.PropNameApikey,
apikey);
Environment.SetEnvironmentVariable(
"SERVICE_1_" + Authenticator.PropNameAuthType,
authType);
Environment.SetEnvironmentVariable(
"SERVICE_1_" + Authenticator.PropNameClientId,
clientId);
Environment.SetEnvironmentVariable(
"SERVICE_1_" + Authenticator.PropNameClientSecret,
clientIdSecret);
Environment.SetEnvironmentVariable(
"SERVICE_1_" + Authenticator.PropNameUrl,
authUrl);
// get props
Dictionary<string, string> props = CredentialUtils.GetEnvCredentialsAsMap("service_1");
Assert.IsNotNull(props);
Assert.AreEqual(props["AUTH_TYPE"], authType);
Assert.AreEqual(props["APIKEY"], apikey);
Assert.AreEqual(props["CLIENT_ID"], clientId);
Assert.AreEqual(props["CLIENT_SECRET"], clientIdSecret);
Assert.AreEqual(props["AUTH_URL"], authUrl);

// delete created env files
Environment.SetEnvironmentVariable(
"SERVICE_1_" + Authenticator.PropNameApikey,
null);
Environment.SetEnvironmentVariable(
"SERVICE_1_" + Authenticator.PropNameAuthType,
null);
Environment.SetEnvironmentVariable(
"SERVICE_1_" + Authenticator.PropNameClientId,
null);
Environment.SetEnvironmentVariable(
"SERVICE_1_" + Authenticator.PropNameClientSecret,
null);
Environment.SetEnvironmentVariable(
"SERVICE_1_" + Authenticator.PropNameUrl,
null);
}
}
}
2 changes: 1 addition & 1 deletion Utilities/CredentialUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ protected static Dictionary<string, string> ParseCredentials(string serviceName,
}

string[] stringSeparators = new string[] { "=" };
List<string> lineTokens = new List<string>(line.Split(stringSeparators, StringSplitOptions.None));
List<string> lineTokens = new List<string>(line.Split(stringSeparators, 2, StringSplitOptions.None));
if (lineTokens.Count != 2)
{
continue;
Expand Down

0 comments on commit 89db5eb

Please # to comment.