-
Notifications
You must be signed in to change notification settings - Fork 6
/
client_config.go
123 lines (106 loc) · 3.34 KB
/
client_config.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
package main
import (
"context"
"fmt"
"os"
"strconv"
"time"
"github.com/spf13/pflag"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/status"
"github.com/CrowdStrike/perseus/perseusapi"
)
// package variables to hold CLI flag values
var (
formatAsJSON, formatAsList, formatAsDotGraph bool
formatTemplate string
maxDepth int
disableTLS bool
)
// clientConfig defines the runtime options for the "client" CLI commands
type clientConfig struct {
// the TCP host/port of the Perseus server
serverAddr string
// do not use TLS when connecting if true
disableTLS bool
}
// clientOption defines a functional option that configures a particular "client" CLI runtime option
type clientOption func(*clientConfig) error
// withServerAddress assigns the TCP host/port of the Perseus server
func withServerAddress(addr string) clientOption {
return func(conf *clientConfig) error {
conf.serverAddr = addr
return nil
}
}
// withInsecureDial disables TLS when connecting to the server
func withInsecureDial() clientOption {
return func(conf *clientConfig) error {
conf.disableTLS = true
return nil
}
}
// readClientConfig scans the process environment vars and returns a list of 0 or more config options
func readClientConfigEnv() []clientOption {
var opts []clientOption
if addr := os.Getenv("PERSEUS_SERVER_ADDR"); addr != "" {
opts = append(opts, withServerAddress(addr))
}
if s := os.Getenv("PERSEUS_SERVER_NO_TLS"); s != "" {
val, err := strconv.ParseBool(s)
if val && err != nil {
opts = append(opts, withInsecureDial())
}
}
return opts
}
// readClientConfigFlags scans the CLI flags in the provided flag set and returns a list of 0 or more
// config options
func readClientConfigFlags(fset *pflag.FlagSet) []clientOption {
var opts []clientOption
if addr, err := fset.GetString("server-addr"); err == nil && addr != "" {
opts = append(opts, withServerAddress(addr))
}
if v, err := fset.GetBool("insecure"); err == nil && v {
opts = append(opts, withInsecureDial())
}
return opts
}
func (conf *clientConfig) dialServer() (client perseusapi.PerseusServiceClient, err error) {
// translate RPC errors to human-friendly ones on return
defer func() {
switch err {
case context.DeadlineExceeded:
err = fmt.Errorf("timed out trying to connect to the Perseus server")
default:
if err != nil {
switch status.Code(err) {
case codes.Unavailable:
err = fmt.Errorf("unable to connect to the Perseus server")
default:
}
}
}
}()
// setup gRPC connection options and connect
dialOpts := []grpc.DialOption{
grpc.WithBlock(),
}
if conf.disableTLS {
dialOpts = append(dialOpts, grpc.WithTransportCredentials(insecure.NewCredentials()))
} else {
dialOpts = append(dialOpts, grpc.WithTransportCredentials(credentials.NewTLS(nil)))
}
debugLog("connecting to Perseus server", "addr", conf.serverAddr, "useTLS", !conf.disableTLS)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
conn, err := grpc.DialContext(ctx, conf.serverAddr, dialOpts...)
if err != nil {
return nil, err
}
// create and return the client
return perseusapi.NewPerseusServiceClient(conn), nil
}