Skip to content

Commit

Permalink
feat(IBMService): enhance vcap parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
jorge-ibm committed Dec 4, 2019
1 parent aab08b4 commit 74cff01
Show file tree
Hide file tree
Showing 2 changed files with 427 additions and 70 deletions.
128 changes: 74 additions & 54 deletions src/IBM.Cloud.SDK.Core/Util/CredentialUtils.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/**
* Copyright 2018 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
/**
* Copyright 2018 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

using IBM.Cloud.SDK.Core.Authentication;
Expand Down Expand Up @@ -83,27 +83,42 @@ private static VcapCredential GetVcapCredentialsObject(Dictionary<string, List<V
{
return null;
}

foreach (KeyValuePair<string, List<VcapCredential>> kvp in vcapServices)
{
if (kvp.Key.StartsWith(serviceName))
{
List<VcapCredential> credentials = kvp.Value;
if (!string.IsNullOrEmpty(plan))
{
foreach (VcapCredential credential in credentials)
{
if (credential.Plan.ToLower() == plan.ToLower())
{
return credential;
}
}
}
else
{
return credentials[0];
}
}
// 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;
}
if (!string.IsNullOrEmpty(plan))
{
foreach (VcapCredential credential in credentials)
{
if (credential.Plan.ToLower() == plan.ToLower())
{
return credential;
}
}
}
else
{
return credentials[0];
}
}

return null;
Expand Down Expand Up @@ -248,24 +263,29 @@ private static List<string> GetFileContents(string file)
if (vcapServices == null || vcapServices.Count == 0)
{
return props;
}

AddToDictionary(props, Authenticator.PropNameUsername, GetVcapCredentialsObject(vcapServices, serviceName).Credentials.Username);
AddToDictionary(props, Authenticator.PropNamePassword, GetVcapCredentialsObject(vcapServices, serviceName).Credentials.Password);
AddToDictionary(props, Authenticator.PropNameUrl, GetVcapCredentialsObject(vcapServices, serviceName).Credentials.Url);

// For the IAM apikey, the "apikey" property has higher precedence than "iam_apikey".
AddToDictionary(props, Authenticator.PropNameApikey, GetVcapCredentialsObject(vcapServices, serviceName).Credentials.IamApikey);
AddToDictionary(props, Authenticator.PropNameApikey, GetVcapCredentialsObject(vcapServices, serviceName).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);
}
// 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);
}
}

return props;
Expand Down

0 comments on commit 74cff01

Please sign in to comment.