From 1cae6f988ef38de821b06bbef7e85c018009e26e Mon Sep 17 00:00:00 2001 From: Gerardo Viedma Date: Fri, 16 Mar 2018 15:40:58 +0000 Subject: [PATCH] Make PKI data and RunnerFactory public objects (#865) * Make PKI data and RunnerFactory public objects * removes unnecessary nullRunner object * renames secure factory to point out MTLS --- api/agent/lb_agent_test.go | 4 ++-- api/agent/runner_client.go | 38 ++++------------------------------- api/runnerpool/runner_pool.go | 9 +++++++++ 3 files changed, 15 insertions(+), 36 deletions(-) diff --git a/api/agent/lb_agent_test.go b/api/agent/lb_agent_test.go index 0c32d64ca..de852e42b 100644 --- a/api/agent/lb_agent_test.go +++ b/api/agent/lb_agent_test.go @@ -27,7 +27,7 @@ type mockRunner struct { type mockRunnerPool struct { runners []pool.Runner generator insecureRunnerFactory - pki *pkiData + pki *pool.PKIData } func newMockRunnerPool(rf insecureRunnerFactory, runnerAddrs []string) *mockRunnerPool { @@ -43,7 +43,7 @@ func newMockRunnerPool(rf insecureRunnerFactory, runnerAddrs []string) *mockRunn return &mockRunnerPool{ runners: runners, generator: rf, - pki: &pkiData{}, + pki: &pool.PKIData{}, } } diff --git a/api/agent/runner_client.go b/api/agent/runner_client.go index b554e0692..d5ad81aca 100644 --- a/api/agent/runner_client.go +++ b/api/agent/runner_client.go @@ -16,28 +16,6 @@ import ( "github.com/sirupsen/logrus" ) -type pkiData struct { - ca string - key string - cert string -} - -type nullRunner struct{} - -func (n *nullRunner) TryExec(ctx context.Context, call pool.RunnerCall) (bool, error) { - return false, nil -} - -func (n *nullRunner) Close() error { - return nil -} - -func (n *nullRunner) Address() string { - return "" -} - -var nullRunnerSingleton = new(nullRunner) - type gRPCRunner struct { // Need a WaitGroup of TryExec in flight wg sync.WaitGroup @@ -46,16 +24,8 @@ type gRPCRunner struct { client pb.RunnerProtocolClient } -// allow factory to be overridden in tests -type secureRunnerFactory func(addr string, cert string, key string, ca string) (pool.Runner, error) - -func secureGRPCRunnerFactory(addr string, cert string, key string, ca string) (pool.Runner, error) { - p := &pkiData{ - cert: cert, - key: key, - ca: ca, - } - conn, client, err := runnerConnection(addr, p) +func SecureGRPCRunnerFactory(addr string, pki *pool.PKIData) (pool.Runner, error) { + conn, client, err := runnerConnection(addr, pki) if err != nil { return nil, err } @@ -85,13 +55,13 @@ func (r *gRPCRunner) Close(ctx context.Context) error { } } -func runnerConnection(address string, pki *pkiData) (*grpc.ClientConn, pb.RunnerProtocolClient, error) { +func runnerConnection(address string, pki *pool.PKIData) (*grpc.ClientConn, pb.RunnerProtocolClient, error) { ctx := context.Background() var creds credentials.TransportCredentials if pki != nil { var err error - creds, err = grpcutil.CreateCredentials(pki.cert, pki.key, pki.ca) + creds, err = grpcutil.CreateCredentials(pki.Cert, pki.Key, pki.Ca) if err != nil { logrus.WithError(err).Error("Unable to create credentials to connect to runner node") return nil, nil, err diff --git a/api/runnerpool/runner_pool.go b/api/runnerpool/runner_pool.go index 9db4d324a..a6d333ab3 100644 --- a/api/runnerpool/runner_pool.go +++ b/api/runnerpool/runner_pool.go @@ -21,6 +21,15 @@ type RunnerPool interface { Shutdown(context.Context) error } +type PKIData struct { + Ca string + Key string + Cert string +} + +// MTLSRunnerFactory represents a factory method for constructing runners using mTLS +type MTLSRunnerFactory func(addr string, pki *PKIData) (Runner, error) + // Runner is the interface to invoke the execution of a function call on a specific runner type Runner interface { TryExec(ctx context.Context, call RunnerCall) (bool, error)