예제 #1
0
        static void Main(string[] args)
        {
            var authotizationInfo = new AzureProviderAuthorization
            {
                AuthorizationServiceUrl = "https://login.microsoftonline.com/{{tenantId}}/oauth2/token",
                TenandId     = "<From Azure AD>",
                ClientId     = "<From RBAC Service Principal AppId>",
                ClientSecret = "<From RBAC Service Principal>"
            };

            var jwt = GetJwt(authotizationInfo).Result;

            var apimConfiguration = new AzureApiManagementGatewayConfiguration
            {
                ApiManagementGatewayName = "hwt-apim-westus-triage",
                ResourceGroupName        = "hwt-rg-westus-triage",
                SubscriptionId           = "<From Azure Portal Subscription Id>"
            };

            var productionApiName = "hwt-api-westus-triage";
            var stagingApiName    = "hwt-api-stage-westus-triage";
            var oldApiName        = "hwt-api-old-westus-triage";

            var productionApiPolicyXml = GetCurrentPolicy(apimConfiguration, jwt, productionApiName).Result;
            var stagingApiPolicyXml    = GetCurrentPolicy(apimConfiguration, jwt, stagingApiName).Result;
            var oldApiPolicyXml        = GetCurrentPolicy(apimConfiguration, jwt, oldApiName).Result;

            var productionBackendId = GetExistingBackendId(productionApiPolicyXml);
            var stagingBackendId    = GetExistingBackendId(stagingApiPolicyXml);
            var oldBackendId        = GetExistingBackendId(oldApiPolicyXml);

            // Iterate over a period of time and increase the percentage of traffic to the new backend.
            var newCanaryPolicy = GetCanaryPolicy(productionApiPolicyXml, 70, productionBackendId, stagingBackendId);
            var setCanaryPolicy = SetPolicy(apimConfiguration, jwt, productionApiName, newCanaryPolicy).Result;

            // At the end of the process, rearrange the pointers so that:
            // 1) The Staging gets promoted to Production.
            // 2) Production gets demoted to Old.
            // 3) Old gets switched to Staging.

            var newPolicyForProduction = GetSingleBackendPolicy(productionApiPolicyXml, stagingBackendId);
            var newPolicyForOld        = GetSingleBackendPolicy(oldApiPolicyXml, productionBackendId);
            var newPolicyForStaging    = GetSingleBackendPolicy(stagingApiPolicyXml, oldBackendId);

            var setProductionPolicy = SetPolicy(apimConfiguration, jwt, productionApiName, newPolicyForProduction).Result;
            var setStagingPolicy    = SetPolicy(apimConfiguration, jwt, stagingApiName, newPolicyForStaging).Result;
            var setOldPolicy        = SetPolicy(apimConfiguration, jwt, oldApiName, newPolicyForOld).Result;
        }
예제 #2
0
        public static async Task <string> GetJwt(AzureProviderAuthorization authorizationInfo)
        {
            var requestUrl = authorizationInfo.AuthorizationServiceUrl.Replace("{{tenantId}}", authorizationInfo.TenandId);
            var postBody   = $"grant_type=client_credentials&client_id={authorizationInfo.ClientId}&client_secret={authorizationInfo.ClientSecret}&resource=https://management.azure.com/";

            using (var request = new HttpRequestMessage {
                RequestUri = new Uri(requestUrl), Content = new StringContent(postBody, Encoding.UTF8, "application/x-www-form-urlencoded")
            })
                using (var response = await HttpClient.SendAsync(request))
                {
                    if (!response.IsSuccessStatusCode)
                    {
                        return(null);
                    }

                    var responseText = await response.Content.ReadAsStringAsync();

                    var jsonResponse = JToken.Parse(responseText);

                    return(jsonResponse["access_token"].Value <string>());
                }
        }