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 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);
        }
Ejemplo n.º 3
0
        public void TestGetEnvCredentialsAsMap()
        {
            var apikey = "bogus-apikey";

            Environment.SetEnvironmentVariable(
                "ASSISTANT_" + Authenticator.PropNameApikey,
                apikey);
            var envCredentialsAsMap = CredentialUtils.GetEnvCredentialsAsMap("assistant");

            Assert.IsNotNull(envCredentialsAsMap);
            Assert.IsTrue(envCredentialsAsMap.ContainsKey(Authenticator.PropNameApikey));
            Assert.IsTrue(envCredentialsAsMap.ContainsValue(apikey));
            envCredentialsAsMap.TryGetValue(
                Authenticator.PropNameApikey,
                out string extractedKey);
            Assert.IsTrue(extractedKey == apikey);
        }