public bool Connect()
        {
            if (_config.LogAnalyticsCreate | _config.LogAnalyticsRecreate | _config.Unique)
            {
                Authenticate();
                GetCurrentWorkspace();

                if (_config.LogAnalyticsRecreate)
                {
                    return(RecreateWorkspace());
                }

                if (_config.LogAnalyticsCreate)
                {
                    return(CreateWorkspace());
                }

                if (_config.Unique)
                {
                    List <string> existingUploads = PostQueryList($"['{_config.LogAnalyticsName}_CL']|distinct RelativeUri_s", false).Select(x => x = Path.GetFileNameWithoutExtension(x)).ToList();
                    Log.Info($"listResults:", existingUploads);
                    foreach (string existingUpload in existingUploads)
                    {
                        _instance.FileObjects.Add(new FileObject(existingUpload)
                        {
                            Status = FileStatus.existing
                        });
                    }
                }
            }

            // send empty object to check connection
            FileObject fileObject = new FileObject();

            fileObject.Stream.Set(Encoding.UTF8.GetBytes("{}"));
            Log.Info($"Checking connection to log analytics workspace {_config.LogAnalyticsId} ", ConsoleColor.Green);

            if (!PostData(fileObject, true))
            {
                return(false);
            }

            if (_config.IsLogAnalyticsPurgeRequested())
            {
                _arm.Authenticate();
                Purge();
                return(false);
            }

            return(true);
        }
Exemple #2
0
        public bool Connect()
        {
            if (Config.LogAnalyticsCreate | Config.LogAnalyticsRecreate | Config.Unique)
            {
                Authenticate();
                GetCurrentWorkspace();

                if (Config.LogAnalyticsRecreate)
                {
                    return(RecreateWorkspace());
                }

                if (Config.LogAnalyticsCreate)
                {
                    return(CreateWorkspace());
                }

                if (Config.Unique)
                {
                    _ingestedUris.AddRange(PostQueryList($"['{Config.LogAnalyticsName}_CL']|distinct RelativeUri_s", false)
                                           .Select(x => x = Path.GetFileNameWithoutExtension(x)));
                    Log.Info($"listResults:", _ingestedUris);
                }
            }

            // send empty object to check connection
            FileObject fileObject = new FileObject();

            fileObject.Stream.Set(Encoding.UTF8.GetBytes("{}"));
            Log.Info($"Checking connection to log analytics workspace {Config.LogAnalyticsId} ", ConsoleColor.Green);

            if (!PostData(fileObject, true))
            {
                return(false);
            }

            if (Config.IsLogAnalyticsPurgeRequested())
            {
                _arm.Authenticate();
                Purge();
                return(false);
            }

            return(true);
        }
        private TestUtilities DefaultUtilities()
        {
            TestUtilities        utils = new TestUtilities();
            CertificateUtilities certificateUtilities = new CertificateUtilities();
            ConfigurationOptions config = utils.Collector.Config;
            // verify test credentials work
            AzureResourceManager arm = new AzureResourceManager(config);

            _appCertificate = certificateUtilities.GetClientCertificate(TestUtilities.TestProperties.AzureClientCertificate);
            //_appCertificate = new X509Certificate2(Convert.FromBase64String(TestUtilities.TestProperties.AzureClientCertificate),
            //    TestUtilities.TestProperties.adminPassword,
            //    X509KeyStorageFlags.Exportable);
            Assert.IsNotNull(_appCertificate);

            _clientCertificate = certificateUtilities.GetClientCertificate(TestUtilities.TestProperties.testAzClientCertificate);
            //_clientCertificate = new X509Certificate2(Convert.FromBase64String(TestUtilities.TestProperties.testAzClientCertificate),
            //    TestUtilities.TestProperties.adminPassword,
            //    X509KeyStorageFlags.Exportable);
            Assert.IsNotNull(_clientCertificate);

            config.AzureResourceGroup         = TestUtilities.TestProperties.AzureResourceGroup;
            config.AzureResourceGroupLocation = TestUtilities.TestProperties.AzureResourceGroupLocation;
            config.AzureSubscriptionId        = TestUtilities.TestProperties.AzureSubscriptionId;
            config.AzureTenantId = TestUtilities.TestProperties.AzureTenantId;

            // config.AzureClientSecret = TestUtilities.TestProperties.AzureClientSecret;
            config.AzureClientId          = TestUtilities.TestProperties.testAzClientId;
            config.AzureClientCertificate = TestUtilities.TestProperties.testAzClientCertificate;

            arm.Authenticate(true);

            config.SasKey                 = TestUtilities.TestProperties.SasKey;
            config.CacheLocation          = TestUtilities.TempDir;
            config.StartTimeStamp         = DateTime.MinValue.ToString("o");
            config.EndTimeStamp           = DateTime.Now.ToString("o");
            config.AzureClientId          = TestUtilities.TestProperties.AzureClientId;
            config.AzureClientCertificate = TestUtilities.TestProperties.AzureClientCertificate;
            config.AzureClientSecret      = TestUtilities.TestProperties.AzureClientSecret;
            config.AzureKeyVault          = TestUtilities.TestProperties.AzureKeyVault;

            config.List       = true;
            config.GatherType = FileTypesEnum.trace.ToString();
            return(utils);
        }
        public void Authenticate(bool throwOnError = false)
        {
            _arm.Scopes = new List <string>()
            {
                $"{ClusterIngestUrl}/kusto.read", $"{ClusterIngestUrl}/kusto.write"
            };

            if (Config.IsClientIdConfigured())
            {
                _arm.Scopes = new List <string>()
                {
                    $"{ClusterIngestUrl}/.default"
                };
            }

            if (Config.IsKustoConfigured() && _arm.Authenticate(throwOnError, ClusterIngestUrl))
            {
                DatabaseConnection = new KustoConnectionStringBuilder(ClusterIngestUrl)
                {
                    FederatedSecurity = true, InitialCatalog = DatabaseName, UserToken = _arm.BearerToken
                };
                ManagementConnection = new KustoConnectionStringBuilder(ManagementUrl)
                {
                    FederatedSecurity = true, InitialCatalog = DatabaseName, UserToken = _arm.BearerToken
                };
            }
            else
            {
                DatabaseConnection = new KustoConnectionStringBuilder(ClusterIngestUrl)
                {
                    FederatedSecurity = true, InitialCatalog = DatabaseName
                };
                ManagementConnection = new KustoConnectionStringBuilder(ManagementUrl)
                {
                    FederatedSecurity = true, InitialCatalog = DatabaseName
                };
            }

            IdentityToken      = RetrieveKustoIdentityToken();
            IngestionResources = RetrieveIngestionResources();
        }
        private LogAnalyticsQueryResults PostQuery(string query)
        {
            LogAnalyticsQueryResults laResults = new LogAnalyticsQueryResults();
            string jsonBody = $"{{\"query\": \"{query}\"}}";

            try
            {
                string uri = $"{_logAnalyticsQueryEndpoint}{Config.LogAnalyticsId}/query";
                Log.Info($"post uri:{uri}", ConsoleColor.Blue, null, query);

                _laArm.Authenticate(true, _logAnalyticsAuthResource);
                _httpClient.SendRequest(
                    uri: uri,
                    authToken: _laArm.BearerToken,
                    jsonBody: jsonBody,
                    httpMethod: HttpMethod.Post);

                Log.Info($"{_httpClient.Response?.ReasonPhrase}");

                if (!_httpClient.Success)
                {
                    Log.Error("unsuccessful response:", _httpClient.Response);
                    return(null);
                }
                else
                {
                    laResults = JsonConvert.DeserializeObject <LogAnalyticsQueryResults>(_httpClient.ResponseStreamString);
                    Log.Info($"result rows count:{laResults?.tables?[0].rows?.Length}", ConsoleColor.DarkBlue);
                    Log.Debug($"result:", laResults);
                }

                return(laResults);
            }
            catch (Exception e)
            {
                Log.Exception($"post exception:{e}");
                return(null);
            }
        }
        private void Authenticate()
        {
            if (!_arm.IsAuthenticated)
            {
                _arm.Scopes = new List <string>()
                {
                    $"{_armAuthResource}//user_impersonation"
                };

                if (_config.IsClientIdConfigured())
                {
                    _arm.Scopes = new List <string>()
                    {
                        $"{_armAuthResource}//.default"
                    };
                }

                _arm.Authenticate(false, _armAuthResource);
            }

            if (!_laArm.IsAuthenticated)
            {
                _laArm.Scopes = new List <string>()
                {
                    $"{_logAnalyticsAuthResource}//user_impersonation"
                };

                if (_config.IsClientIdConfigured())
                {
                    _laArm.Scopes = new List <string>()
                    {
                        $"{_logAnalyticsAuthResource}//.default"
                    };
                }

                _laArm.Authenticate(true, _logAnalyticsAuthResource);
            }
        }
        public void Authenticate(bool throwOnError = false)
        {
            _arm.Scopes = new List <string>()
            {
                $"{ClusterIngestUrl}/kusto.read", $"{ClusterIngestUrl}/kusto.write"
            };

            if (_config.IsClientIdConfigured())
            {
                _arm.Scopes = new List <string>()
                {
                    $"{ClusterIngestUrl}/.default"
                };
            }

            if (_config.IsKustoConfigured() && _arm.Authenticate(throwOnError, ClusterIngestUrl))
            {
                if (_arm.ClientIdentity.IsAppRegistration)
                {
                    Log.Info($"connecting to kusto with app registration {_config.AzureClientId}");

                    DatabaseConnection = new KustoConnectionStringBuilder(ClusterIngestUrl)
                    {
                        FederatedSecurity          = true,
                        InitialCatalog             = DatabaseName,
                        ApplicationClientId        = _config.AzureClientId,
                        ApplicationCertificateBlob = _config.ClientCertificate,
                        Authority = _config.AzureTenantId,
                        ApplicationCertificateSendX5c = true
                    };

                    ManagementConnection = new KustoConnectionStringBuilder(ManagementUrl)
                    {
                        FederatedSecurity          = true,
                        InitialCatalog             = DatabaseName,
                        ApplicationClientId        = _config.AzureClientId,
                        ApplicationCertificateBlob = _config.ClientCertificate,
                        Authority = _config.AzureTenantId,
                        ApplicationCertificateSendX5c = true
                    };
                }
                else
                {
                    DatabaseConnection = new KustoConnectionStringBuilder(ClusterIngestUrl)
                    {
                        FederatedSecurity = true,
                        InitialCatalog    = DatabaseName,
                        UserToken         = _arm.BearerToken
                    };

                    ManagementConnection = new KustoConnectionStringBuilder(ManagementUrl)
                    {
                        FederatedSecurity = true,
                        InitialCatalog    = DatabaseName,
                        UserToken         = _arm.BearerToken
                    };
                }
            }
            else
            {
                DatabaseConnection = new KustoConnectionStringBuilder(ClusterIngestUrl)
                {
                    FederatedSecurity = true,
                    InitialCatalog    = DatabaseName
                };

                ManagementConnection = new KustoConnectionStringBuilder(ManagementUrl)
                {
                    FederatedSecurity = true,
                    InitialCatalog    = DatabaseName
                };
            }

            IdentityToken      = RetrieveKustoIdentityToken();
            IngestionResources = RetrieveIngestionResources();
        }