github.com/argoproj/argo-cd/v3@v3.2.1/test/e2e/fixture/certs/certs.go (about)

     1  package certs
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/argoproj/argo-cd/v3/test/e2e/fixture"
    11  	"github.com/argoproj/argo-cd/v3/util/errors"
    12  )
    13  
    14  // Add a custom CA certificate to the test and also create the certificate file
    15  // on the file system, so argocd-server and argocd-repo-server can use it.
    16  func AddCustomCACert(t *testing.T) {
    17  	t.Helper()
    18  	caCertPath, err := filepath.Abs("../fixture/certs/argocd-test-ca.crt")
    19  	require.NoError(t, err)
    20  	// We need to setup TLS certs according to whether we are running tests
    21  	// against a local workload (repositories available as localhost) and
    22  	// against remote workloads (repositories available as argocd-e2e-server)
    23  	if fixture.IsLocal() {
    24  		args := []string{"cert", "add-tls", "localhost", "--from", caCertPath}
    25  		errors.NewHandler(t).FailOnErr(fixture.RunCli(args...))
    26  		args = []string{"cert", "add-tls", "127.0.0.1", "--from", caCertPath}
    27  		errors.NewHandler(t).FailOnErr(fixture.RunCli(args...))
    28  		certData, err := os.ReadFile(caCertPath)
    29  		require.NoError(t, err)
    30  		err = os.WriteFile(fixture.TmpDir+"/app/config/tls/localhost", certData, 0o644)
    31  		require.NoError(t, err)
    32  		err = os.WriteFile(fixture.TmpDir+"/app/config/tls/127.0.0.1", certData, 0o644)
    33  		require.NoError(t, err)
    34  	} else {
    35  		args := []string{"cert", "add-tls", "argocd-e2e-server", "--from", caCertPath}
    36  		errors.NewHandler(t).FailOnErr(fixture.RunCli(args...))
    37  		fixture.RestartAPIServer(t)
    38  		fixture.RestartRepoServer(t)
    39  	}
    40  }
    41  
    42  // AddCustomSSHKnownHostsKeys adds SSH known hosts data to the Argo CD server
    43  // being tested against. The env ARGOCD_E2E_SSH_KNOWN_HOSTS lets you specify
    44  // an optional path to the known hosts file, instead of using the default one.
    45  func AddCustomSSHKnownHostsKeys(t *testing.T) {
    46  	t.Helper()
    47  	source := os.Getenv("ARGOCD_E2E_SSH_KNOWN_HOSTS")
    48  	if source == "" {
    49  		source = "../fixture/testrepos/ssh_known_hosts"
    50  	}
    51  	knownHostsPath, err := filepath.Abs(source)
    52  	require.NoError(t, err)
    53  	args := []string{"cert", "add-ssh", "--upsert", "--batch", "--from", knownHostsPath}
    54  	errors.NewHandler(t).FailOnErr(fixture.RunCli(args...))
    55  
    56  	if fixture.IsLocal() {
    57  		knownHostsData, err := os.ReadFile(knownHostsPath)
    58  		require.NoError(t, err)
    59  		err = os.WriteFile(fixture.TmpDir+"/app/config/ssh/ssh_known_hosts", knownHostsData, 0o644)
    60  		require.NoError(t, err)
    61  	} else {
    62  		fixture.RestartAPIServer(t)
    63  		fixture.RestartRepoServer(t)
    64  	}
    65  }