Получите сертификат APNS для центра уведомлений Azure - PullRequest
0 голосов
/ 20 сентября 2019

Я хочу создать приложение, чтобы я мог проверять все наши 200+ узлов уведомлений.Это в первую очередь для поиска сертификатов для APNS, срок действия которых истекает.

Я могу сделать это просто отлично в PowerShell, но я не могу получить это право в C #, поскольку у Fluent SDK нет метода для таких учетных данных, какPowerShell.

var credentials = SdkContext.AzureCredentialsFactory
    .FromServicePrincipal(clientId,
    clientSecret,
    tenantId,
    AzureEnvironment.AzureGlobalCloud);

var azure = Azure
    .Configure()
    .Authenticate(credentials)
    .WithDefaultSubscription();

NotificationHubsManagementClient nhClient = new NotificationHubsManagementClient(credentials);
nhClient.SubscriptionId = subscriptionId;

var nhNamespaces = nhClient.Namespaces.ListAllAsync().Result;

foreach (var nhNamespace in nhNamespaces)
{
    var nhHubs = nhClient.NotificationHubs.ListAsync(resourceGroupName, nhNamespace.Name).Result;

    foreach (var nhHub in nhHubs)
    {
        var hub = nhClient.NotificationHubs.GetAsync(resourceGroupName, nhNamespace.Name, nhHub.Name).Result;

        //THEY ARE ALWAYS NULL AND THERE IS NO METHOD TO GET THE APNS CREDENTIALS
        if (hub.ApnsCredential != null)
        {
            var apnsCred = hub.ApnsCredential;
            Console.WriteLine(apnsCred.ApnsCertificate);
        }
    }
}

В PowerShell я могу позвонить:

$pnsCred = Get-AzureRmNotificationHubPNSCredentials -ResourceGroup $resourceGroup -Namespace $hubNamespaceName -NotificationHub $hub.Name

Мне нужен способ получить учетные данные концентратора в C #.

МОЕ ПОЧТИ ФИНАЛЬНОЕ РЕШЕНИЕ (НЕОБХОДИМО БОЛЬШЕ РЕФРАКТИРОВКИ И ОБРАБОТКИ ОШИБОК):

using Microsoft.Azure.Management.NotificationHubs.Fluent;
using Microsoft.Azure.Management.NotificationHubs.Fluent.Models;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Newtonsoft.Json;
using RestSharp;
using System;
using System.Linq;
using System.Configuration;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using mpu.sql;
using mpu.snow;
using mpu.helpers;

namespace mpu.azure
{
    public static class NotificationHubHelper
    {
        private static string tenantId = ConfigurationManager.AppSettings["nhIda:tenantId"];
        private static string clientId = ConfigurationManager.AppSettings["nhIda:clientId"];
        private static string clientSecret = ConfigurationManager.AppSettings["nhIda:clientSecret"];
        private static string resourceGroupName = ConfigurationManager.AppSettings["nhIda:resourceGroupName"];
        private static string subscriptionId = ConfigurationManager.AppSettings["nhIda:subscriptionId"];
        private static DateTime expiryCheck = DateTime.Now.AddDays(-30);

        public static void CheckHubApnsCerts()
        {
            var credentials = SdkContext.AzureCredentialsFactory
                .FromServicePrincipal(clientId,
                clientSecret,
                tenantId,
                AzureEnvironment.AzureGlobalCloud);

            NotificationHubsManagementClient nhClient = new NotificationHubsManagementClient(credentials);
            nhClient.SubscriptionId = subscriptionId;

            var nhNamespaces = nhClient.Namespaces.ListAllAsync().Result;

            nhNamespaces.AsParallel().ForAll(nhNamespace =>
            {
                var nhHubs = nhClient.NotificationHubs.ListAsync(resourceGroupName, nhNamespace.Name).Result;

                nhHubs.AsParallel().ForAll(async (nhHub) =>
                {
                    PnsCredentialResponse pnsCredential = await GetPnsCredential(nhNamespace, nhHub, nhClient.ApiVersion);

                    if (!string.IsNullOrEmpty(pnsCredential?.properties?.apnsCredential?.properties?.apnsCertificate))
                    {
                        var certRawValue = pnsCredential.properties.apnsCredential.properties.apnsCertificate;
                        var certKey = pnsCredential.properties.apnsCredential.properties.certificateKey;

                        var cert = new X509Certificate2(Convert.FromBase64String(certRawValue), certKey, X509KeyStorageFlags.MachineKeySet
                                                  | X509KeyStorageFlags.PersistKeySet
                                                  | X509KeyStorageFlags.Exportable);

                        Console.ForegroundColor = ConsoleColor.Green;
                        if (cert.NotAfter <= expiryCheck)
                        {
                            Console.ForegroundColor = ConsoleColor.Red;

                            try
                            {
                                var certIncident = SqlHelper.GetRecordByCertThumb(cert.Thumbprint);

                                if (certIncident == null)
                                {
                                    var incidentNumber = SnowIncidents.CreateP2Incident($"Notification Hub APNS Certificate Expiring {nhHub.Name}", $"The notification hub APNS certificate for hub {nhHub.Name} is due to expire on {cert.NotAfter}. Please verify in Azure and request a new certificate from the client ASAP.");

                                    if (!string.IsNullOrEmpty(incidentNumber))
                                        SqlHelper.CreateIncidentRecord(cert.Thumbprint, incidentNumber);
                                }
                            }
                            catch
                            {
                                EmailHelper.SendCertExpiryEmailForNotificationHub(nhHub.Name, cert.NotAfter);
                            }
                        }

                        Console.WriteLine($"{nhHub.Name} - {cert.NotAfter} - {cert.Thumbprint}");
                    }
                });
            });
        }

        private static async Task<PnsCredentialResponse> GetPnsCredential(NamespaceResourceInner nhNamespace, NotificationHubResourceInner nhHub, string apiVerion)
        {
            var requestString = $"https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.NotificationHubs/namespaces/{nhNamespace.Name}/notificationHubs/{nhHub.Name}/pnsCredentials?api-version={apiVerion}";

            var client = new RestClient(requestString);
            var request = new RestRequest(Method.POST);
            request.AddHeader("Authorization", $"Bearer {await GetAccessToken(tenantId, clientId, clientSecret)}");
            IRestResponse response = client.Execute(request);

            var pnsCredential = JsonConvert.DeserializeObject<PnsCredentialResponse>(response.Content);
            return pnsCredential;
        }

        private static async Task<string> GetAccessToken(string tenantId, string clientId, string clientKey)
        {
            string authContextURL = "https://login.windows.net/" + tenantId;
            var authenticationContext = new AuthenticationContext(authContextURL);
            var credential = new ClientCredential(clientId, clientKey);
            var result = await authenticationContext
                .AcquireTokenAsync("https://management.azure.com/", credential);

            if (result == null)
                throw new InvalidOperationException("Failed to obtain the JWT token for management.azure.com");

            return result.AccessToken;
        }
    }

    public class PnsCredentialResponse
    {
        public string id { get; set; }
        public string name { get; set; }
        public string type { get; set; }
        public object location { get; set; }
        public object tags { get; set; }
        public Properties properties { get; set; }
    }

    public class Properties
    {
        public Apnscredential apnsCredential { get; set; }
    }

    public class Apnscredential
    {
        [JsonProperty("properties")]
        public ApnsProperties properties { get; set; }
    }

    public class ApnsProperties
    {
        public string endpoint { get; set; }
        public string apnsCertificate { get; set; }
        public string certificateKey { get; set; }
        public string thumbprint { get; set; }
    }
}

1 Ответ

0 голосов
/ 20 сентября 2019

Модули Azure PowerShell будут построены поверх пакета Management SDK.Самая последняя версия может быть найдена на NuGet .Исходный код этих SDK может быть найден на GitHub .Если память служит, я считаю, что вам нужен метод NamespaceOperations.GetWithHttpMessagesAsync.

Стоит отметить, что функциональность управления ранее была в v1 самого NotificationHubs SDK.Он был удален в v2, чтобы избежать путаницы в том, чтобы иметь несколько способов сделать одно и то жеТем не менее, популярным требованием является возвращение функциональности управления, поэтому существует единый пакет для работы со всеми компонентами Notification Hubs.На момент написания эти изменения находятся на рассмотрении .

...