This repository has been archived by the owner on Nov 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
182 lines (167 loc) · 6.18 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"bufio"
"flag"
"io"
"log"
"math"
"net/url"
"os"
"os/exec"
"strings"
"github.com/Amnesic-Systems/nitriding/internal"
)
var (
elog = log.New(os.Stderr, "nitriding: ", log.Ldate|log.Ltime|log.LUTC|log.Lshortfile)
)
func main() {
var fqdn, fqdnLeader, appURL, appWebSrv, appCmd, prometheusNamespace, mockCertFp string
var extPubPort, extPrivPort, intPort, hostProxyPort, prometheusPort uint
var useACME, waitForApp, useProfiling, useVsockForExtPort, disableKeepAlives, debug bool
var err error
flag.StringVar(&fqdn, "fqdn", "",
"FQDN of the enclave application (e.g., \"example.com\").")
flag.StringVar(&fqdnLeader, "fqdn-leader", "",
"FQDN of the leader enclave (e.g., \"leader.example.com\"). Setting this enables key synchronization.")
flag.StringVar(&appURL, "app-url", "",
"Code repository of the enclave application (e.g., \"github.com/foo/bar\").")
flag.StringVar(&appWebSrv, "appwebsrv", "",
"Enclave-internal HTTP server of the enclave application (e.g., \"http://127.0.0.1:8081\").")
flag.StringVar(&appCmd, "app-cmd", "",
"Launch enclave application via the given command.")
flag.StringVar(&prometheusNamespace, "prometheus-namespace", "",
"Prometheus namespace for exported metrics.")
flag.UintVar(&extPubPort, "ext-pub-port", 443,
"Nitriding's external, public HTTPS port. Must match port forwarding rules on EC2 host.")
flag.UintVar(&extPrivPort, "ext-priv-port", 444,
"Nitriding's external, non-public HTTPS port. Must match port forwarding rules on the EC2 host.")
flag.BoolVar(&disableKeepAlives, "disable-keep-alives", false,
"Disables keep-alive connections for the HTTPS service.")
flag.BoolVar(&useVsockForExtPort, "vsock-ext", false,
"Listen on VSOCK interface for HTTPS port.")
flag.UintVar(&intPort, "int-port", 8080,
"Nitriding's enclave-internal HTTP port. Only used by the enclave application.")
flag.UintVar(&hostProxyPort, "host-proxy-port", 1024,
"Port of proxy application running on EC2 host.")
flag.UintVar(&prometheusPort, "prometheus-port", 0,
"Port to expose Prometheus metrics at.")
flag.BoolVar(&useProfiling, "profile", false,
"Enable pprof profiling. Only useful for debugging and must not be used in production.")
flag.BoolVar(&useACME, "acme", false,
"Use Let's Encrypt's ACME to fetch HTTPS certificate.")
flag.BoolVar(&waitForApp, "wait-for-app", false,
"Start Internet-facing Web server only after application signals its readiness.")
flag.BoolVar(&debug, "debug", false,
"Print extra debug messages and use dummy attester for testing outside enclaves.")
flag.StringVar(&mockCertFp, "mock-cert-fp", "",
"Mock certificate fingerprint to use in attestation documents (hexadecimal)")
flag.Parse()
if fqdn == "" {
elog.Fatalf("-fqdn must be set.")
}
if extPubPort < 1 || extPubPort > math.MaxUint16 {
elog.Fatalf("-extport must be in interval [1, %d]", math.MaxUint16)
}
if extPrivPort < 1 || extPrivPort > math.MaxUint16 {
elog.Fatalf("-extPrivPort must be in interval [1, %d]", math.MaxUint16)
}
if intPort < 1 || intPort > math.MaxUint16 {
elog.Fatalf("-intport must be in interval [1, %d]", math.MaxUint16)
}
if hostProxyPort < 1 || hostProxyPort > math.MaxUint32 {
elog.Fatalf("-host-proxy-port must be in interval [1, %d]", math.MaxUint32)
}
if prometheusPort > math.MaxUint16 {
elog.Fatalf("-prometheus-port must be in interval [1, %d]", math.MaxUint16)
}
if prometheusPort != 0 && prometheusNamespace == "" {
elog.Fatalf("-prometheus-namespace must be set when Prometheus is used.")
}
c := &internal.Config{
FQDN: fqdn,
FQDNLeader: fqdnLeader,
ExtPubPort: uint16(extPubPort),
ExtPrivPort: uint16(extPrivPort),
IntPort: uint16(intPort),
UseVsockForExtPort: useVsockForExtPort,
DisableKeepAlives: disableKeepAlives,
PrometheusPort: uint16(prometheusPort),
PrometheusNamespace: prometheusNamespace,
HostProxyPort: uint32(hostProxyPort),
UseACME: useACME,
WaitForApp: waitForApp,
UseProfiling: useProfiling,
MockCertFp: mockCertFp,
Debug: debug,
}
if appURL != "" {
u, err := url.Parse(appURL)
if err != nil {
elog.Fatalf("Failed to parse application URL: %v", err)
}
c.AppURL = u
}
if appWebSrv != "" {
u, err := url.Parse(appWebSrv)
if err != nil {
elog.Fatalf("Failed to parse URL of Web server: %v", err)
}
c.AppWebSrv = u
}
if debug {
elog.Println("WARNING: Using debug mode, which must not be enabled in production!")
}
enclave, err := internal.NewEnclave(c)
if err != nil {
elog.Fatalf("Failed to create enclave: %v", err)
}
if err := enclave.Start(); err != nil {
elog.Fatalf("Enclave terminated: %v", err)
}
// Nitriding supports two ways of starting the enclave application:
//
// 1) Nitriding spawns the enclave application itself, and waits for it
// to terminate.
//
// 2) The enclave application is started by a shell script (which also
// starts nitriding). In this case, we simply block forever.
if appCmd != "" {
runAppCommand(appCmd, func(s string) { elog.Printf("> %s", s) })
} else {
// Block forever.
<-make(chan struct{})
}
elog.Println("Exiting nitriding.")
}
// runAppCommand (i) runs the given command, (ii) waits until the command
// finished execution, and (iii) in the meanwhile prints the command's stdout
// and stderr.
func runAppCommand(appCmd string, f func(string)) {
var (
err error
stdout, stderr io.ReadCloser
)
elog.Printf("Invoking command: %s", appCmd)
args := strings.Split(appCmd, " ")
cmd := exec.Command(args[0], args[1:]...)
if stderr, err = cmd.StderrPipe(); err != nil {
elog.Fatalf("Error obtaining stderr pipe: %v", err)
}
if stdout, err = cmd.StdoutPipe(); err != nil {
elog.Fatalf("Error obtaining stdout pipe: %v", err)
}
if err := cmd.Start(); err != nil {
elog.Fatalf("Error starting application: %v", err)
}
s := bufio.NewScanner(io.MultiReader(stdout, stderr))
for s.Scan() {
f(s.Text())
}
if err := s.Err(); err != nil {
elog.Printf("Error reading from application: %v", err)
}
if err := cmd.Wait(); err != nil {
elog.Fatalf("Enclave application exited with non-0 exit code: %v", err)
}
elog.Println("Enclave application exited.")
}