コード例 #1
0
        public async Task LetsEncryptRenewerTimerTriggerAsync([TimerTrigger("%LetsEncryptRenewerWebJobSettings:TimerSettings%", RunOnStartup = true, UseMonitor = true)] TimerInfo timerInfo, ILogger logger)
        {
            if (_letsEncryptConfig.IsJobEnabled)
            {
                PublishingCredentials publishingCredentials = null;
                try
                {
                    logger.LogInformation($"LetsEncryptRenewerWebJob started!");
                    logger.LogInformation($"Trying to obtain token...");
                    var token = await CredentialsManager.GetAccessToken(_letsEncryptConfig.AzureEnvironment);

                    logger.LogInformation($"Token obtain completed!");

                    logger.LogInformation($"Trying to obtain publishing credentials...");
                    publishingCredentials = await CredentialsManager.GetPublishingCredentials(_letsEncryptConfig.AzureEnvironment, token);

                    logger.LogInformation($"Publishing credentials were retrieved successfully!");
                }
                catch (Exception ex)
                {
                    _appInsightLogger.TrackException(ex);
                    logger.LogInformation($"Failed to obtain credentials!");
                }
                if (publishingCredentials != null)
                {
                    try
                    {
                        logger.LogInformation($"Proceed lets encrypt renewer api...");
                        var client   = new HttpClient();
                        var renewUrl = string.Format(Constants.LetsEncryptApiUrlTemplate, _letsEncryptConfig.AzureEnvironment.WebAppName);
                        client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes($"{publishingCredentials.Properties.PublishingUserName}:{publishingCredentials.Properties.PublishingPassword}")));
                        var res = await client.PostAsync(renewUrl, new StringContent(JsonConvert.SerializeObject(_letsEncryptConfig, Formatting.None, new JsonSerializerSettings {
                            NullValueHandling = NullValueHandling.Ignore
                        }), Encoding.UTF8, "application/json"));

                        switch (res.StatusCode)
                        {
                        case HttpStatusCode.OK:
                            logger.LogInformation($"Lets encrypt certificate succsessfully renewed...");
                            break;

                        default:
                            logger.LogInformation($"Failed to renew certificate, something wrong with lets encrypt renewer api or configuration!");
                            var ex = new Exception(string.Format("Failed to renew certificate! : Response: {0}", await res.Content.ReadAsStringAsync()));
                            _appInsightLogger.TrackException(ex);
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        _appInsightLogger.TrackException(ex);
                        logger.LogInformation($"Exception during lets encrypt renewer api call!");
                    }
                }
            }
            else
            {
                logger.LogInformation($"Job disabled by settings!");
            }
        }
コード例 #2
0
        public static async Task<PublishingCredentials> GetPublishingCredentials(AzureEnvironment config, string token)
        {
            var requestUrl = string.Format(Constants.PublishingCredentialsUrlTemplate, config.SubscriptionId, config.ResourceGroupName, config.WebAppName);

            var client = new HttpClient();
            client.DefaultRequestHeaders.Add("Authorization", string.Format("Bearer {0}", token));

            var result = await client.PostAsync(requestUrl, new StringContent("", Encoding.UTF8, "application/json"));
            if (result.IsSuccessStatusCode)
            {
                var content = await result.Content.ReadAsStringAsync();
                PublishingCredentials publishingCredentials = JsonConvert.DeserializeObject<PublishingCredentials>(content);
                return publishingCredentials;
            }
            else
            {
                throw new InvalidOperationException("Failed to obtain publishing credentials!");
            }
        }