Ejemplo n.º 1
0
        public static async Task <Result <Monitor> > CreateAsync(OperationContext context, Configuration configuration)
        {
            Tracer.Info(context, "Creating Kusto ingest client");
            var kustoIngestClient = ExternalDependenciesFactory.CreateKustoIngestClient(configuration.KustoIngestionCredentials).ThrowIfFailure();

            IIcmClient icmClient;

            if (!configuration.ReadOnly)
            {
                Tracer.Info(context, "Creating KeyVault client");
                var keyVaultClient = new KeyVaultClient(
                    configuration.KeyVaultUrl,
                    configuration.KeyVaultCredentials.TenantId,
                    configuration.KeyVaultCredentials.AppId,
                    configuration.KeyVaultCredentials.AppKey,
                    SystemClock.Instance,
                    Constants.IcmCertificateCacheTimeToLive);

                Tracer.Info(context, "Creating IcM client");
                icmClient = new IcmClient(
                    keyVaultClient,
                    configuration.IcmUrl,
                    configuration.IcmConnectorId,
                    configuration.IcmCertificateName,
                    SystemClock.Instance);
            }
            else
            {
                Tracer.Info(context, "Using mock ICM client");
                icmClient = new MockIcmClient();
            }

            var environmentResources = new Dictionary <MonitorEnvironment, EnvironmentResources>();

            // This does a bunch of Azure API calls, which are really slow. Making them a bit faster by doing them
            // concurrently.
            await configuration.Environments.ParallelForEachAsync(async (keyValuePair) =>
            {
                var environment = keyValuePair.Key;
                var environmentConfiguration = keyValuePair.Value;

                Tracer.Info(context, $"Loading resources for environment `{environment}`");
                var resources = await CreateEnvironmentResourcesAsync(context, environmentConfiguration);

                lock (environmentResources)
                {
                    environmentResources[environment] = resources;
                }
            });

            context.Token.ThrowIfCancellationRequested();
            return(new Monitor(
                       configuration,
                       kustoIngestClient,
                       icmClient,
                       SystemClock.Instance,
                       environmentResources,
                       context.TracingContext.Logger));
        }
Ejemplo n.º 2
0
        public async Task IcmClientTestAsync()
        {
            Debugger.Launch();

            var appKey = GetApplicationKey().ShouldBeSuccess();

            var config = new App.Monitor.Configuration
            {
                AzureAppKey = appKey.Value !
            };

            var clock = new MemoryClock();

            clock.Increment();

            var keyVault = new KeyVaultClient(
                config.KeyVaultUrl,
                config.AzureTenantId,
                config.AzureAppId,
                config.AzureAppKey,
                clock,
                cacheTimeToLive: TimeSpan.FromSeconds(1));

            keyVault.IcmCallsCounter.Value.Should().Be(0);

            // Simulate that the certificate has been acquired before.
            _ = await keyVault.GetCertificateAsync(config.IcmCertificateName);

            keyVault.IcmCallsCounter.Value.Should().Be(1);

            var icmClient = new IcmClient(keyVault, config.IcmUrl, config.IcmConnectorId, config.IcmCertificateName, clock);

            var incident = new IcmIncident(
                stamp: "Test",
                environment: "PROD",
                machines: new [] { "MachineA", "MachineB" },
                correlationIds: new[] { "GuidA", "GuidB" },
                severity: 4,
                description: "This incident was created for testing the cache monitor",
                title: "Cache Monitor Test Incident",
                incidentTime: DateTime.Now,
                cacheTimeToLive: null);

            await icmClient.EmitIncidentAsync(incident);

            // Should have used cached cert.
            keyVault.IcmCallsCounter.Value.Should().Be(1);

            // Simulate that the certificate will be acquired in the future.
            clock.AddSeconds(2);
            _ = await keyVault.GetCertificateAsync(config.IcmCertificateName);

            keyVault.IcmCallsCounter.Value.Should().Be(2);
        }
    }
Ejemplo n.º 3
0
        public static async Task <Result <Monitor> > CreateAsync(OperationContext context, Configuration configuration)
        {
            Tracer.Info(context, "Creating Kusto ingest client");
            var kustoIngestClient = ExternalDependenciesFactory.CreateKustoIngestClient(configuration.KustoIngestionCredentials).ThrowIfFailure();

            IIcmClient icmClient;

            if (!configuration.ReadOnly)
            {
                Tracer.Info(context, "Creating KeyVault client");
                var keyVaultClient = new KeyVaultClient(
                    configuration.KeyVaultUrl,
                    configuration.KeyVaultCredentials.TenantId,
                    configuration.KeyVaultCredentials.AppId,
                    configuration.KeyVaultCredentials.AppKey,
                    SystemClock.Instance,
                    Constants.IcmCertificateCacheTimeToLive);

                Tracer.Info(context, "Creating IcM client");
                icmClient = new IcmClient(
                    keyVaultClient,
                    configuration.IcmUrl,
                    configuration.IcmConnectorId,
                    configuration.IcmCertificateName,
                    SystemClock.Instance);
            }
            else
            {
                Tracer.Info(context, "Using mock ICM client");
                icmClient = new MockIcmClient();
            }

            var environmentResources = await CreateEnvironmentResourcesAsync(context, configuration.Environments);

            context.Token.ThrowIfCancellationRequested();
            return(new Monitor(
                       configuration,
                       kustoIngestClient,
                       icmClient,
                       SystemClock.Instance,
                       environmentResources,
                       context.TracingContext.Logger));
        }