/
server.go
258 lines (227 loc) · 7.9 KB
/
server.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package server
import (
"context"
"fmt"
"net"
"net/http"
"net/textproto"
"net/url"
"time"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/pkg/errors"
"go.uber.org/zap"
"google.golang.org/grpc/grpclog"
api "github.com/chef/automate/api/interservice/authn"
"github.com/chef/automate/api/interservice/authz"
"github.com/chef/automate/api/interservice/id_token"
"github.com/chef/automate/api/interservice/teams"
"github.com/chef/automate/components/authn-service/authenticator"
tokens "github.com/chef/automate/components/authn-service/tokens/types"
"github.com/chef/automate/lib/grpc/health"
"github.com/chef/automate/lib/grpc/secureconn"
wrap "github.com/chef/automate/lib/logger"
"github.com/chef/automate/lib/tls/certs"
"github.com/chef/automate/lib/version"
)
const (
IngestPolicyID = "ingest-access"
)
// Config holds the server's configuration options.
type Config struct {
// These are the configs of the specified Authenticators, which will be
// Open()ed when this config is passed to NewServer
Authenticators map[string]AuthenticatorConfig
Upstream *url.URL
Logger *zap.Logger
Token tokens.TokenConfig
ServiceCerts *certs.ServiceCerts
TeamsAddress string // "ip:port"
AuthzAddress string
LegacyDataCollectorToken string
SessionAddress string
}
// Server is the top level object.
type Server struct {
// Map of authenticator IDs to authenticators.
// Note: dex wraps this with a ResourceVersion, but that is due to storing
// connectors in the database -- we don't do this
TokenStorage tokens.Storage
authenticators map[string]authenticator.Authenticator
logger *zap.Logger
connFactory *secureconn.Factory
teamsClient teams.TeamsServiceClient
policiesClient authz.PoliciesServiceClient
authzClient authz.AuthorizationServiceClient
health *health.Service
}
// NewServer constructs a server from the provided config.
func NewServer(ctx context.Context, c Config) (*Server, error) {
return newServer(ctx, c)
}
// Serve tells authn to start responding to GRPC and HTTP1 requests. On success, it never returns.
func (s *Server) Serve(grpcEndpoint, http1Endpoint string) error {
list, err := net.Listen("tcp", grpcEndpoint)
if err != nil {
return err
}
server := s.NewGRPCServer(s.policiesClient, s.authzClient)
opts := []runtime.ServeMuxOption{
runtime.WithIncomingHeaderMatcher(headerMatcher),
}
pbmux := runtime.NewServeMux(opts...)
ctx := context.Background()
err = api.RegisterAuthenticationServiceHandlerFromEndpoint(
ctx, pbmux, grpcEndpoint, s.connFactory.DialOptions("authn-service"))
if err != nil {
return err
}
// Note: we start both servers in a goroutine, and when one of them returns,
// (with what must be an error), we (brutally) abort everything and return
// that error.
errc := make(chan error)
if http1Endpoint != "" {
go func() {
err := s.ServeHTTP1(pbmux, http1Endpoint)
errc <- errors.Wrap(err, "HTTP")
}()
} else {
s.logger.Info("HTTP1 not configured")
}
go func() {
errc <- errors.Wrap(server.Serve(list), "GRPC")
}()
return errors.Wrap(<-errc, "Serve")
}
func (s *Server) ServeHTTP1(pbmux *runtime.ServeMux, http1Endpoint string) error {
httpmux := http.NewServeMux()
httpmux.Handle("/", pbmux)
return http.ListenAndServe(http1Endpoint, httpmux)
}
func newServer(ctx context.Context, c Config) (*Server, error) {
// Users shouldn't see this, but gives you a clearer error message if you
// don't configure things correctly in testing.
if c.ServiceCerts == nil {
return nil, errors.New("config is missing required TLS settings")
}
factory := secureconn.NewFactory(*c.ServiceCerts, secureconn.WithVersionInfo(
version.Version,
version.GitSHA,
))
teamsConn, err := factory.Dial("teams-service", c.TeamsAddress)
if err != nil {
return nil, errors.Wrapf(err, "dial teams-service (%s)", c.TeamsAddress)
}
sessionConn, err := factory.Dial("session-service", c.SessionAddress)
if err != nil {
return nil, errors.Wrapf(err, "dial session-service (%s)", c.SessionAddress)
}
sessionClient := id_token.NewValidateIdTokenServiceClient(sessionConn)
authzConn, err := factory.Dial("authz-service", c.AuthzAddress)
if err != nil {
return nil, errors.Wrapf(err, "dial authz-service (%s)", c.AuthzAddress)
}
authzClient := authz.NewAuthorizationServiceClient(authzConn)
policiesClient := authz.NewPoliciesServiceClient(authzConn)
var ts tokens.Storage
if c.Token != nil {
ts, err = c.Token.Open(c.ServiceCerts, c.Logger, authzClient)
if err != nil {
return nil, errors.Wrap(err, "initialize tokens adapter")
}
} else {
c.Logger.Debug("no tokens adapter defined")
}
authenticators := make(map[string]authenticator.Authenticator)
for authnID, authnCfg := range c.Authenticators {
authn, err := authnCfg.Open(c.Upstream, c.ServiceCerts, c.Logger, sessionClient)
if err != nil {
return nil, errors.Wrapf(err, "initialize authenticator %s", authnID)
}
authenticators[authnID] = authn
}
s := &Server{
TokenStorage: ts,
authzClient: authzClient,
policiesClient: policiesClient,
authenticators: authenticators,
logger: c.Logger,
connFactory: factory,
teamsClient: teams.NewTeamsServiceClient(teamsConn),
health: health.NewService(),
}
// make grpc-go log through zap
grpclog.SetLoggerV2(wrap.WrapZapGRPC(s.logger))
// Add the legacy data collector token as a secret if it was defined in the config.
if c.LegacyDataCollectorToken != "" {
var tokenID string
if existingID, err := ts.GetTokenIDWithValue(ctx, c.LegacyDataCollectorToken); err != nil {
// If we couldn't find the legacy data collector token, create it.
if _, ok := errors.Cause(err).(*tokens.NotFoundError); ok {
var token *tokens.Token
token, err = ts.CreateLegacyTokenWithValue(ctx, c.LegacyDataCollectorToken)
tokenID = token.ID
}
if err != nil {
return nil, errors.Wrap(err,
"could not populate the legacy data collector token")
}
} else {
tokenID = existingID
}
/*
Below code is only execute when you add token in config at the time of deployment
Adding the sleep, is just the tempory solution, this might work
*/
var policyAssigned bool = false
for i := 4; i > 0; i-- {
_, err = policiesClient.AddPolicyMembers(ctx, &authz.AddPolicyMembersReq{
Id: IngestPolicyID,
Members: []string{fmt.Sprintf("token:%s", tokenID)},
})
if err != nil {
policyAssigned = false
} else {
s.logger.Debug("exiting for loop")
policyAssigned = true
break
}
s.logger.Debug(fmt.Sprintf(" Adding the sleep for %d seconds ", i))
time.Sleep(time.Duration(3) * time.Second)
}
if !policyAssigned {
s.logger.Warn(errors.Wrap(err, "there was an error granting the legacy data collector token ingest access").Error())
s.logger.Warn(fmt.Sprintf("please manually add token with ID %q to the policy with ID %q", tokenID, IngestPolicyID))
}
}
return s, nil
}
func (s *Server) fetchLocalTeams(ctx context.Context, userID string) ([]string, error) {
teamsResp, err := s.teamsClient.GetTeamsForMember(ctx, &teams.GetTeamsForMemberReq{UserId: userID})
if err != nil {
return nil, errors.Wrapf(err, "failed to fetch local teams for user %q", userID)
}
teams := make([]string, len(teamsResp.GetTeams()))
for i, team := range teamsResp.GetTeams() {
teams[i] = team.GetId()
}
return teams, nil
}
// headerMatcher extends runtime.DefaultHeaderMatcher by also injecting headers
// that are not-standard, but in use in Automate.
func headerMatcher(key string) (string, bool) {
// default behavior
if key, ok := runtime.DefaultHeaderMatcher(key); ok {
return key, ok
}
if isAutomateHeader(key) {
return runtime.MetadataPrefix + key, true
}
return "", false
}
func isAutomateHeader(hdr string) bool {
switch textproto.CanonicalMIMEHeaderKey(hdr) {
case "X-Data-Collector-Token", "Api-Token", "X-Client-Cert":
return true
}
return false
}