Skip to content

Commit a511f62

Browse files
committed
cleaning up caserver launch
1 parent acafc9a commit a511f62

File tree

5 files changed

+74
-143
lines changed

5 files changed

+74
-143
lines changed

Makefile

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@ all: test build ## run tests and build binaries
44
epithet:
55
go build ./cmd/epithet
66

7-
epithet-ca:
8-
go build ./cmd/epithet-ca
9-
107
.PHONY: build
11-
build: epithet epithet-ca
8+
build: epithet
129

1310
.PHONY: test
1411
test: ## run all tests
@@ -18,7 +15,6 @@ test: ## run all tests
1815
clean: ## clean all local resources
1916
go clean ./...
2017
go clean -testcache
21-
rm -f epithet-*
2218
rm -rf epithet
2319
rm -rf dist
2420

cmd/epithet-ca/epithet-ca.go

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

cmd/epithet/ca.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log/slog"
6+
"net/http"
7+
"os"
8+
"time"
9+
10+
"github.com/epithet-ssh/epithet/pkg/ca"
11+
"github.com/epithet-ssh/epithet/pkg/caserver"
12+
"github.com/epithet-ssh/epithet/pkg/sshcert"
13+
"github.com/go-chi/chi"
14+
"github.com/go-chi/chi/middleware"
15+
)
16+
17+
type CACLI struct {
18+
Policy string `help:"URL for policy service" short:"p" env:"POLICY_URL" required:"true"`
19+
Key string `help:"Path to ca private key" short:"k" default:"/etc/epithet/ca.key"`
20+
Address string `help:"Address to bind to" short:"a" env:"PORT" default:"0.0.0.0:8080"`
21+
}
22+
23+
func (c *CACLI) Run(logger *slog.Logger) error {
24+
logger.Debug("ca command called", "ca", c)
25+
26+
// Read CA private key
27+
privKey, err := os.ReadFile(c.Key)
28+
if err != nil {
29+
return fmt.Errorf("unable to load ca key: %w", err)
30+
}
31+
logger.Info("ca_key", "path", c.Key)
32+
logger.Info("policy_url", "url", c.Policy)
33+
34+
// Create CA
35+
caInstance, err := ca.New(sshcert.RawPrivateKey(string(privKey)), c.Policy)
36+
if err != nil {
37+
return fmt.Errorf("unable to create CA: %w", err)
38+
}
39+
40+
// Set up HTTP router
41+
r := chi.NewRouter()
42+
43+
// Middleware stack
44+
r.Use(middleware.RequestID)
45+
r.Use(middleware.RealIP)
46+
r.Use(middleware.Logger)
47+
r.Use(middleware.Recoverer)
48+
r.Use(middleware.Timeout(60 * time.Second))
49+
50+
r.Handle("/", caserver.New(caInstance, logger, nil))
51+
52+
logger.Info("listening", "address", c.Address)
53+
err = http.ListenAndServe(c.Address, r)
54+
if err != nil {
55+
return err
56+
}
57+
58+
return nil
59+
}

cmd/epithet/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var cli struct {
1717

1818
Agent AgentCLI `cmd:"agent" help:"start the epithet agent"`
1919
Match MatchCLI `cmd:"match" help:"Invoked during ssh invocation in a 'Match exec ...'"`
20+
CA CACLI `cmd:"ca" help:"Run the epithet CA server"`
2021
}
2122

2223
func main() {

pkg/caserver/caserver.go

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,29 @@ import (
44
"encoding/json"
55
"fmt"
66
"io"
7+
"log/slog"
78
"net/http"
89
"time"
910

1011
"github.com/epithet-ssh/epithet/pkg/ca"
1112
"github.com/epithet-ssh/epithet/pkg/policy"
1213
"github.com/epithet-ssh/epithet/pkg/sshcert"
13-
"github.com/sirupsen/logrus"
1414
)
1515

1616
type caServer struct {
1717
c *ca.CA
1818
httpClient *http.Client
19+
log *slog.Logger
1920
}
2021

2122
// New creates a new CA Server which needs to then
22-
// be atatched to some http server, a la
23+
// be attached to some http server, a la
2324
// `http.ListenAndServeTLS(...)`
24-
func New(c *ca.CA, options ...Option) http.Handler {
25+
func New(c *ca.CA, log *slog.Logger, httpClient *http.Client) http.Handler {
2526
cas := &caServer{
26-
c: c,
27-
}
28-
29-
for _, o := range options {
30-
o.apply(cas)
27+
c: c,
28+
log: log,
29+
httpClient: httpClient,
3130
}
3231

3332
if cas.httpClient == nil {
@@ -39,24 +38,6 @@ func New(c *ca.CA, options ...Option) http.Handler {
3938
return cas
4039
}
4140

42-
// Option configures the agent
43-
type Option interface {
44-
apply(*caServer)
45-
}
46-
47-
type optionFunc func(*caServer)
48-
49-
func (f optionFunc) apply(a *caServer) {
50-
f(a)
51-
}
52-
53-
// WithHTTPClient specifies the http client to use
54-
func WithHTTPClient(httpClient *http.Client) Option {
55-
return optionFunc(func(s *caServer) {
56-
s.httpClient = httpClient
57-
})
58-
}
59-
6041
func (s *caServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
6142
switch r.Method {
6243
case "GET":
@@ -98,7 +79,10 @@ func (s *caServer) createCert(w http.ResponseWriter, r *http.Request) {
9879
if err != nil {
9980
w.Header().Add("Content-type", "text/plain")
10081
w.WriteHeader(400)
101-
w.Write([]byte(fmt.Sprintf("unable to parse body: %s", err)))
82+
_, err := w.Write([]byte(fmt.Sprintf("unable to parse body: %s", err)))
83+
if err != nil {
84+
85+
}
10286
return
10387
}
10488

@@ -125,15 +109,15 @@ func (s *caServer) createCert(w http.ResponseWriter, r *http.Request) {
125109
out, err := json.Marshal(&resp)
126110
if err != nil {
127111
w.WriteHeader(500)
128-
logrus.Warn("unable to jsonify response: %w", err)
112+
s.log.Warn("unable to jsonify response", "error", err)
129113
return
130114
}
131115

132116
w.Header().Add("Content-type", "application/json")
133117
w.WriteHeader(200)
134118
_, err = w.Write(out)
135119
if err != nil {
136-
logrus.Warn("unable to write response: %w", err)
120+
s.log.Warn("unable to write response", "error", err)
137121
return
138122
}
139123
}

0 commit comments

Comments
 (0)