private AzureCredentials GetAzureCredentials()
        {
            var factory = new AzureCredentialsFactory();

            Debug.Assert(factory != null);
            Debug.Assert(_hostEnv != null);

            if (_hostEnv.IsDevelopment())
            {
                return(factory.FromFile("./azureauth.json"));
            }
            else
            {
                Debug.Assert(_config != null);

                var tenantId               = _config["SERVICE_PRINCIPAL_TENANT_ID"];
                var servicePrincipalId     = _config["SERVICE_PRINCIPAL_ID"];
                var servicePrincipalSecret = _config["SERVICE_PRINCIPAL_SECRET"];

                return(factory.FromServicePrincipal(servicePrincipalId,
                                                    servicePrincipalSecret,
                                                    tenantId,
                                                    AzureEnvironment.AzureGlobalCloud));
            }
        }
        private void InitAzureEnvironment()
        {
            var credentials = default(AzureCredentials);

            // Read required environment variables
            this.subscriptionId    = Environment.GetEnvironmentVariable(TEST_SUBSCRIPTION_ID);
            this.resourceGroupName = Environment.GetEnvironmentVariable(TEST_RG_NAME_ENV);

            // For local dev, rely on an auth file, otherwise on a service principal set in the environment of the build agent.
            var localAuthFile = Environment.GetEnvironmentVariable(LOCAL_DEV_ENV);

            if (string.IsNullOrWhiteSpace(localAuthFile))
            {
                var clientId     = Environment.GetEnvironmentVariable(CLIENT_ID_ENV);
                var clientSecret = Environment.GetEnvironmentVariable(CLIENT_SECRET_ENV);
                var tenantId     = Environment.GetEnvironmentVariable(TENANT_ID_ENV);

                credentials = credFactory.FromServicePrincipal(clientId, clientSecret, tenantId, AzureEnvironment.AzureGlobalCloud);
            }
            else
            {
                // Create the file with "az ad sp create-for-rbac --sdk-auth"
                credentials = credFactory.FromFile(localAuthFile);

                // Assign the default subscription ID if none has been provided in the environment
                if (string.IsNullOrWhiteSpace(this.subscriptionId))
                {
                    this.subscriptionId = credentials.DefaultSubscriptionId;
                }
            }

            // Create the rest client based on the authentication above.
            TestAzure = Az.Azure.Configure()
                        .WithLogLevel(Microsoft.Azure.Management.ResourceManager.Fluent.Core.HttpLoggingDelegatingHandler.Level.Headers)
                        .Authenticate(credentials)
                        .WithDefaultSubscription();
        }