<commit_before>package actors

import (
	"fmt"

	"github.com/Azure/azure-sdk-for-go/arm/network"
	"github.com/Azure/go-autorest/autorest"
	"github.com/Azure/go-autorest/autorest/adal"
	"github.com/Azure/go-autorest/autorest/azure"
	acceptance "github.com/cloudfoundry/bosh-bootloader/acceptance-tests"
	"github.com/cloudfoundry/bosh-bootloader/testhelpers"

	. "github.com/onsi/gomega"
)

type azureLBHelper struct {
	applicationGatewaysClient *network.ApplicationGatewaysClient
	loadBalancersClient       *network.LoadBalancersClient
}

func NewAzureLBHelper(config acceptance.Config) azureLBHelper {
	oauthConfig, err := adal.NewOAuthConfig(azure.PublicCloud.ActiveDirectoryEndpoint, config.AzureTenantID)
	Expect(err).NotTo(HaveOccurred())

	servicePrincipalToken, err := adal.NewServicePrincipalToken(*oauthConfig, config.AzureClientID, config.AzureClientSecret, azure.PublicCloud.ResourceManagerEndpoint)
	Expect(err).NotTo(HaveOccurred())

	agc := network.NewApplicationGatewaysClient(config.AzureSubscriptionID)
	agc.ManagementClient.Authorizer = autorest.NewBearerAuthorizer(servicePrincipalToken)
	agc.ManagementClient.Sender = autorest.CreateSender(autorest.AsIs())

	lbc := network.NewLoadBalancersClient(config.AzureSubscriptionID)
	lbc.ManagementClient.Authorizer = autorest.NewBearerAuthorizer(servicePrincipalToken)
	lbc.ManagementClient.Sender = autorest.CreateSender(autorest.AsIs())

	return azureLBHelper{
		loadBalancersClient:       &lbc,
		applicationGatewaysClient: &agc,
	}
}

func (z azureLBHelper) getLoadBalancer(resourceGroupName, loadBalancerName string) (bool, error) {
	_, err := z.loadBalancersClient.Get(fmt.Sprintf("%s-bosh", resourceGroupName), loadBalancerName, "")
	if err != nil {
		if aerr, ok := err.(autorest.DetailedError); ok {
			if aerr.StatusCode.(int) == 404 {
				return false, nil
			}
		}
		return false, err
	}

	return true, nil
}

func (z azureLBHelper) getApplicationGateway(resourceGroupName, applicationGatewayName string) (bool, error) {
	_, err := z.applicationGatewaysClient.Get(fmt.Sprintf("%s-bosh", resourceGroupName), applicationGatewayName)
	if err != nil {
		if aerr, ok := err.(autorest.DetailedError); ok {
			if aerr.StatusCode.(int) == 404 {
				return false, nil
			}
		}
		return false, err
	}

	return true, nil
}

func (z azureLBHelper) GetLBArgs() []string {
	certPath, err := testhelpers.WriteContentsToTempFile(testhelpers.PFX_BASE64)
	Expect(err).NotTo(HaveOccurred())
	keyPath, err := testhelpers.WriteContentsToTempFile(testhelpers.PFX_PASSWORD)
	Expect(err).NotTo(HaveOccurred())

	return []string{
		"--lb-type", "cf",
		"--lb-cert", certPath,
		"--lb-key", keyPath,
		"--lb-domain", "azure.example.com",
	}
}

func (z azureLBHelper) VerifyCloudConfigExtensions(vmExtensions []string) {
	Expect(vmExtensions).To(ContainElement("lb"))
}

func (z azureLBHelper) ConfirmLBsExist(envID string) {
	exists, err := z.getLoadBalancer(envID, fmt.Sprintf("%s-cf-lb", envID))
	Expect(err).NotTo(HaveOccurred())
	Expect(exists).To(BeTrue())
}

func (z azureLBHelper) ConfirmNoLBsExist(envID string) {
	exists, err := z.getLoadBalancer(envID, fmt.Sprintf("%s-cf-lb", envID))
	Expect(err).NotTo(HaveOccurred())
	Expect(exists).To(BeFalse())<commit_msg>Decode pfx cert before passing to --lb-cert.<commit_after>package actors

import (
	"encoding/base64"
	"fmt"

	"github.com/Azure/azure-sdk-for-go/arm/network"
	"github.com/Azure/go-autorest/autorest"
	"github.com/Azure/go-autorest/autorest/adal"
	"github.com/Azure/go-autorest/autorest/azure"
	acceptance "github.com/cloudfoundry/bosh-bootloader/acceptance-tests"
	"github.com/cloudfoundry/bosh-bootloader/testhelpers"

	. "github.com/onsi/gomega"
)

type azureLBHelper struct {
	applicationGatewaysClient *network.ApplicationGatewaysClient
	loadBalancersClient       *network.LoadBalancersClient
}

func NewAzureLBHelper(config acceptance.Config) azureLBHelper {
	oauthConfig, err := adal.NewOAuthConfig(azure.PublicCloud.ActiveDirectoryEndpoint, config.AzureTenantID)
	Expect(err).NotTo(HaveOccurred())

	servicePrincipalToken, err := adal.NewServicePrincipalToken(*oauthConfig, config.AzureClientID, config.AzureClientSecret, azure.PublicCloud.ResourceManagerEndpoint)
	Expect(err).NotTo(HaveOccurred())

	agc := network.NewApplicationGatewaysClient(config.AzureSubscriptionID)
	agc.ManagementClient.Authorizer = autorest.NewBearerAuthorizer(servicePrincipalToken)
	agc.ManagementClient.Sender = autorest.CreateSender(autorest.AsIs())

	lbc := network.NewLoadBalancersClient(config.AzureSubscriptionID)
	lbc.ManagementClient.Authorizer = autorest.NewBearerAuthorizer(servicePrincipalToken)
	lbc.ManagementClient.Sender = autorest.CreateSender(autorest.AsIs())

	return azureLBHelper{
		loadBalancersClient:       &lbc,
		applicationGatewaysClient: &agc,
	}
}

func (z azureLBHelper) getLoadBalancer(resourceGroupName, loadBalancerName string) (bool, error) {
	_, err := z.loadBalancersClient.Get(fmt.Sprintf("%s-bosh", resourceGroupName), loadBalancerName, "")
	if err != nil {
		if aerr, ok := err.(autorest.DetailedError); ok {
			if aerr.StatusCode.(int) == 404 {
				return false, nil
			}
		}
		return false, err
	}

	return true, nil
}

func (z azureLBHelper) getApplicationGateway(resourceGroupName, applicationGatewayName string) (bool, error) {
	_, err := z.applicationGatewaysClient.Get(fmt.Sprintf("%s-bosh", resourceGroupName), applicationGatewayName)
	if err != nil {
		if aerr, ok := err.(autorest.DetailedError); ok {
			if aerr.StatusCode.(int) == 404 {
				return false, nil
			}
		}
		return false, err
	}

	return true, nil
}

func (z azureLBHelper) GetLBArgs() []string {
	decodedCert, err := base64.StdEncoding.DecodeString(testhelpers.PFX_BASE64)
	Expect(err).NotTo(HaveOccurred())
	certPath, err := testhelpers.WriteByteContentsToTempFile(decodedCert)
	Expect(err).NotTo(HaveOccurred())
	keyPath, err := testhelpers.WriteContentsToTempFile(testhelpers.PFX_PASSWORD)
	Expect(err).NotTo(HaveOccurred())

	return []string{
		"--lb-type", "cf",
		"--lb-cert", certPath,
		"--lb-key", keyPath,
		"--lb-domain", "azure.example.com",
	}
}

func (z azureLBHelper) VerifyCloudConfigExtensions(vmExtensions []string) {
	Expect(vmExtensions).To(ContainElement("lb"))
}

func (z azureLBHelper) ConfirmLBsExist(envID string) {
	exists, err := z.getLoadBalancer(envID, fmt.Sprintf("%s-cf-lb", envID))
	Expect(err).NotTo(HaveOccurred())
	Expect(exists).To(BeTrue())
}

func (z azureLBHelper) ConfirmNoLBsExist(envID string) {
	exists, err := z.getLoadBalancer(envID, fmt.Sprintf("%s-cf-lb", envID))
	Expect(err).NotTo(HaveOccurred())
	Expect(exists).To(BeFalse())
{'fraction_non_alphanumeric': 0.13271651751858032, 'fraction_numerical': 0.003033520400424693, 'mean_word_length': 5.955696202531645, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1341750', 'n_tokens_mistral': 2345, 'n_tokens_neox': 2223, 'n_words': 350}