-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.go
157 lines (133 loc) · 4.16 KB
/
serve.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
package main
import (
"context"
"fmt"
"io/ioutil"
"net/url"
"os"
"github.com/ghodss/yaml"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/chef/automate/components/authn-service/server"
"github.com/chef/automate/lib/logger"
"github.com/chef/automate/lib/tracing"
)
func commandServe() *cobra.Command {
return &cobra.Command{
Use: "serve [config file]",
Short: "Begin serving requests.",
Long: ``,
Example: "authn serve config.yaml",
Run: func(cmd *cobra.Command, args []string) {
closer, err := tracing.NewGlobalTracer("authn-service")
if err == nil {
defer tracing.CloseQuietly(closer)
}
if err := serve(cmd, args); err != nil {
/* TODO (tyler) this gives an error:
`Errors unhandled.,LOW,HIGH (gas)`
We should probably fix it since gas is a security linter,
but I don't have enough context to fix it now or know why
it's failing. */
fmt.Fprintln(os.Stderr, err) // #nosec
os.Exit(2)
}
},
}
}
func serve(cmd *cobra.Command, args []string) error {
switch len(args) {
default:
return errors.New("surplus arguments")
case 0:
return errors.New("no arguments provided")
case 1:
}
configFile := args[0]
configData, err := ioutil.ReadFile(configFile)
if err != nil {
return errors.Wrapf(err, "failed to read config file %s", configFile)
}
var c Config
if err := yaml.Unmarshal(configData, &c); err != nil { // nolint: vetshadow
return errors.Wrapf(err, "error parse config file %s", configFile)
}
c.FixupRelativeTLSPaths(configFile)
serviceCerts, err := c.ReadCerts()
if err != nil {
return errors.Wrapf(err, "failed to read tls keys/certs")
}
logger, err := newLogger(c.Logger.Level, c.Logger.Format) // nolint: vetshadow
if err != nil {
return errors.Wrapf(err, "failed to init logger")
}
// use sugared logger here for convenience
l := logger.Sugar()
authenticatorConfigMap := make(map[string]server.AuthenticatorConfig, len(c.Authenticators))
for _, a := range c.Authenticators {
if a.ID == "" || a.Type == "" {
return fmt.Errorf("invalid config: id and type fields are required for an authenticator")
}
if a.Config == nil {
return fmt.Errorf("invalid config: no config field for authenticator %q", a.ID)
}
l.Infof("config authenticator: %s", a.ID)
authenticatorConfigMap[a.ID] = a.Config
}
var upstream *url.URL
if c.Upstream != "" {
upstream, err = url.Parse(c.Upstream)
if err != nil {
return errors.Wrap(err, "could not parse upstream url")
} else if upstream.Scheme == "" || upstream.Host == "" {
return fmt.Errorf("could not parse upstream url '%v'", upstream)
}
}
if c.TeamsAddress == "" {
return fmt.Errorf("missing address for teams service")
}
if c.AuthzAddress == "" {
return fmt.Errorf("missing address for authz service")
}
serverConfig := server.Config{
Upstream: upstream,
Logger: logger,
Authenticators: authenticatorConfigMap,
Token: c.Token.Config,
ServiceCerts: serviceCerts,
TeamsAddress: c.TeamsAddress,
AuthzAddress: c.AuthzAddress,
LegacyDataCollectorToken: c.LegacyDataCollectorToken,
}
serv, err := server.NewServer(context.Background(), serverConfig)
if err != nil {
return errors.Wrap(err, "failed to initialize server")
}
if c.GRPC == "" {
return fmt.Errorf("GRPC endpoint not configured")
}
l.Infof("grpc endpoint listening on %s", c.GRPC)
l.Infof("http1 endpoint listening on %s", c.HTTP1)
err = serv.Serve(c.GRPC, c.HTTP1)
if err != nil {
return errors.Wrapf(err, "grpc endpoint listening on %s failed", c.GRPC)
}
return nil
}
func newLogger(level, format string) (*zap.Logger, error) {
cfg := zap.NewProductionConfig()
// log timestamps in ISO8601
cfg.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
cfg.Level.SetLevel(logger.ParseZapLevel(level))
cfg.Encoding = logger.ParseZapEncoding(format)
logger, err := cfg.Build()
if err != nil {
return nil, errors.Wrapf(err, "can't initialize zap logger")
}
defer logger.Sync() // nolint: errcheck
// redirect stdlib's log to zap
zap.RedirectStdLog(logger)
return logger, nil
}