This repository has been archived by the owner on Dec 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
authz_proxyd.go
166 lines (137 loc) · 5.04 KB
/
authz_proxyd.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
/*
Copyright (C) 2018 Yahoo Japan Corporation Athenz team.
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 usecase
import (
"context"
"github.com/kpango/glg"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
"github.com/yahoojapan/authorization-proxy/config"
"github.com/yahoojapan/authorization-proxy/handler"
"github.com/yahoojapan/authorization-proxy/infra"
"github.com/yahoojapan/authorization-proxy/router"
"github.com/yahoojapan/authorization-proxy/service"
authorizerd "github.com/yahoojapan/athenz-authorizer/v2"
)
// AuthzProxyDaemon represents Authorization Proxy daemon behavior.
type AuthzProxyDaemon interface {
Init(ctx context.Context) error
Start(ctx context.Context) <-chan []error
}
type authzProxyDaemon struct {
cfg config.Config
athenz service.Authorizationd
server service.Server
}
// New returns a Authorization Proxy daemon, or error occurred.
// The daemon contains a token service authentication and authorization server.
// This function will also initialize the mapping rules for the authentication and authorization check.
func New(cfg config.Config) (AuthzProxyDaemon, error) {
athenz, err := newAuthzD(cfg)
if err != nil {
return nil, errors.Wrap(err, "cannot newAuthzD(cfg)")
}
debugMux := router.NewDebugRouter(cfg.Server, athenz)
return &authzProxyDaemon{
cfg: cfg,
athenz: athenz,
server: service.NewServer(
service.WithServerConfig(cfg.Server),
service.WithServerHandler(handler.New(cfg.Proxy, infra.NewBuffer(cfg.Proxy.BufferSize), athenz)),
service.WithDebugHandler(debugMux)),
}, nil
}
// Init initializes child daemons synchronously.
func (g *authzProxyDaemon) Init(ctx context.Context) error {
return g.athenz.Init(ctx)
}
// Start returns a channel of error slice . This error channel reports the errors inside the Authorizer daemon and the Authorization Proxy server.
func (g *authzProxyDaemon) Start(ctx context.Context) <-chan []error {
ech := make(chan []error)
var emap map[string]uint64 // used for returning value from child goroutine, should not r/w in this goroutine
var eg *errgroup.Group
eg, ctx = errgroup.WithContext(ctx)
// handle authorizer daemon error, return on channel close
eg.Go(func() error {
// closure, only this goroutine should write on the variable and the map
emap = make(map[string]uint64, 1)
pch := g.athenz.Start(ctx)
for err := range pch {
if err != nil {
glg.Errorf("pch: %v", err)
// count errors by cause
cause := errors.Cause(err).Error()
_, ok := emap[cause]
if !ok {
emap[cause] = 1
} else {
emap[cause]++
}
}
}
return nil
})
// handle proxy server error, return on server shutdown done
eg.Go(func() error {
errs := <-g.server.ListenAndServe(ctx)
glg.Errorf("sch: %v", errs)
if len(errs) == 0 {
// cannot be nil so that the context can cancel
return errors.New("")
}
var baseErr error
for i, err := range errs {
if i == 0 {
baseErr = err
} else {
baseErr = errors.Wrap(baseErr, err.Error())
}
}
return baseErr
})
// wait for shutdown, and summarize errors
go func() {
defer close(ech)
<-ctx.Done()
err := eg.Wait()
/*
Read on emap is safe here, if and only if:
1. emap is not used in the parenet goroutine
2. the writer goroutine returns only if all erros are written, i.e. pch is closed
3. this goroutine should wait for the writer goroutine to end, i.e. eg.Wait()
*/
// aggregate all errors as array
perrs := make([]error, 0, len(emap))
for errMsg, count := range emap {
perrs = append(perrs, errors.WithMessagef(errors.New(errMsg), "authorizerd: %d times appeared", count))
}
// proxy server go func, should always return not nil error
ech <- append(perrs, err)
}()
return ech
}
func newAuthzD(cfg config.Config) (service.Authorizationd, error) {
return authorizerd.New(
authorizerd.WithAthenzURL(cfg.Athenz.URL),
authorizerd.WithPubkeyRefreshDuration(cfg.Authorization.PubKeyRefreshDuration),
authorizerd.WithPubkeySysAuthDomain(cfg.Authorization.PubKeySysAuthDomain),
authorizerd.WithPubkeyEtagExpTime(cfg.Authorization.PubKeyEtagExpTime),
authorizerd.WithPubkeyEtagFlushDuration(cfg.Authorization.PubKeyEtagFlushDur),
authorizerd.WithPubkeyErrRetryInterval(cfg.Authorization.PubKeyErrRetryInterval),
authorizerd.WithAthenzDomains(cfg.Authorization.AthenzDomains...),
authorizerd.WithPolicyExpireMargin(cfg.Authorization.PolicyExpireMargin),
authorizerd.WithPolicyRefreshDuration(cfg.Authorization.PolicyRefreshDuration),
authorizerd.WithPolicyErrRetryInterval(cfg.Authorization.PolicyErrRetryInterval),
authorizerd.WithDisableJwkd(),
)
}