-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
301 lines (254 loc) · 8.85 KB
/
main.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package main
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"time"
"github.com/go-redis/redis/v7"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
ctrl "sigs.k8s.io/controller-runtime"
)
func main() {
config := ctrl.GetConfigOrDie()
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
log.Fatalf("Failed to create Kubernetes client: %v", err)
}
userToObjectPatternMapping, groupToObjectPatternMapping := loadRBACPolicyFromConfigMap(clientset, "argocd", "argocd-rbac-cm")
redisClient := initializeRedis("localhost:16379", "", 1)
proxy := createReverseProxy("http://localhost:8443")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
handleRequest(w, r, proxy, redisClient, userToObjectPatternMapping, groupToObjectPatternMapping)
})
log.Println("Proxy server running on :8081")
log.Fatal(http.ListenAndServe(":8081", nil))
}
func loadRBACPolicyFromConfigMap(clientset *kubernetes.Clientset, namespace, configMapName string) (map[string][]string, map[string][]string) {
cm, err := clientset.CoreV1().ConfigMaps(namespace).Get(context.Background(), configMapName, metav1.GetOptions{})
if err != nil {
fmt.Printf("Failed to fetch ConfigMap %s: %v", configMapName, err)
return nil, nil
}
policyCSV, ok := cm.Data["policy.csv"]
if !ok {
fmt.Printf("policy.csv not found in ConfigMap %s", configMapName)
return nil, nil
}
return parsePolicyCSV(policyCSV)
}
func initializeRedis(addr, password string, db int) *redis.Client {
client := redis.NewClient(&redis.Options{
Addr: addr,
Password: password,
DB: db,
DialTimeout: 5 * time.Second,
})
if _, err := client.Ping().Result(); err != nil {
log.Fatalf("Failed to connect to Redis: %v", err)
}
fmt.Println("Connected to Redis successfully")
return client
}
func createReverseProxy(target string) *httputil.ReverseProxy {
parsedURL, err := url.Parse(target)
if err != nil {
log.Fatalf("Invalid ArgoCD server URL: %v", err)
}
return &httputil.ReverseProxy{
Director: func(req *http.Request) {
req.URL.Scheme = parsedURL.Scheme
req.URL.Host = parsedURL.Host
req.Host = parsedURL.Host
},
}
}
func handleRequest(w http.ResponseWriter, r *http.Request, proxy *httputil.ReverseProxy, redisClient *redis.Client, userToObjectPatternMapping, groupToObjectPatternMapping map[string][]string) {
token := extractToken(r)
if token == "" || (r.Method != http.MethodGet || !strings.HasPrefix(r.URL.Path, "/api/v1/applications")) {
proxy.ServeHTTP(w, r)
return
}
payload, err := decodeJWTPayload(token)
if err != nil {
proxy.ServeHTTP(w, r)
return
}
email, _ := payload["email"].(string)
groups, _ := payload["groups"].([]string)
objectPatterns := resolveObjectPatterns(email, groups, userToObjectPatternMapping, groupToObjectPatternMapping)
resp := fetchApplicationsFromRedis(redisClient, objectPatterns)
if len(resp.Items) == 0 {
proxy.ServeHTTP(w, r)
return
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(resp); err != nil {
log.Printf("Failed to write response: %v", err)
proxy.ServeHTTP(w, r)
}
}
func extractToken(r *http.Request) string {
if authHeader := r.Header.Get("Authorization"); strings.HasPrefix(authHeader, "Bearer ") {
return strings.TrimPrefix(authHeader, "Bearer ")
}
if cookie, err := r.Cookie("argocd.token"); err == nil {
return cookie.Value
}
return ""
}
func decodeJWTPayload(token string) (map[string]interface{}, error) {
parts := strings.Split(token, ".")
if len(parts) < 2 {
return nil, fmt.Errorf("invalid token format")
}
payloadBytes, err := base64.RawURLEncoding.DecodeString(parts[1])
if err != nil {
return nil, fmt.Errorf("failed to decode payload: %v", err)
}
var payload map[string]interface{}
if err := json.Unmarshal(payloadBytes, &payload); err != nil {
return nil, fmt.Errorf("failed to unmarshal payload: %v", err)
}
return payload, nil
}
func resolveObjectPatterns(email string, groups []string, userToObjectPatternMapping, groupToObjectPatternMapping map[string][]string) map[string]struct{} {
objectPatterns := make(map[string]struct{})
for _, pattern := range userToObjectPatternMapping[email] {
objectPatterns[pattern] = struct{}{}
}
for _, group := range groups {
for _, pattern := range groupToObjectPatternMapping[group] {
objectPatterns[pattern] = struct{}{}
}
}
return objectPatterns
}
func fetchApplicationsFromRedis(redisClient *redis.Client, objectPatterns map[string]struct{}) struct {
Items []interface{} `json:"items"`
} {
resp := struct {
Items []interface{} `json:"items"`
}{Items: []interface{}{}}
var allKeys []string
for pattern := range objectPatterns {
keys, err := redisClient.Keys(fmt.Sprintf("%s|*", pattern)).Result()
if err != nil {
log.Printf("Failed to fetch keys for pattern %s: %v", pattern, err)
continue
}
allKeys = append(allKeys, keys...)
}
if len(allKeys) > 0 {
pipe := redisClient.Pipeline()
cmds := make([]*redis.StringCmd, len(allKeys))
for i, key := range allKeys {
cmds[i] = pipe.Get(key)
}
_, err := pipe.Exec()
if err != nil && err != redis.Nil {
log.Printf("Failed to fetch values for keys: %v", err)
}
for i, cmd := range cmds {
if cmd.Err() == nil {
var rawJson interface{}
if err := json.Unmarshal([]byte(cmd.Val()), &rawJson); err == nil {
resp.Items = append(resp.Items, rawJson)
} else {
log.Printf("Failed to unmarshal value for key %s: %v", allKeys[i], err)
}
} else {
log.Printf("Failed to fetch value for key %s: %v", allKeys[i], cmd.Err())
}
}
}
return resp
}
func parsePolicyCSV(policyCSV string) (map[string][]string, map[string][]string) {
userToRoleMapping := make(map[string][]string)
groupToRoleMapping := make(map[string][]string)
roleToObjectPatternMapping := make(map[string][]string)
// default rules
// - role:admin: unrestricted access to all objects
// - role:readonly: read-only access to all objects
roleToObjectPatternMapping["role:admin"] = []string{"*"}
roleToObjectPatternMapping["role:readonly"] = []string{"*"}
lines := strings.Split(policyCSV, "\n")
for _, line := range lines {
// Ignore empty lines and comments
line = strings.TrimSpace(line)
if line == "" || strings.HasPrefix(line, "#") {
continue
}
// Split the line into fields
fields := strings.Split(line, ",")
for i := range fields {
fields[i] = strings.TrimSpace(fields[i]) // Trim spaces around each field
}
// Process "g" entries (group-role mappings)
if fields[0] == "g" && len(fields) >= 3 {
userOrGroup := fields[1]
role := fields[2]
if strings.Contains(userOrGroup, "@") {
// Process user-role mappings
user := userOrGroup
if _, exists := userToRoleMapping[user]; !exists {
// Initialize the role in the groupToRoleMapping map if it doesn't exist
userToRoleMapping[user] = []string{}
}
userToRoleMapping[user] = append(userToRoleMapping[user], role)
} else {
// Process group-role mappings
group := userOrGroup
if _, exists := groupToRoleMapping[group]; !exists {
// Initialize the role in the groupToRoleMapping map if it doesn't exist
groupToRoleMapping[group] = []string{}
}
groupToRoleMapping[group] = append(groupToRoleMapping[group], role)
}
}
// Process "p" entries (role-resource mappings)
if fields[0] == "p" && len(fields) >= 5 {
role := fields[1]
resource := fields[2]
// action := fields[3]
objectPattern := fields[4]
// effect := field[5]
if resource == "applications" || resource == "applicationsets" || resource == "logs" || resource == "exec " {
objectPattern = strings.TrimSuffix(objectPattern, "/*")
}
if _, exists := roleToObjectPatternMapping[role]; !exists {
// Initialize the role in the roleToObjectPatternMapping map if it doesn't exist
roleToObjectPatternMapping[role] = []string{}
}
roleToObjectPatternMapping[role] = append(roleToObjectPatternMapping[role], objectPattern)
}
}
// Aggregate the user to object pattern mapping
userToObjectPatternMapping := make(map[string][]string)
for user, roles := range userToRoleMapping {
userToObjectPatternMapping[user] = []string{}
for _, role := range roles {
if objectPatterns, exists := roleToObjectPatternMapping[role]; exists {
userToObjectPatternMapping[user] = append(userToObjectPatternMapping[user], objectPatterns...)
}
}
}
// Aggregate the group to object pattern mapping
groupToObjectPatternMapping := make(map[string][]string)
for group, roles := range groupToRoleMapping {
groupToObjectPatternMapping[group] = []string{}
for _, role := range roles {
if objectPatterns, exists := roleToObjectPatternMapping[role]; exists {
groupToObjectPatternMapping[group] = append(groupToObjectPatternMapping[group], objectPatterns...)
}
}
}
return userToObjectPatternMapping, groupToObjectPatternMapping
}