public void TestGetVcapCredentialsAsMapEmptySvcName()
        {
            var tempVcapCredential = new Dictionary <string, List <VcapCredential> >();
            //create credential entries for first service entry
            var vcapCredential = new VcapCredential()
            {
                Credentials = new Credential()
                {
                    ApiKey = "assistantV1apikey"
                }
            };

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

            var vcapString = JsonConvert.SerializeObject(tempVcapCredential);

            Environment.SetEnvironmentVariable("VCAP_SERVICES", vcapString);
            Assert.IsNotNull(Environment.GetEnvironmentVariable("VCAP_SERVICES"));

            var vcapCredentaialsAsMap = CredentialUtils.GetVcapCredentialsAsMap("");

            Assert.IsNotNull(vcapCredentaialsAsMap);
            Assert.IsTrue(vcapCredentaialsAsMap.Count == 0);
        }
예제 #2
0
        /// <summary>
        /// Returns a Map containing properties found within the VCAP_SERVICES environment variable that are associated
        /// with the specified cloud service.
        /// </summary>
        /// <param name="serviceName">The name of the cloud service whose properties should be retrieved</param>
        /// <returns>A Dictionary containing the properties</returns>
        public static Dictionary <string, string> GetVcapCredentialsAsMap(string serviceName)
        {
            Dictionary <string, string> props = new Dictionary <string, string>();
            var vcapServices = GetVcapServices();

            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.
            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);
        }
        public void TestGetServiceProperties()
        {
            var apikey             = "bogus-apikey";
            var tempVcapCredential = new Dictionary <string, List <VcapCredential> >();
            var vcapCredential     = new VcapCredential()
            {
                Credentials = new Credential()
                {
                    ApiKey = apikey
                }
            };

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

            var vcapString = JsonConvert.SerializeObject(tempVcapCredential);

            Environment.SetEnvironmentVariable("VCAP_SERVICES", vcapString);
            Assert.IsNotNull(Environment.GetEnvironmentVariable("VCAP_SERVICES"));

            var serviceProperties = CredentialUtils.GetServiceProperties("assistant");

            Assert.IsNotNull(serviceProperties);
        }
        public void TestGetVcapCredentialsAsMapMissingNameField()
        {
            var tempVcapCredential = new Dictionary <string, List <VcapCredential> >();
            //create credential entries for first service entry
            var vcapCredential = new VcapCredential()
            {
                Credentials = new Credential()
                {
                    ApiKey = "assistantV1apikey"
                }
            };

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

            var vcapString = JsonConvert.SerializeObject(tempVcapCredential);

            Environment.SetEnvironmentVariable("VCAP_SERVICES", vcapString);
            Assert.IsNotNull(Environment.GetEnvironmentVariable("VCAP_SERVICES"));

            var vcapCredentaialsAsMap = CredentialUtils.GetVcapCredentialsAsMap("assistant");

            Assert.IsNotNull(vcapCredentaialsAsMap);
            vcapCredentaialsAsMap.TryGetValue(
                Authenticator.PropNameApikey,
                out string extractedKey);
            Assert.IsTrue(extractedKey == "assistantV1apikey");
        }
예제 #5
0
        public void GetAuthenticator()
        {
            var apikey             = "bogus-apikey";
            var tempVcapCredential = new Dictionary <string, List <VcapCredential> >();
            var vcapCredential     = new VcapCredential()
            {
                Credentials = new Credential()
                {
                    ApiKey = apikey
                }
            };

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

            var vcapString = JsonConvert.SerializeObject(tempVcapCredential);

            Environment.SetEnvironmentVariable("VCAP_SERVICES", vcapString);
            Assert.IsNotNull(Environment.GetEnvironmentVariable("VCAP_SERVICES"));

            var authenticator = ConfigBasedAuthenticatorFactory.GetAuthenticator("assistant");

            Assert.IsNotNull(authenticator);
            Assert.IsNotNull(authenticator.AuthenticationType);
        }
        public void TestGetVcapCredentialsAsMapFromInnerEntry()
        {
            var tempVcapCredential = new Dictionary <string, List <VcapCredential> >();
            //create credential entries
            var vcapCredential = new VcapCredential()
            {
                Credentials = new Credential()
                {
                    ApiKey = "fakeapikey"
                }
            };

            vcapCredential.Name = "assistant1";

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

            vcapCredential2.Name = "assistant2";

            var vcapCredential3 = new VcapCredential()
            {
                Credentials = new Credential()
                {
                    ApiKey = "fakeapikey3"
                }
            };

            vcapCredential3.Name = "assistant3";
            //map to a single key
            tempVcapCredential.Add("assistant", new List <VcapCredential>()
            {
                vcapCredential, vcapCredential2, vcapCredential3
            });

            var vcapString = JsonConvert.SerializeObject(tempVcapCredential);

            Environment.SetEnvironmentVariable("VCAP_SERVICES", vcapString);
            Assert.IsNotNull(Environment.GetEnvironmentVariable("VCAP_SERVICES"));

            var vcapCredentaialsAsMap = CredentialUtils.GetVcapCredentialsAsMap("assistant2");

            Assert.IsNotNull(vcapCredentaialsAsMap);
            vcapCredentaialsAsMap.TryGetValue(
                Authenticator.PropNameApikey,
                out string extractedKey);
            Assert.IsTrue(extractedKey == "fakeapikey2");
        }
        public void TestGetVcapCredentialsAsMap()
        {
            var apikey             = "bogus-apikey";
            var service1_apikey    = "V4HXmoUtMjohnsnow=KotN";
            var tempVcapCredential = new Dictionary <string, List <VcapCredential> >();
            var vcapCredential     = new VcapCredential()
            {
                Credentials = new Credential()
                {
                    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);
            Assert.IsNotNull(Environment.GetEnvironmentVariable("VCAP_SERVICES"));

            var vcapCredentaialsAsMap = CredentialUtils.GetVcapCredentialsAsMap("assistant");

            Assert.IsNotNull(vcapCredentaialsAsMap);
            vcapCredentaialsAsMap.TryGetValue(
                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);
        }
예제 #8
0
        public void TestGetVcapCredentialsAsMapVcapNotSet()
        {
            var tempVcapCredential = new Dictionary <string, List <VcapCredential> >();
            //create credential entries for first service entry
            var vcapCredential = new VcapCredential()
            {
                Credentials = new Credential()
                {
                    ApiKey = "assistantV1apikey"
                }
            };

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

            var vcapCredentaialsAsMap = CredentialUtils.GetVcapCredentialsAsMap("fake_entry");
            // Assert.IsNotNull(vcapCredentaialsAsMap);
            // Assert.IsTrue(vcapCredentaialsAsMap.Count == 0);
        }
        public void TestGetVcapCredentialsAsMapNoMatchingName()
        {
            var tempVcapCredential = new Dictionary <string, List <VcapCredential> >();
            //create credential entries for first service entry
            var vcapCredential = new VcapCredential()
            {
                Credentials = new Credential()
                {
                    ApiKey = "assistantV1apikey"
                }
            };

            vcapCredential.Name = "assistantV1";
            var vcapCredential2 = new VcapCredential()
            {
                Credentials = new Credential()
                {
                    ApiKey = "assistantV1apikeyCopy"
                }
            };

            vcapCredential2.Name = "assistantV1Copy";
            //map to creds to first service
            tempVcapCredential.Add("no_matching_name", new List <VcapCredential>()
            {
                vcapCredential, vcapCredential2
            });

            //create credential entries for second service entry
            var vcapCredential3 = new VcapCredential()
            {
                Credentials = new Credential()
                {
                    ApiKey = "assistantV2apikey"
                }
            };

            vcapCredential3.Name = "assistantV2";

            var vcapCredential4 = new VcapCredential()
            {
                Credentials = new Credential()
                {
                    ApiKey = "assistantV2apikeyCopy"
                }
            };

            vcapCredential4.Name = "assistantV2Copy";

            //map to second service
            tempVcapCredential.Add("assistant", new List <VcapCredential>()
            {
                vcapCredential3, vcapCredential4
            });

            var vcapString = JsonConvert.SerializeObject(tempVcapCredential);

            Environment.SetEnvironmentVariable("VCAP_SERVICES", vcapString);
            Assert.IsNotNull(Environment.GetEnvironmentVariable("VCAP_SERVICES"));

            var vcapCredentaialsAsMap = CredentialUtils.GetVcapCredentialsAsMap("no_matching_name");

            Assert.IsNotNull(vcapCredentaialsAsMap);
            vcapCredentaialsAsMap.TryGetValue(
                Authenticator.PropNameApikey,
                out string extractedKey);
            Assert.IsTrue(extractedKey == "assistantV1apikey");
        }