Skip to content

Commit 1a0ee8a

Browse files
author
Brian McCallister
committed
simplification
1 parent 209ef8a commit 1a0ee8a

File tree

20 files changed

+71
-27
lines changed

20 files changed

+71
-27
lines changed

Makefile

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
DOCKER_TEST_SSHD_VERSION := 4
2+
3+
.PHONY: all
4+
all: test build ## run tests and build binaries
5+
6+
epithet-agent:
7+
go build ./cmd/epithet-agent
8+
9+
epithet-ca:
10+
go build ./cmd/epithet-ca
11+
12+
.PHONY: build
13+
build: epithet-agent epithet-ca
14+
15+
.PHONY: test
16+
test: test-support ## build and run test plumbing
17+
go test ./...
18+
19+
test/test_sshd/.built_$(DOCKER_TEST_SSHD_VERSION):
20+
cd test/test_sshd; docker build -t brianm/epithet-test-sshd:$(DOCKER_TEST_SSHD_VERSION) .; touch .built_$(DOCKER_TEST_SSHD_VERSION)
21+
22+
.PHONY: test-support
23+
test-support: test/test_sshd/.built_$(DOCKER_TEST_SSHD_VERSION)
24+
25+
.PHONY: clean
26+
clean: ## clean all local resources
27+
go clean ./...
28+
rm -f epithet-*
29+
30+
.PHONY: clean-all
31+
clean-all: clean
32+
rm -f test/test_sshd/.built_*
33+
docker rmi -f brianm/epithet-test-sshd:$(DOCKER_TEST_SSHD_VERSION)
34+
35+
.PHONY: help
36+
help: ## Show this help.
37+
@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//'

cmd/epithet-agent/epithet-agent.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"context"
54
"fmt"
65
"os"
76
"os/exec"
@@ -26,15 +25,12 @@ func main() {
2625
}
2726

2827
func run(cc *cobra.Command, args []string) error {
28+
a, err := agent.Start()
2929

30-
ctx, cancel := context.WithCancel(context.Background())
31-
a, err := agent.Start(agent.WithContext(ctx))
3230
sigs := make(chan os.Signal, 1)
3331
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
3432
go func() {
3533
<-sigs
36-
fmt.Println("exiting")
37-
cancel()
3834
a.Close()
3935
}()
4036

@@ -62,7 +58,7 @@ func run(cc *cobra.Command, args []string) error {
6258
cmd.Env = fixEnv(a.AuthSocketPath(), os.Environ())
6359

6460
err = cmd.Run()
65-
cancel()
61+
a.Close()
6662
return err
6763
}
6864

internal/agent/agent_test.go

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package agent
22

33
import (
4-
"context"
54
"fmt"
65
"log"
76
"net"
@@ -15,32 +14,27 @@ import (
1514
)
1615

1716
func TestBasics(t *testing.T) {
18-
ctx, cancel := context.WithCancel(context.Background())
19-
defer cancel()
20-
21-
closer, port, err := startServer()
17+
a, err := Start()
2218
require.NoError(t, err)
23-
defer closer()
2419

25-
a, err := Start(WithContext(ctx))
20+
server, err := startServer()
2621
require.NoError(t, err)
22+
defer server.Close()
2723

2824
err = a.UseCredential(Credential{
2925
PrivateKey: []byte(privateKey),
3026
Certificate: []byte(certificate),
3127
})
3228
require.NoError(t, err)
3329

34-
cmd := exec.Command("ssh", "-o", "UserKnownHostsFile=/dev/null", "-o", "StrictHostKeyChecking=no", "-p", port, "root@localhost", "ls", "/etc/ssh/")
35-
cmd.Env = fixEnv(a.AuthSocketPath(), os.Environ())
36-
37-
out, err := cmd.CombinedOutput()
30+
out, err := server.ssh(a, "ls", "/etc/ssh/")
3831

32+
fmt.Printf(out)
3933
require.NoError(t, err)
4034

41-
require.Contains(t, string(out), "sshd_config")
42-
require.Contains(t, string(out), "auth_principals")
43-
require.Contains(t, string(out), "ca.pub")
35+
require.Contains(t, out, "sshd_config")
36+
require.Contains(t, out, "auth_principals")
37+
require.Contains(t, out, "ca.pub")
4438

4539
err = a.Close()
4640
require.NoError(t, err)
@@ -61,13 +55,32 @@ func fixEnv(path string, env []string) []string {
6155
return append(newEnv, fmt.Sprintf("SSH_AUTH_SOCK=%s", path))
6256
}
6357

64-
func startServer() (closer func(), port string, err error) {
58+
type sshServer struct {
59+
*dockertest.Resource
60+
}
61+
62+
func (s sshServer) Port() string {
63+
return s.GetPort("22/tcp")
64+
}
65+
66+
func (s sshServer) ssh(a *Agent, args ...string) (string, error) {
67+
argv := []string{"-o", "UserKnownHostsFile=/dev/null", "-o", "StrictHostKeyChecking=no", "-p", s.Port(), "root@localhost"}
68+
for _, v := range args {
69+
argv = append(argv, v)
70+
}
71+
cmd := exec.Command("ssh", argv...)
72+
cmd.Env = fixEnv(a.AuthSocketPath(), os.Environ())
73+
bs, err := cmd.CombinedOutput()
74+
return string(bs), err
75+
}
76+
77+
func startServer() (*sshServer, error) {
6578
pool, err := dockertest.NewPool("")
6679
if err != nil {
6780
log.Fatalf("Could not connect to docker: %s", err)
6881
}
6982
// pulls an image, creates a container based on it and runs it
70-
resource, err := pool.Run("sshd", "3", []string{})
83+
resource, err := pool.Run("brianm/epithet-test-sshd", "4", []string{})
7184
if err != nil {
7285
log.Fatalf("Could not start resource: %s", err)
7386
}
@@ -83,10 +96,10 @@ func startServer() (closer func(), port string, err error) {
8396
conn.Close()
8497
return nil
8598
}); err != nil {
86-
return nil, "", fmt.Errorf("Could not connect to docker: %w", err)
99+
return nil, fmt.Errorf("Could not connect to docker: %w", err)
87100
}
88101

89-
return func() { resource.Close() }, resource.GetPort("22/tcp"), err
102+
return &sshServer{resource}, err
90103
}
91104

92105
const privateKey = `-----BEGIN OPENSSH PRIVATE KEY-----

play/PLAY.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

play/ca/ca renamed to test/ca/ca

File renamed without changes.
File renamed without changes.

test/test_sshd/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.built_*
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)