private static string GetWebsiteAuthToken()
        {
            string   websiteAuthEncryptionKey = System.Environment.GetEnvironmentVariable(SettingsKeys.AuthEncryptionKey);
            DateTime expiry = DateTime.UtcNow.AddMinutes(5);

            return(SimpleWebTokenHelper.CreateToken(expiry, websiteAuthEncryptionKey.ToKeyBytes()));
        }
        /// <summary>
        /// Remove all site workers after cloudbuilt content is uploaded
        /// </summary>
        /// <param name="websiteHostname">WEBSITE_HOSTNAME</param>
        /// <param name="sitename">WEBSITE_SITE_NAME</param>
        /// <exception cref="ArgumentException">Thrown when RemoveAllWorkers url is malformed.</exception>
        /// <exception cref="HttpRequestException">Thrown when request to RemoveAllWorkers is not OK.</exception>
        public static async Task RemoveAllWorkersAsync(string websiteHostname, string sitename)
        {
            // Generate URL encoded auth token
            string   websiteAuthEncryptionKey = System.Environment.GetEnvironmentVariable(SettingsKeys.AuthEncryptionKey);
            DateTime expiry           = DateTime.UtcNow.AddMinutes(5);
            string   authToken        = SimpleWebTokenHelper.CreateToken(expiry, websiteAuthEncryptionKey.ToKeyBytes());
            string   authTokenEncoded = HttpUtility.UrlEncode(authToken);

            // Generate RemoveAllWorker request URI
            string baseUrl = $"http://{websiteHostname}/operations/removeworker/{sitename}/allStandard?token={authTokenEncoded}";
            Uri    baseUri = null;

            if (!Uri.TryCreate(baseUrl, UriKind.Absolute, out baseUri))
            {
                throw new ArgumentException($"Malformed URI is used in RemoveAllWorkers");
            }
            Trace(TraceEventType.Information, "Calling RemoveAllWorkers to refresh the function app");

            // Initiate GET request
            using (var client = HttpClientFactory())
                using (var response = await client.GetAsync(baseUri))
                {
                    response.EnsureSuccessStatusCode();
                    Trace(TraceEventType.Information, "RemoveAllWorkers, statusCode = {0}", response.StatusCode);
                }

            return;
        }
Exemple #3
0
        private static async Task PostAsync(string path, string requestId, string content = null)
        {
            var host       = HttpHost;
            var statusCode = default(HttpStatusCode);

            Trace(TraceEventType.Verbose, "Begin HttpPost https://{0}{1}, x-ms-request-id: {2}", host, path, requestId);
            try
            {
                using (var client = HttpClientFactory())
                {
                    client.BaseAddress = new Uri(string.Format("https://{0}", host));
                    client.DefaultRequestHeaders.UserAgent.Add(_userAgent.Value);
                    client.DefaultRequestHeaders.Add(Constants.SiteRestrictedToken, SimpleWebTokenHelper.CreateToken(DateTime.UtcNow.AddMinutes(5)));
                    client.DefaultRequestHeaders.Add(Constants.RequestIdHeader, requestId);

                    var payload = new StringContent(content ?? string.Empty, Encoding.UTF8, "application/json");
                    using (var response = await client.PostAsync(path, payload))
                    {
                        statusCode = response.StatusCode;
                        response.EnsureSuccessStatusCode();
                    }
                }
            }
            finally
            {
                Trace(TraceEventType.Verbose, "End HttpPost, status: {0}", statusCode);
            }
        }
Exemple #4
0
        // Throws on failure
        private static async Task PostAsync(string path, string requestId, string hostName = null, string content = null)
        {
            var hostOrAuthority = IsLocalHost ? HttpAuthority : HttpHost;

            hostName = hostName ?? hostOrAuthority;

            var ipAddress = await GetAlternativeIPAddress(hostOrAuthority);

            var scheme     = IsLocalHost ? "http" : "https";
            var statusCode = default(HttpStatusCode);

            try
            {
                using (var client = HttpClientFactory())
                {
                    if (ipAddress == null)
                    {
                        Trace(TraceEventType.Verbose, "Begin HttpPost {0}://{1}{2}, x-ms-request-id: {3}", scheme, hostName, path, requestId);
                        client.BaseAddress = new Uri(string.Format("{0}://{1}", scheme, hostName));
                    }
                    else
                    {
                        Trace(TraceEventType.Verbose, "Begin HttpPost {0}://{1}{2}, host: {3}, x-ms-request-id: {4}", scheme, ipAddress, path, hostName, requestId);
                        client.BaseAddress = new Uri(string.Format("{0}://{1}", scheme, ipAddress));
                        client.DefaultRequestHeaders.Host = hostName;
                    }

                    client.DefaultRequestHeaders.UserAgent.Add(_userAgent.Value);
                    client.DefaultRequestHeaders.Add(Constants.SiteRestrictedToken, SimpleWebTokenHelper.CreateToken(DateTime.UtcNow.AddMinutes(5)));
                    client.DefaultRequestHeaders.Add(Constants.RequestIdHeader, requestId);

                    var payload = new StringContent(content ?? string.Empty, Encoding.UTF8, "application/json");
                    using (var response = await client.PostAsync(path, payload))
                    {
                        statusCode = response.StatusCode;
                        response.EnsureSuccessStatusCode();
                    }
                }
            }
            finally
            {
                Trace(TraceEventType.Verbose, "End HttpPost, status: {0}", statusCode);
            }
        }
        // Throws on failure
        public static async Task PostAsync(string path, string requestId, string content = null)
        {
            var hostOrAuthority = IsLocalHost ? HttpAuthority : HttpHost;
            var scheme          = IsLocalHost ? "http" : "https";
            var ipAddress       = await GetAlternativeIPAddress(hostOrAuthority);

            var    statusCode = default(HttpStatusCode);
            string resContent = "";

            try
            {
                using (var client = HttpClientFactory())
                {
                    if (ipAddress == null)
                    {
                        Trace(TraceEventType.Verbose, "Begin HttpPost {0}://{1}{2}, x-ms-request-id: {3}", scheme, hostOrAuthority, path, requestId);
                        client.BaseAddress = new Uri(string.Format("{0}://{1}", scheme, hostOrAuthority));
                    }
                    else
                    {
                        Trace(TraceEventType.Verbose, "Begin HttpPost {0}://{1}{2}, host: {3}, x-ms-request-id: {4}", scheme, ipAddress, path, hostOrAuthority, requestId);
                        client.BaseAddress = new Uri(string.Format("{0}://{1}", scheme, ipAddress));
                        client.DefaultRequestHeaders.Host = hostOrAuthority;
                    }

                    client.DefaultRequestHeaders.UserAgent.Add(_userAgent.Value);
                    client.DefaultRequestHeaders.Add(Constants.SiteRestrictedToken, SimpleWebTokenHelper.CreateToken(DateTime.UtcNow.AddMinutes(5)));
                    client.DefaultRequestHeaders.Add(Constants.RequestIdHeader, requestId);

                    var payload = new StringContent(content ?? string.Empty, Encoding.UTF8, "application/json");
                    using (var response = await client.PostAsync(path, payload))
                    {
                        statusCode = response.StatusCode;
                        if (response.Content != null)
                        {
                            resContent = response.Content.ReadAsStringAsync().Result;
                        }

                        response.EnsureSuccessStatusCode();
                    }

                    if (path.Equals(Constants.UpdateDeployStatusPath) && resContent.Contains("Excessive SCM Site operation requests. Retry after 5 seconds"))
                    {
                        // Request was throttled throw an exception
                        // If max retries aren't reached, this request will be retried
                        Trace(TraceEventType.Information, $"Call to {path} was throttled. Setting statusCode to {HttpStatusCode.NotAcceptable}");

                        statusCode = HttpStatusCode.NotAcceptable;
                        throw new HttpRequestException();
                    }
                }
            }
            catch (HttpRequestException ex)
            {
                if (path.Equals(Constants.UpdateDeployStatusPath, StringComparison.OrdinalIgnoreCase) && statusCode == HttpStatusCode.NotFound)
                {
                    // Fail silently if 404 is encountered.
                    // This will only happen transiently during a platform upgrade if new bits aren't on the FrontEnd yet.
                    Trace(TraceEventType.Warning, $"Call to {path} ended in 404. {ex}");
                }
                else
                {
                    throw;
                }
            }
            finally
            {
                Trace(TraceEventType.Verbose, "End HttpPost, status: {0}", statusCode);
            }
        }
        private static string GetBlobSasEncryptedToken(string blobSas)
        {
            string websiteAuthEncryptionKey = System.Environment.GetEnvironmentVariable(SettingsKeys.AuthEncryptionKey);

            return(SimpleWebTokenHelper.CreateEncryptedBlobSas(blobSas, websiteAuthEncryptionKey.ToKeyBytes()));
        }