public static Authenticator GetAuthenticator(string serviceName)
        {
            Authenticator authenticator = null;

            // Gather authentication-related properties from all the supported config sources:
            // - 1) Credential file
            // - 2) Environment variables
            // - 3) VCAP_SERVICES env variable
            Dictionary <string, string> authProps = new Dictionary <string, string>();

            // First check to see if this service has any properties defined in a credential file.
            authProps = CredentialUtils.GetFileCredentialsAsMap(serviceName);

            // If we didn't find any properties so far, then try the environment.
            if (authProps == null || authProps.Count == 0)
            {
                authProps = CredentialUtils.GetEnvCredentialsAsMap(serviceName);
            }

            // If we didn't find any properties so far, then try VCAP_SERVICES
            if (authProps == null || authProps.Count == 0)
            {
                authProps = CredentialUtils.GetVcapCredentialsAsMap(serviceName);
            }

            // Now create an authenticator from the map.
            if (authProps != null && authProps.Count > 0)
            {
                authenticator = CreateAuthenticator(authProps);
            }

            return(authenticator);
        }
        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);
        }
Пример #3
0
        public void TestGetFileCredentialsAsMap()
        {
            var fileCredentialsAsMap = CredentialUtils.GetFileCredentialsAsMap("assistant");

            Assert.IsNotNull(fileCredentialsAsMap);
        }
Пример #4
0
        public void TestCredentialOrder()
        {
            // 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         = { "TEST_SERVICE_LOCATION=working-directory" };
            var      directoryPathWorking = Directory.GetCurrentDirectory();
            var      docPathWorking       = Path.Combine(directoryPathWorking, "ibm-credentials.env");

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

            //  get props
            Dictionary <string, string> propsWorking = CredentialUtils.GetFileCredentialsAsMap("test_service");

            Assert.IsTrue(propsWorking.ContainsKey("LOCATION"));
            propsWorking.TryGetValue("LOCATION", out string envWorkingLocation);
            Assert.IsTrue(envWorkingLocation == "working-directory");

            //  create .env file in user set directory
            string[] lines = { "TEST_SERVICE_LOCATION=user-set-location" };

            var directoryPath = Environment.GetFolderPath(
                Environment.SpecialFolder.CommonApplicationData
                );

            var docPath = Path.Combine(directoryPath, "test-credentials.env");

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

            //  set user set env file path variable
            Environment.SetEnvironmentVariable("IBM_CREDENTIALS_FILE", docPath);

            //  get props
            Dictionary <string, string> props = CredentialUtils.GetFileCredentialsAsMap("test_service");

            Assert.IsTrue(props.ContainsKey("LOCATION"));
            props.TryGetValue("LOCATION", out string envLocation);
            Assert.IsTrue(envLocation == "user-set-location");

            //  delete created env files
            if (File.Exists(docPath))
            {
                File.Delete(docPath);
            }

            if (File.Exists(docPathWorking))
            {
                File.Delete(docPathWorking);
            }

            //  reset env variable
            Environment.SetEnvironmentVariable("IBM_CREDENTIALS_FILE", ibmCredFile);
        }