/
configmap.go
127 lines (109 loc) · 3.58 KB
/
configmap.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
// 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 (
"fmt"
"io/ioutil"
"os"
"time"
"github.com/fsnotify/fsnotify"
yaml "gopkg.in/yaml.v2"
"istio.io/istio/pkg/filewatcher"
"istio.io/istio/pkg/mcp/env"
"istio.io/istio/pkg/mcp/server"
)
type accessList struct {
IsBlackList bool
Allowed []string
}
var (
newFileWatcher = filewatcher.NewWatcher
readFile = ioutil.ReadFile
watchEventHandledProbe func()
)
var (
// For the purposes of logging rate limiting authz failures, this controls how
// many authz failures are logs as a burst every AUTHZ_FAILURE_LOG_FREQ.
authzFailureLogBurstSize = env.Integer("AUTHZ_FAILURE_LOG_BURST_SIZE", 1)
// For the purposes of logging rate limiting authz failures, this controls how
// frequently bursts of authz failures are logged.
authzFailureLogFreq = env.Duration("AUTHZ_FAILURE_LOG_FREQ", time.Minute)
)
func watchAccessList(stopCh <-chan struct{}, accessListFile string) (*server.ListAuthChecker, error) {
// Do the initial read.
list, err := readAccessList(accessListFile)
if err != nil {
return nil, err
}
options := server.DefaultListAuthCheckerOptions()
options.AuthzFailureLogBurstSize = authzFailureLogBurstSize
options.AuthzFailureLogFreq = authzFailureLogFreq
if list.IsBlackList {
options.AuthMode = server.AuthBlackList
} else {
options.AuthMode = server.AuthWhiteList
}
checker := server.NewListAuthChecker(options)
checker.Set(list.Allowed...)
watcher := newFileWatcher()
if err = watcher.Add(accessListFile); err != nil {
return nil, fmt.Errorf("unable to watch accesslist file %q: %v", accessListFile, err)
}
go func() {
for {
select {
case e := <-watcher.Events(accessListFile):
if e.Op&fsnotify.Write == fsnotify.Write || e.Op&fsnotify.Create == fsnotify.Create {
if list, err = readAccessList(accessListFile); err != nil {
scope.Errorf("Error reading access list %q: %v", accessListFile, err)
} else {
if list.IsBlackList {
checker.SetMode(server.AuthBlackList)
} else {
checker.SetMode(server.AuthWhiteList)
}
checker.Set(list.Allowed...)
}
} else if e.Op&fsnotify.Remove == fsnotify.Remove {
checker.SetMode(server.AuthBlackList)
checker.Set()
}
if watchEventHandledProbe != nil {
watchEventHandledProbe()
}
case e := <-watcher.Errors(accessListFile):
scope.Errorf("error event while watching access list file: %v", e)
case <-stopCh:
_ = watcher.Close()
return
}
}
}()
return checker, nil
}
func readAccessList(accessListFile string) (accessList, error) {
b, err := readFile(accessListFile)
if err != nil {
if os.IsNotExist(err) {
// Treat a non-existent access list file as default open
return accessList{IsBlackList: true}, nil
}
return accessList{}, fmt.Errorf("unable to read access list file %q: %v", accessListFile, err)
}
var list accessList
if err = yaml.Unmarshal(b, &list); err != nil {
return accessList{}, fmt.Errorf("unable to parse access list file %q: %v", accessListFile, err)
}
return list, nil
}