Beispiel #1
0
        public static string GetToken()
        {
            Token         token;
            ConfVariables confVariables = GetConfigurationVariables();

            if (confVariables == null)
            {
                return("");
            }
            if ((confVariables.TenantID == "") || (confVariables.ClientID == "") || (confVariables.Password == ""))
            {
                return("");
            }
            string getTokenString = "/oauth2/token?api-version=";
            string endPoint       = confVariables.Auth_Url + "/" + confVariables.TenantID + getTokenString + confVariables.ApiVersion;
            var    client         = new RestClient(endPoint)
            {
                Method   = HttpVerb.POST,
                PostData = "grant_type=client_credentials&resource=" + confVariables.Resource + "&client_id=" + confVariables.ClientID + "&client_secret=" + confVariables.Password
            };
            var json = client.MakeRequest();

            token = JsonConvert.DeserializeObject <Token>(json);
            return(token.Access_token);
        }
Beispiel #2
0
        public static ConfVariables GetConfigurationVariables()
        {
            try
            {
                ConfVariables confVariables = new ConfVariables();
                string[]      bufferedFile  = File.ReadAllLines("BlackICEconnect.cnf");
                foreach (string line in bufferedFile)
                {
                    var result = from Match match in Regex.Matches(line, "\"([^\"]*)\"") select match.ToString();

                    if ((line.Contains("AUTH_URL")) && !line.Contains("#"))
                    {
                        confVariables.Auth_Url = result.First().ToString().Substring(1, result.First().ToString().Length - 2);
                    }
                    if ((line.Contains("HOST")) && !line.Contains("#"))
                    {
                        confVariables.Host = result.First().ToString().Substring(1, result.First().ToString().Length - 2);
                    }
                    if ((line.Contains("AUTH_APIVERSION")) && !line.Contains("#"))
                    {
                        confVariables.Auth_ApiVersion = result.First().ToString().Substring(1, result.First().ToString().Length - 2);
                    }
                    if ((line.Contains("TENANTID")) && !line.Contains("#"))
                    {
                        confVariables.TenantID = result.First().ToString().Substring(1, result.First().ToString().Length - 2);
                    }
                    if ((line.Contains("CLIENTID")) && !line.Contains("#"))
                    {
                        confVariables.ClientID = result.First().ToString().Substring(1, result.First().ToString().Length - 2);
                    }
                    if ((line.Contains("PASSWORD")) && !line.Contains("#"))
                    {
                        confVariables.Password = result.First().ToString().Substring(1, result.First().ToString().Length - 2);
                    }
                    if ((line.Contains("RESOURCE")) && !line.Contains("#"))
                    {
                        confVariables.Resource = result.First().ToString().Substring(1, result.First().ToString().Length - 2);
                    }
                }
                if (confVariables.Auth_Url == null)
                {
                    confVariables.Auth_Url = "https://login.windows.net";
                }
                if (confVariables.Auth_ApiVersion == null)
                {
                    confVariables.Auth_ApiVersion = "1.0";
                }
                if (confVariables.ApiVersion == null)
                {
                    confVariables.ApiVersion = "2016-10-01";
                }
                if (confVariables.Resource == null)
                {
                    confVariables.Resource = "https://vault.azure.net";
                }
                return(confVariables);
            }
            catch { return(null); }
        }
Beispiel #3
0
        public static string[] NonExistingCertificates(string[] certificates)
        {
            if (certificates == null)
            {
                throw new ArgumentNullException(nameof(certificates));
            }
            string token = GetToken();

            Assert.AreNotEqual("", token);
            ConfVariables confVariables = GetConfigurationVariables();

            Assert.AreNotEqual(null, confVariables);
            string listCert = "certificates?api-version=";
            string endPoint = confVariables.Host + listCert + confVariables.ApiVersion;
            var    client   = new RestClient(endPoint)
            {
                Method        = HttpVerb.GET,
                Authorization = token,
                ContentType   = "application/json"
            };
            var      json            = client.MakeRequest();
            ListCert listCertificate = JsonConvert.DeserializeObject <ListCert>(json);

            foreach (Certificates cert in listCertificate.Value)
            {
                for (int i = 0; i < certificates.Length; i++)
                {
                    if (cert.Id.Contains(certificates[i]))
                    {
                        certificates = certificates.Where(w => w != certificates[i]).ToArray();
                    }
                }
            }
            while (listCertificate.NextLink != null)
            {
                var newClient = new RestClient(listCertificate.NextLink)
                {
                    Method        = HttpVerb.GET,
                    Authorization = token,
                    ContentType   = "application/json"
                };
                json            = newClient.MakeRequest();
                listCertificate = JsonConvert.DeserializeObject <ListCert>(json);
                foreach (Certificates cert in listCertificate.Value)
                {
                    for (int i = 0; i < certificates.Length; i++)
                    {
                        if (cert.Id.Contains(certificates[i]))
                        {
                            certificates = certificates.Where(w => w != certificates[i]).ToArray();
                        }
                    }
                }
            }
            return(certificates);
        }
Beispiel #4
0
        public static void CreateCertificates(string[] certificates)
        {
            if (certificates == null)
            {
                throw new ArgumentNullException(nameof(certificates));
            }
            string token = GetToken();

            Assert.AreNotEqual("", token);
            ConfVariables confVariables = GetConfigurationVariables();

            Assert.AreNotEqual(null, confVariables);
            foreach (string cert in certificates)
            {
                CreateCertificateBody createCert = new CreateCertificateBody()
                {
                    Policy = new CertificatePolicy
                    {
                        Key_props = new KeyProperties
                        {
                            Exportable = true,
                            Kty        = "RSA",
                            Key_size   = 2048,
                            Reuse_key  = true
                        },
                        Secret_props = new SecretProperties
                        {
                            contentType = "application/x-pkcs12"
                        },
                        X509_props = new X509CertificateProperties
                        {
                            Subject         = "CN=" + cert + ".com",
                            Key_usage       = new string[] { "DataEncipherment", "DigitalSignature", "KeyEncipherment" },
                            Validity_months = 12,
                        },
                        Issuer = new IssuerParameters
                        {
                            Name = "Self"
                        }
                    }
                };
                string listCert = "certificates/" + cert + "/create?api-version=";
                string endPoint = confVariables.Host + listCert + confVariables.ApiVersion;
                var    client   = new RestClient(endPoint)
                {
                    Method        = HttpVerb.POST,
                    Authorization = token,
                    ContentType   = "application/json",
                    PostData      = JsonConvert.SerializeObject(createCert).ToString()
                };
                var json = client.MakeRequest();
            }
            foreach (string cert in certificates)
            {
                CertificateOperation CertificateCreation;
                int    count    = 0;
                string listCert = "certificates/" + cert + "/pending?api-version=";
                string endPoint = confVariables.Host + listCert + confVariables.ApiVersion;
                var    client   = new RestClient(endPoint)
                {
                    Method        = HttpVerb.GET,
                    Authorization = token,
                    ContentType   = "application/json"
                };
                do
                {
                    var json = client.MakeRequest();
                    CertificateCreation = JsonConvert.DeserializeObject <CertificateOperation>(json);
                    if ((CertificateCreation.Status.Contains("completed")) || count > 10)
                    {
                        break;
                    }
                    count++;
                    Thread.Sleep(2000);
                } while (1 == 1);
            }
        }