/
listchecker.go
236 lines (193 loc) · 6.16 KB
/
listchecker.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
// Copyright 2018 Istio Authors
//
// 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 server
import (
"crypto/x509/pkix"
"errors"
"fmt"
"sort"
"strings"
"sync"
"time"
"golang.org/x/time/rate"
"google.golang.org/grpc/credentials"
"istio.io/istio/security/pkg/pki/util"
)
// AllowAllChecker is a simple auth checker that allows all requests.
type AllowAllChecker struct{}
// NewAllowAllChecker creates a new AllowAllChecker.
func NewAllowAllChecker() *AllowAllChecker { return &AllowAllChecker{} }
// Check is an implementation of AuthChecker.Check that allows all check requests.
func (*AllowAllChecker) Check(credentials.AuthInfo) error { return nil }
// AuthListMode indicates the list checking mode
type AuthListMode bool
const (
// AuthBlackList indicates that the list should work as a black list
AuthBlackList AuthListMode = false
// AuthWhiteList indicates that the list should work as a white list
AuthWhiteList AuthListMode = true
)
// ListAuthChecker implements AuthChecker function and is backed by a set of ids.
type ListAuthChecker struct {
mode AuthListMode
idsMutex sync.RWMutex
ids map[string]struct{}
checkFailureRecordLimiter *rate.Limiter
failureCountSinceLastRecord int
// overridable functions for testing
extractIDsFn func(exts []pkix.Extension) ([]string, error)
}
type ListAuthCheckerOptions struct {
// For the purposes of logging rate limiting authz failures, this controls how
// many authz failures are logged in a burst every AuthzFailureLogFreq.
AuthzFailureLogBurstSize int
// For the purposes of logging rate limiting authz failures, this controls how
// frequently bursts of authz failures are logged.
AuthzFailureLogFreq time.Duration
// AuthMode indicates the list checking mode
AuthMode AuthListMode
}
func DefaultListAuthCheckerOptions() *ListAuthCheckerOptions {
return &ListAuthCheckerOptions{
AuthzFailureLogBurstSize: 1,
AuthzFailureLogFreq: time.Minute,
AuthMode: AuthWhiteList,
}
}
// NewListAuthChecker returns a new instance of ListAuthChecker
func NewListAuthChecker(options *ListAuthCheckerOptions) *ListAuthChecker {
// Initialize record limiter for the auth checker.
limit := rate.Every(options.AuthzFailureLogFreq)
limiter := rate.NewLimiter(limit, options.AuthzFailureLogBurstSize)
return &ListAuthChecker{
mode: options.AuthMode,
ids: make(map[string]struct{}),
extractIDsFn: util.ExtractIDs,
checkFailureRecordLimiter: limiter,
}
}
// Add the provided id to the list of ids.
func (l *ListAuthChecker) Add(id string) {
l.idsMutex.Lock()
defer l.idsMutex.Unlock()
l.ids[id] = struct{}{}
}
// Remove the provided id from the list of ids.
func (l *ListAuthChecker) Remove(id string) {
l.idsMutex.Lock()
defer l.idsMutex.Unlock()
delete(l.ids, id)
}
// Set new sets of ids. Previous ones are removed.
func (l *ListAuthChecker) Set(ids ...string) {
newIds := make(map[string]struct{})
for _, id := range ids {
newIds[id] = struct{}{}
}
l.idsMutex.Lock()
defer l.idsMutex.Unlock()
l.ids = newIds
}
// SetMode sets the list-checking mode for this list.
func (l *ListAuthChecker) SetMode(mode AuthListMode) {
l.idsMutex.Lock()
defer l.idsMutex.Unlock()
l.mode = mode
}
// Allowed checks whether the given id is allowed.
func (l *ListAuthChecker) Allowed(id string) bool {
l.idsMutex.RLock()
defer l.idsMutex.RUnlock()
if l.mode == AuthWhiteList {
return l.contains(id)
}
return !l.contains(id) // AuthBlackList
}
func (l *ListAuthChecker) contains(id string) bool {
_, found := l.ids[id]
return found
}
// String is an implementation of Stringer.String.
func (l *ListAuthChecker) String() string {
l.idsMutex.RLock()
defer l.idsMutex.RUnlock()
var ids []string
for id := range l.ids {
ids = append(ids, id)
}
sort.Strings(ids)
result := ""
switch l.mode {
case AuthWhiteList:
result += "Mode: whitelist\n"
case AuthBlackList:
result += "Mode: blacklist\n"
}
result += "Known ids:\n"
result += strings.Join(ids, "\n")
return result
}
func (l *ListAuthChecker) Check(authInfo credentials.AuthInfo) error {
if err := l.check(authInfo); err != nil {
l.failureCountSinceLastRecord++
if l.checkFailureRecordLimiter.Allow() {
scope.Warnf("NewConnection: auth check failed: %v (repeated %d times).",
err, l.failureCountSinceLastRecord)
l.failureCountSinceLastRecord = 0
}
return err
}
return nil
}
// Check is an implementation of AuthChecker.Check.
func (l *ListAuthChecker) check(authInfo credentials.AuthInfo) error {
l.idsMutex.RLock()
defer l.idsMutex.RUnlock()
// Extract the identities
if authInfo == nil {
// Do not allow access
return errors.New("denying by default: no auth info found")
}
tlsInfo, ok := authInfo.(credentials.TLSInfo)
if !ok {
return errors.New("unable to extract TLS info from the supplied auth info")
}
for i, arr := range tlsInfo.State.VerifiedChains {
for j, cert := range arr {
ids, err := l.extractIDsFn(cert.Extensions)
// The error maybe due to SAN extensions not existing in a particular certificate.
// Simply skip to the next one.
if err != nil {
scope.Debugf("Error during id extraction from certificate (%d,%d): %v", i, j, err)
continue
}
for _, id := range ids {
if l.contains(id) {
switch l.mode {
case AuthWhiteList:
scope.Infof("Allowing access from peer with id: %s", id)
return nil
case AuthBlackList:
scope.Infof("Blocking access from peer with id: %s", id)
return fmt.Errorf("id is blacklisted: %s", id)
}
}
}
}
}
if l.mode == AuthWhiteList {
return errors.New("no allowed identity found in peer's authentication info")
}
return nil // AuthBlackList
}