-
Notifications
You must be signed in to change notification settings - Fork 18
/
access_restriction.go
279 lines (222 loc) · 6.64 KB
/
access_restriction.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Gardener contributors
SPDX-License-Identifier: Apache-2.0
*/
package ac
import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"strconv"
"strings"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
)
// AccessRestriction is used to define an access restriction.
type AccessRestriction struct {
// Key is the identifier of an access restriction
Key string `json:"key,omitempty"`
// NotifyIf controls which value the annotation must have for a notification to be sent
NotifyIf bool `json:"notifyIf,omitempty"`
// Msg is the notification text that is sent
Msg string `json:"msg,omitempty"`
// Options is a list of access restriction options
Options []AccessRestrictionOption `json:"options,omitempty"`
}
// AccessRestrictionOption is used to define an access restriction option.
type AccessRestrictionOption struct {
// Key is the identifier of an access restriction option
Key string `json:"key,omitempty"`
// NotifyIf controls which value the annotation must have for a notification to be sent
NotifyIf bool `json:"notifyIf,omitempty"`
// Msg is the notification text that is sent
Msg string `json:"msg,omitempty"`
}
// AccessRestrictionHandler is a function that should display a single AccessRestrictionMessage to the user.
// The typical implementation of this function looks like this:
//
// func(messages AccessRestrictionMessages) { message.Render(os.Stdout) }
type (
AccessRestrictionHandler func(AccessRestrictionMessages) bool
accessRestrictionHandlerContextKey struct{}
)
// WithAccessRestrictionHandler returns a copy of parent context to which the given AccessRestrictionHandler function has been added.
func WithAccessRestrictionHandler(ctx context.Context, fn AccessRestrictionHandler) context.Context {
return context.WithValue(ctx, accessRestrictionHandlerContextKey{}, fn)
}
// AccessRestrictionHandlerFromContext extracts an AccessRestrictionHandler function from the context.
func AccessRestrictionHandlerFromContext(ctx context.Context) AccessRestrictionHandler {
if val := ctx.Value(accessRestrictionHandlerContextKey{}); val != nil {
if fn, ok := val.(AccessRestrictionHandler); ok {
return fn
}
}
return nil
}
// NewAccessRestrictionHandler create an access restriction handler function.
func NewAccessRestrictionHandler(r io.Reader, w io.Writer, askForConfirmation bool) AccessRestrictionHandler {
return func(messages AccessRestrictionMessages) bool {
if len(messages) == 0 {
return true
}
messages.Render(w)
if !askForConfirmation {
return true
}
return messages.Confirm(r, w)
}
}
func max(x, y int) int {
if y > x {
return y
}
return x
}
func (m *AccessRestrictionMessage) messageWidth() int {
width := 0
for _, text := range m.Items {
for _, line := range strings.Split(text, "\n") {
width = max(width, len(line))
}
}
width += 2
for _, line := range strings.Split(m.Header, "\n") {
width = max(width, len(line))
}
return width
}
func (accessRestriction *AccessRestriction) checkAccessRestriction(matchLabels, annotations map[string]string) *AccessRestrictionMessage {
matches := func(m map[string]string, key string, val bool) bool {
if strVal, ok := m[key]; ok {
if boolVal, err := strconv.ParseBool(strVal); err == nil {
return boolVal == val
}
}
return false
}
if !matches(matchLabels, accessRestriction.Key, accessRestriction.NotifyIf) {
return nil
}
message := &AccessRestrictionMessage{
Header: accessRestriction.Msg,
}
for _, option := range accessRestriction.Options {
if matches(annotations, option.Key, option.NotifyIf) {
message.Items = append(message.Items, option.Msg)
}
}
return message
}
// CheckAccessRestrictions returns a list of access restriction messages for a given shoot cluster.
func CheckAccessRestrictions(accessRestrictions []AccessRestriction, shoot *gardencorev1beta1.Shoot) (messages AccessRestrictionMessages) {
seedSelector := shoot.Spec.SeedSelector
if seedSelector == nil || seedSelector.MatchLabels == nil {
return
}
matchLabels := seedSelector.MatchLabels
annotations := shoot.GetAnnotations()
for _, accessRestriction := range accessRestrictions {
if message := accessRestriction.checkAccessRestriction(matchLabels, annotations); message != nil {
messages = append(messages, message)
}
}
return messages
}
// AccessRestrictionMessage collects all messages for an access restriction in order to display them to the user.
type AccessRestrictionMessage struct {
Header string
Items []string
}
// AccessRestrictionMessages is a list of access restriction messages.
type AccessRestrictionMessages []*AccessRestrictionMessage
var _ fmt.Stringer = &AccessRestrictionMessages{}
type pos int
const (
header pos = iota
body
footer
)
func (p pos) start() string {
switch p {
case header:
return "┌─"
case footer:
return "└─"
default:
return "│ "
}
}
func (p pos) end() string {
switch p {
case header:
return "─┐"
case footer:
return "─┘"
default:
return " │"
}
}
func (p pos) paddEnd(text string, width int) string {
switch p {
case header, footer:
return fmt.Sprintf("%s%s", text, strings.Repeat("─", width-len(text)))
default:
return fmt.Sprintf("%-*s", width, text)
}
}
func (p pos) print(text string, width int) string {
var results []string
hasPrefix := strings.HasPrefix(text, "* ")
for i, line := range strings.Split(text, "\n") {
if hasPrefix && i > 0 {
line = " " + line
}
results = append(results, p.start()+p.paddEnd(line, width)+p.end())
}
return strings.Join(results, "\n")
}
func (messages AccessRestrictionMessages) String() string {
b := &bytes.Buffer{}
messages.Render(b)
return b.String()
}
// Render displays the access restriction messages.
func (messages AccessRestrictionMessages) Render(w io.Writer) {
title := " Access Restriction"
if len(messages) > 1 {
title += "s"
}
title += " "
width := len(title)
for _, m := range messages {
mw := m.messageWidth()
if mw > width {
width = mw
}
}
fmt.Fprintln(w, header.print(title, width))
for _, m := range messages {
fmt.Fprintln(w, body.print(m.Header, width))
for _, item := range m.Items {
fmt.Fprintln(w, body.print("* "+item, width))
}
}
fmt.Fprintln(w, footer.print("", width))
}
// Confirm asks for confirmation to continue.
func (messages AccessRestrictionMessages) Confirm(r io.Reader, w io.Writer) bool {
reader := bufio.NewReader(r)
for {
fmt.Fprint(w, "Do you want to continue? [y/N]: ")
str, _ := reader.ReadString('\n')
str = strings.TrimSpace(str)
str = strings.ToLower(str)
switch str {
case "y", "yes":
return true
case "", "n", "no":
return false
}
}
}