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 11, 2020
1 parent cc2b18c commit 761da72
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 48 deletions.
88 changes: 44 additions & 44 deletions src/IBM.Cloud.SDK.Core/Util/CredentialUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,29 +84,29 @@ private static VcapCredential GetVcapCredentialsObject(Dictionary<string, List<V
return null;
}
// search the inner entries of the vcap for the matching serviceName
foreach (KeyValuePair<string, List<VcapCredential>> kvp in vcapServices)
{
List<VcapCredential> item = kvp.Value;
if (item != null && item.Count > 0)
{
foreach (VcapCredential credential in item)
{
if (credential.Name == serviceName)
{
return credential;
}
}
}
}
// Second, try to find a service list with the specified key.
if (vcapServices.TryGetValue(serviceName, out List<VcapCredential> credentials))
{
if (credentials == null || credentials.Count == 0)
{
return null;
}

return credentials[0];
foreach (KeyValuePair<string, List<VcapCredential>> kvp in vcapServices)
{
List<VcapCredential> item = kvp.Value;
if (item != null && item.Count > 0)
{
foreach (VcapCredential credential in item)
{
if (credential.Name == serviceName)
{
return credential;
}
}
}
}
// Second, try to find a service list with the specified key.
if (vcapServices.TryGetValue(serviceName, out List<VcapCredential> credentials))
{
if (credentials == null || credentials.Count == 0)
{
return null;
}

return credentials[0];
}

return null;
Expand Down Expand Up @@ -251,29 +251,29 @@ private static List<string> GetFileContents(string file)
if (vcapServices == null || vcapServices.Count == 0)
{
return props;
}
// Retrieve the vcap service entry for the specific key and name, then copy its values to the dictionary.
}
// Retrieve the vcap service entry for the specific key and name, then copy its values to the dictionary.
VcapCredential serviceCredentials = GetVcapCredentialsObject(vcapServices, serviceName);

if (serviceCredentials != null)
{
AddToDictionary(props, Authenticator.PropNameUsername, serviceCredentials.Credentials.Username);
AddToDictionary(props, Authenticator.PropNamePassword, serviceCredentials.Credentials.Password);
AddToDictionary(props, Authenticator.PropNameUrl, serviceCredentials.Credentials.Url);

// For the IAM apikey, the "apikey" property has higher precedence than "iam_apikey".
AddToDictionary(props, Authenticator.PropNameApikey, serviceCredentials.Credentials.IamApikey);
AddToDictionary(props, Authenticator.PropNameApikey, serviceCredentials.Credentials.ApiKey);

// Try to guess at the auth type based on the properties found.
if (props.ContainsKey(Authenticator.PropNameApikey))
{
AddToDictionary(props, Authenticator.PropNameAuthType, Authenticator.AuthTypeIam);
}
else if (props.ContainsKey(Authenticator.PropNameUsername) || props.ContainsKey(Authenticator.PropNamePassword))
{
AddToDictionary(props, Authenticator.PropNameAuthType, Authenticator.AuthTypeBasic);
}
{
AddToDictionary(props, Authenticator.PropNameUsername, serviceCredentials.Credentials.Username);
AddToDictionary(props, Authenticator.PropNamePassword, serviceCredentials.Credentials.Password);
AddToDictionary(props, Authenticator.PropNameUrl, serviceCredentials.Credentials.Url);

// For the IAM apikey, the "apikey" property has higher precedence than "iam_apikey".
AddToDictionary(props, Authenticator.PropNameApikey, serviceCredentials.Credentials.IamApikey);
AddToDictionary(props, Authenticator.PropNameApikey, serviceCredentials.Credentials.ApiKey);

// Try to guess at the auth type based on the properties found.
if (props.ContainsKey(Authenticator.PropNameApikey))
{
AddToDictionary(props, Authenticator.PropNameAuthType, Authenticator.AuthTypeIam);
}
else if (props.ContainsKey(Authenticator.PropNameUsername) || props.ContainsKey(Authenticator.PropNamePassword))
{
AddToDictionary(props, Authenticator.PropNameAuthType, Authenticator.AuthTypeBasic);
}
}

return props;
Expand Down Expand Up @@ -340,7 +340,7 @@ private static void AddToDictionary(Dictionary<string, string> dictionary, strin
}

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
123 changes: 119 additions & 4 deletions test/IBM.Cloud.SDK.Core.Tests/CredentialUtilsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,49 @@ public void TestGetFileCredentialsAsMap()
Assert.IsNotNull(fileCredentialsAsMap);
}

[TestMethod]
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);
}

[TestMethod]
public void TestGetEnvCredentialsAsMap()
{
Expand All @@ -75,10 +118,62 @@ public void TestGetEnvCredentialsAsMap()
Assert.IsTrue(extractedKey == apikey);
}

[TestMethod]
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);
}

[TestMethod]
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 @@ -87,7 +182,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 @@ -99,6 +205,15 @@ 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);


}

[TestMethod]
Expand Down Expand Up @@ -406,8 +521,8 @@ public void TestGetVcapCredentialsAsMapEmptySvcName()
}
};
vcapCredential.Name = "assistantV1";
tempVcapCredential.Add("assistant", new List<VcapCredential>() { vcapCredential });

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

var vcapString = JsonConvert.SerializeObject(tempVcapCredential);
Environment.SetEnvironmentVariable("VCAP_SERVICES", vcapString);
Assert.IsNotNull(Environment.GetEnvironmentVariable("VCAP_SERVICES"));
Expand All @@ -427,8 +542,8 @@ public void TestGetVcapCredentialsAsMapNoCreds()

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

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

var vcapString = JsonConvert.SerializeObject(tempVcapCredential);
Environment.SetEnvironmentVariable("VCAP_SERVICES", vcapString);
Assert.IsNotNull(Environment.GetEnvironmentVariable("VCAP_SERVICES"));
Expand Down

0 comments on commit 761da72

Please sign in to comment.