-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
247 lines (210 loc) · 7.92 KB
/
main.go
File metadata and controls
247 lines (210 loc) · 7.92 KB
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
package main
import (
"compareapp/handlers"
"context"
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
"github.com/coreos/go-oidc"
"golang.org/x/oauth2"
)
var (
gitlabServer string
clientID string
clientSecret string
redirectURL string
gitlabTokenTime int
gitAllowedGroup string
store = sessions.NewCookieStore([]byte("90a541ecfa8ec6629c9")) // Replace with your secret
)
type Config struct {
AppPort int `json:"server_port"`
GitLabAuth bool `json:"gitlab_auth"`
GitlabServer string `json:"gitlab_server"`
GitlabSkipTls bool `json:"gitlab_skip_tls_verify"`
GitlabClientId string `json:"client_id"`
GitlabClientSecret string `json:"client_secret"`
GitlabCallBackUrl string `json:"callback_url"`
GitlabTokenLife int `json:"max_age_session_token"`
GitlabAllowedGroup string `json:"auth_group_name_allowed"`
}
func checkAuthentication(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
//callback := "auth/callback"
//url := r.URL.Path
session, _ := store.Get(r, "compare-app") // Replace with your session name
token := session.Values["token"]
if r.URL.Path != "/auth/login" && r.URL.Path != "/auth/callback" && (token == nil || token == "") {
http.Redirect(w, r, "/auth/login", http.StatusSeeOther)
return
}
next.ServeHTTP(w, r)
})
}
func isGroupAllowed(groups []string) bool {
allowedGroups := []string{gitAllowedGroup}
for _, group := range groups {
for _, allowedGroup := range allowedGroups {
if group == allowedGroup {
return true
}
}
}
return false
}
func main() {
// Read the configuration file
configApp, err := os.ReadFile("./conf/config.json")
if err != nil {
panic(err)
}
// Unmarshal the configuration data
var config Config
if err := json.Unmarshal(configApp, &config); err != nil {
panic(err)
}
gitlabServer = config.GitlabServer
clientID = config.GitlabClientId
clientSecret = config.GitlabClientSecret
redirectURL = config.GitlabCallBackUrl
gitlabTokenTime = config.GitlabTokenLife
gitAllowedGroup = config.GitlabAllowedGroup
gitlabAuth := config.GitLabAuth
if gitlabAuth == true {
// Create a custom HTTP client to ignore SSL verification
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: config.GitlabSkipTls},
}
httpClient := &http.Client{Transport: tr}
ctx := context.Background()
provider, err := oidc.NewProvider(oidc.ClientContext(ctx, httpClient), gitlabServer)
if err != nil {
fmt.Printf("failed to get provider: %v\n", err)
return
}
conf := &oauth2.Config{
ClientID: clientID,
ClientSecret: clientSecret,
RedirectURL: redirectURL,
Endpoint: provider.Endpoint(),
Scopes: []string{oidc.ScopeOpenID, "profile", "email"},
}
r := mux.NewRouter()
r.Use(checkAuthentication)
r.HandleFunc("/auth/login", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, conf.AuthCodeURL("state"), http.StatusFound)
})
r.HandleFunc("/auth/callback", func(w http.ResponseWriter, r *http.Request) {
var ctx = context.Background()
if r.URL.Query().Get("state") != "state" {
http.Error(w, "state did not match", http.StatusBadRequest)
return
}
oauth2Token, err := conf.Exchange(oidc.ClientContext(ctx, httpClient), r.URL.Query().Get("code"))
if err != nil {
http.Error(w, "Failed to exchange token: "+err.Error(), http.StatusInternalServerError)
return
}
rawIDToken, ok := oauth2Token.Extra("id_token").(string)
if !ok {
http.Error(w, "No id_token field in oauth2 token.", http.StatusInternalServerError)
return
}
idToken, err := provider.Verifier(&oidc.Config{ClientID: conf.ClientID}).Verify(ctx, rawIDToken)
if err != nil {
http.Error(w, "Failed to verify ID Token: "+err.Error(), http.StatusInternalServerError)
return
}
var profile map[string]interface{}
if err := idToken.Claims(&profile); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
//fmt.Printf("Claims: %+v\n", profile)
// check that user group is allowed to access
groupsDirect, ok := profile["groups_direct"].([]interface{})
if !ok {
http.Error(w, "Groups not found", http.StatusForbidden)
return
}
// convert []interface{} to []string
var groups []string
for _, group := range groupsDirect {
groupStr, ok := group.(string)
if ok {
groups = append(groups, groupStr)
}
}
if !isGroupAllowed(groups) {
http.Error(w, "Access denied, your group in gitlab not allowed access for this tools, sorry", http.StatusForbidden)
fmt.Print(groups)
return
}
// Set the "token" in the session
session, _ := store.Get(r, "compare-app") // Replace with your session name
store.Options = &sessions.Options{
Path: "/", // cookie path
MaxAge: gitlabTokenTime * 60, // 15 minutes in seconds for session expiration
HttpOnly: true, // HttpOnly to prevent XSS attacks
}
session.Values["token"] = rawIDToken
session.Save(r, w)
http.Redirect(w, r, "/", http.StatusSeeOther)
})
r.HandleFunc("/auth/logout", func(w http.ResponseWriter, r *http.Request) {
session, err := store.Get(r, "compare-app") // Replace with your session name
if err != nil {
http.Error(w, "Failed to retrieve session", http.StatusInternalServerError)
return
}
session.Values["token"] = nil // delete session token, when logout action
session.Save(r, w)
http.Redirect(w, r, "/", http.StatusSeeOther) // redirect to login page
})
r.HandleFunc("/", handlers.IndexHandler)
r.HandleFunc("/select_config", handlers.SelectConfigHandler)
r.HandleFunc("/clear/selected/cluster/config/connections", handlers.ClearSelectHandler)
r.HandleFunc("/namespaces", handlers.NamespaceHandler)
r.HandleFunc("/resources", handlers.ResourceHandler)
r.HandleFunc("/compare_cluster", handlers.CompareClusterHandler)
r.HandleFunc("/compare_cluster/canary_json", handlers.DisplayCanaryJSONHandler)
r.HandleFunc("/compare_cluster/mettempl", handlers.CompareClusterCMTHandler)
r.HandleFunc("/compare_cluster/deploy_json", handlers.DisplayDeployJSONHandler)
r.HandleFunc("/compare_cluster/dmnset_json", handlers.DisplayDmnSetJSONHandler)
r.HandleFunc("/compare_cluster/services_json", handlers.DisplaySvsJSONHandler)
r.HandleFunc("/compare_cluster/tingress_json", handlers.DisplayTingJSONHandler)
r.HandleFunc("/compare_cluster/helmvalues_json", handlers.DisplayHelmJSONHandler)
fmt.Println("Listening on port", config.AppPort)
port := ":" + strconv.Itoa(config.AppPort)
err = http.ListenAndServe(port, r)
if err != nil {
fmt.Println("Cannot open port", config.AppPort, err)
}
} else {
r := mux.NewRouter()
r.HandleFunc("/", handlers.IndexHandler)
r.HandleFunc("/select_config", handlers.SelectConfigHandler)
r.HandleFunc("/clear/selected/cluster/config/connections", handlers.ClearSelectHandler)
r.HandleFunc("/namespaces", handlers.NamespaceHandler)
r.HandleFunc("/resources", handlers.ResourceHandler)
r.HandleFunc("/compare_cluster", handlers.CompareClusterHandler)
r.HandleFunc("/compare_cluster/canary_json", handlers.DisplayCanaryJSONHandler)
r.HandleFunc("/compare_cluster/mettempl", handlers.CompareClusterCMTHandler)
r.HandleFunc("/compare_cluster/deploy_json", handlers.DisplayDeployJSONHandler)
r.HandleFunc("/compare_cluster/dmnset_json", handlers.DisplayDmnSetJSONHandler)
r.HandleFunc("/compare_cluster/services_json", handlers.DisplaySvsJSONHandler)
r.HandleFunc("/compare_cluster/tingress_json", handlers.DisplayTingJSONHandler)
r.HandleFunc("/compare_cluster/helmvalues_json", handlers.DisplayHelmJSONHandler)
fmt.Println("Listening on port", config.AppPort)
port := ":" + strconv.Itoa(config.AppPort)
err = http.ListenAndServe(port, r)
if err != nil {
fmt.Println("Cannot open port", config.AppPort, err)
}
}
}