-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinit.go
193 lines (177 loc) · 6.9 KB
/
init.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
183
184
185
186
187
188
189
190
191
192
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
"time"
"unicode"
"golang.org/x/exp/slices"
"google.golang.org/grpc/metadata"
)
var (
flMode string
flIPv6 bool
flPort int
flUserAgent string
flEndpoint string
flHTTPCodes string
flService string
flConnTimeout time.Duration
flRPCHeaders = rpcHeaders{MD: make(metadata.MD)}
flRPCTimeout time.Duration
flTLS bool
flTLSNoVerify bool
flTLSCACert string
flTLSClientCert string
flTLSClientKey string
flTLSServerName string
flALTS bool
flVerbose bool
flGZIP bool
flSPIFFE bool
)
const (
// LocalAddress to call
LocalAddress = "127.0.0.1"
// LocalAddress6 IPv6 to call
LocalAddress6 = "[::1]"
// StatusInvalidArguments indicates specified invalid arguments.
StatusInvalidArguments = 1
// StatusConnectionFailure indicates connection failed.
StatusConnectionFailure = 2
// StatusRPCFailure indicates rpc failed.
StatusRPCFailure = 3
// StatusUnhealthy indicates rpc succeeded but indicates unhealthy service.
StatusUnhealthy = 4
// StatusSpiffeFailed indicates failure to retrieve credentials using spiffe workload API
StatusSpiffeFailed = 20
)
func getSupportedModes() []string {
return []string{"http", "grpc"}
}
func getAddr() string {
if flIPv6 {
return LocalAddress6
}
return LocalAddress
}
func init() {
flagSet := flag.NewFlagSet("", flag.ContinueOnError)
log.SetFlags(0)
// core settings
flagSet.StringVar(&flMode, "mode", "http", "Select mode: http, grpc (default: http)")
flagSet.BoolVar(&flIPv6, "ipv6", false, "Use IPv6 ::1 address (default: false)")
flagSet.IntVar(&flPort, "port", 8080, "port number to check (defaut 8080)")
flagSet.StringVar(&flUserAgent, "user-agent", "lprobe", "user-agent header value of health check requests")
// HTTP settings
flagSet.StringVar(&flEndpoint, "endpoint", "/", "HTTP endpoint (default: /)")
flagSet.StringVar(&flHTTPCodes, "http-codes", "200-299", "comma-separated list of expected HTTP status codes (default: 200-299) with optional ranges (e.g. 200,201,202-204,301-303")
// gRPC settings
flagSet.StringVar(&flService, "service", "", "service name to check (default: \"\")")
// timeouts
flagSet.DurationVar(&flConnTimeout, "connect-timeout", time.Second, "timeout for establishing connection")
// headers
flagSet.Var(&flRPCHeaders, "rpc-header", "additional RPC headers in 'name: value' format. May specify more than one via multiple flags.")
flagSet.DurationVar(&flRPCTimeout, "rpc-timeout", time.Second, "timeout for health check rpc")
// tls settings
flagSet.BoolVar(&flTLS, "tls", false, "use TLS (default: false, INSECURE plaintext transport)")
flagSet.BoolVar(&flTLSNoVerify, "tls-no-verify", false, "(with -tls) don't verify the certificate (INSECURE) presented by the server (default: false)")
flagSet.StringVar(&flTLSCACert, "tls-ca-cert", "", "(with -tls, optional) file containing trusted certificates for verifying server")
flagSet.StringVar(&flTLSClientCert, "tls-client-cert", "", "(with -tls, optional) client certificate for authenticating to the server (requires -tls-client-key)")
flagSet.StringVar(&flTLSClientKey, "tls-client-key", "", "(with -tls) client private key for authenticating to the server (requires -tls-client-cert)")
flagSet.StringVar(&flTLSServerName, "tls-server-name", "", "(with -tls) override the hostname used to verify the server certificate")
flagSet.BoolVar(&flALTS, "alts", false, "use ALTS (default: false, INSECURE plaintext transport)")
flagSet.BoolVar(&flVerbose, "v", false, "verbose logs")
flagSet.BoolVar(&flGZIP, "gzip", false, "use GZIPCompressor for requests and GZIPDecompressor for response (default: false)")
flagSet.BoolVar(&flSPIFFE, "spiffe", false, "use SPIFFE to obtain mTLS credentials")
err := flagSet.Parse(os.Args[1:])
if err != nil {
os.Exit(StatusInvalidArguments)
}
argError := func(s string, v ...interface{}) {
log.Printf("error: "+s, v...)
os.Exit(StatusInvalidArguments)
}
if !slices.Contains(getSupportedModes(), flMode) {
argError("Unsupported -mode. Please use one of %v", getSupportedModes())
}
if flConnTimeout <= 0 {
argError("-connect-timeout must be greater than zero (specified: %v)", flConnTimeout)
}
if flRPCTimeout <= 0 {
argError("-rpc-timeout must be greater than zero (specified: %v)", flRPCTimeout)
}
if flALTS && flSPIFFE {
argError("-alts and -spiffe are mutually incompatible")
}
if flTLS && flALTS {
argError("cannot specify -tls with -alts")
}
if !flTLS && flTLSNoVerify {
argError("specified -tls-no-verify without specifying -tls")
}
if !flTLS && flTLSCACert != "" {
argError("specified -tls-ca-cert without specifying -tls")
}
if !flTLS && flTLSClientCert != "" {
argError("specified -tls-client-cert without specifying -tls")
}
if !flTLS && flTLSServerName != "" {
argError("specified -tls-server-name without specifying -tls")
}
if flTLSClientCert != "" && flTLSClientKey == "" {
argError("specified -tls-client-cert without specifying -tls-client-key")
}
if flTLSClientCert == "" && flTLSClientKey != "" {
argError("specified -tls-client-key without specifying -tls-client-cert")
}
if flTLSNoVerify && flTLSCACert != "" {
argError("cannot specify -tls-ca-cert with -tls-no-verify (CA cert would not be used)")
}
if flTLSNoVerify && flTLSServerName != "" {
argError("cannot specify -tls-server-name with -tls-no-verify (server name would not be used)")
}
if flVerbose {
log.Printf("parsed options:")
log.Printf("> conn_timeout=%v rpc_timeout=%v", flConnTimeout, flRPCTimeout)
if flRPCHeaders.Len() > 0 {
log.Printf("> headers: %s", flRPCHeaders)
}
log.Printf("> tls=%v", flTLS)
if flTLS {
log.Printf(" > no-verify=%v ", flTLSNoVerify)
log.Printf(" > ca-cert=%s", flTLSCACert)
log.Printf(" > client-cert=%s", flTLSClientCert)
log.Printf(" > client-key=%s", flTLSClientKey)
log.Printf(" > server-name=%s", flTLSServerName)
}
log.Printf("> alts=%v", flALTS)
log.Printf("> spiffe=%v", flSPIFFE)
}
}
type rpcHeaders struct{ metadata.MD }
func (s *rpcHeaders) String() string { return fmt.Sprintf("%v", s.MD) }
func (s *rpcHeaders) Set(value string) error {
parts := strings.SplitN(value, ":", 2)
if len(parts) != 2 {
return fmt.Errorf("invalid RPC header, expected 'key: value', got %q", value)
}
trimmed := strings.TrimLeftFunc(parts[1], unicode.IsSpace)
s.Append(parts[0], trimmed)
return nil
}