1 package main
2
3 import (
4 "context"
5 "fmt"
6 "os"
7
8 srv "github.com/datawire/ambassador/v2/cmd/kat-server/services"
9 "github.com/datawire/dlib/dlog"
10 )
11
12 const (
13
14 Crt = "server.crt"
15
16 Key = "server.key"
17
18 Port int16 = 8080
19
20 SSLPort int16 = 8443
21 )
22
23 func main() {
24 ctx := context.Background()
25 listeners := make([]srv.Service, 0)
26 var s srv.Service
27
28 t := os.Getenv("KAT_BACKEND_TYPE")
29
30 if len(t) <= 0 {
31 t = "http"
32 }
33
34 dlog.Printf(ctx, "Running as type %s", t)
35
36 switch t {
37 case "grpc_echo":
38 s = &srv.GRPC{
39 Port: Port,
40 Backend: os.Getenv("BACKEND"),
41 SecurePort: SSLPort,
42 SecureBackend: os.Getenv("BACKEND"),
43 Cert: Crt,
44 Key: Key,
45 }
46
47 listeners = append(listeners, s)
48
49 case "grpc_auth":
50 protocolVersion := os.Getenv("GRPC_AUTH_PROTOCOL_VERSION")
51 if protocolVersion == "v3" {
52 s = &srv.GRPCAUTHV3{
53 Port: Port,
54 Backend: os.Getenv("BACKEND"),
55 SecurePort: SSLPort,
56 SecureBackend: os.Getenv("BACKEND"),
57 Cert: Crt,
58 Key: Key,
59 ProtocolVersion: protocolVersion,
60 }
61 } else {
62 s = &srv.GRPCAUTH{
63 Port: Port,
64 Backend: os.Getenv("BACKEND"),
65 SecurePort: SSLPort,
66 SecureBackend: os.Getenv("BACKEND"),
67 Cert: Crt,
68 Key: Key,
69 ProtocolVersion: protocolVersion,
70 }
71 }
72
73 listeners = append(listeners, s)
74
75 case "grpc_rls":
76 protocolVersion := os.Getenv("GRPC_RLS_PROTOCOL_VERSION")
77 if protocolVersion == "v3" {
78 s = &srv.GRPCRLSV3{
79 Port: Port,
80 Backend: os.Getenv("BACKEND"),
81 SecurePort: SSLPort,
82 SecureBackend: os.Getenv("BACKEND"),
83 Cert: Crt,
84 Key: Key,
85 ProtocolVersion: protocolVersion,
86 }
87
88 } else {
89 s = &srv.GRPCRLS{
90 Port: Port,
91 Backend: os.Getenv("BACKEND"),
92 SecurePort: SSLPort,
93 SecureBackend: os.Getenv("BACKEND"),
94 Cert: Crt,
95 Key: Key,
96 ProtocolVersion: protocolVersion,
97 }
98
99 }
100
101 listeners = append(listeners, s)
102 case "grpc_als":
103 s = &srv.GRPCALS{
104 HTTPListener: srv.HTTPListener{
105 CleartextPort: Port,
106 TLSPort: SSLPort,
107 TLSCert: Crt,
108 TLSKey: Key,
109 },
110 }
111 listeners = append(listeners, s)
112 case "grpc_agent":
113 s = &srv.GRPCAgent{
114 Port: Port,
115 }
116 listeners = append(listeners, s)
117
118 default:
119 port := Port
120 securePort := SSLPort
121
122 for {
123 eName := fmt.Sprintf("BACKEND_%d", port)
124 clearBackend := os.Getenv(eName)
125
126 dlog.Printf(ctx, "clear: checking %s -- %s", eName, clearBackend)
127
128 if len(clearBackend) <= 0 {
129 if port == 8080 {
130
131 clearBackend = os.Getenv("BACKEND")
132
133 dlog.Printf(ctx, "clear: fallback to BACKEND -- %s", clearBackend)
134 }
135 }
136
137 if len(clearBackend) <= 0 {
138 dlog.Printf(ctx, "clear: bailing, no backend")
139 break
140 }
141
142 eName = fmt.Sprintf("BACKEND_%d", securePort)
143 secureBackend := os.Getenv(eName)
144
145 dlog.Printf(ctx, "secure: checking %s -- %s", eName, secureBackend)
146
147 if len(secureBackend) <= 0 {
148 if securePort == 8443 {
149
150 secureBackend = os.Getenv("BACKEND")
151
152 dlog.Printf(ctx, "secure: fallback to BACKEND -- %s", clearBackend)
153 }
154 }
155
156 if len(secureBackend) <= 0 {
157 dlog.Printf(ctx, "secure: bailing, no backend")
158 break
159 }
160
161 if clearBackend != secureBackend {
162 dlog.Printf(ctx, "BACKEND_%d and BACKEND_%d do not match", port, securePort)
163 } else {
164 dlog.Printf(ctx, "creating HTTP listener for %s on ports %d/%d", clearBackend, port, securePort)
165
166 s = &srv.HTTP{
167 Port: port,
168 Backend: clearBackend,
169 SecurePort: securePort,
170 SecureBackend: secureBackend,
171 Cert: Crt,
172 Key: Key,
173 }
174
175 listeners = append(listeners, s)
176 }
177
178 port++
179 securePort++
180 }
181 }
182
183 if len(listeners) > 0 {
184 var waitFor <-chan bool
185 first := true
186
187 for _, s := range listeners {
188 c := s.Start(ctx)
189
190 if first {
191 waitFor = c
192 first = false
193 }
194 }
195
196 <-waitFor
197 } else {
198 dlog.Error(ctx, "no listeners, exiting")
199 os.Exit(1)
200 }
201 }
202
View as plain text