-
Notifications
You must be signed in to change notification settings - Fork 480
/
UserAuthRouter.go
162 lines (148 loc) · 6.41 KB
/
UserAuthRouter.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
/*
* Copyright (c) 2020 Devtron Labs
*
* 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 router
import (
"fmt"
"github.com/argoproj/argo-cd/util/settings"
"github.com/devtron-labs/devtron/api/restHandler"
"github.com/devtron-labs/devtron/client/argocdServer"
"github.com/devtron-labs/devtron/pkg/dex"
"github.com/devtron-labs/devtron/pkg/user"
"github.com/gorilla/mux"
"go.uber.org/zap"
"net"
"net/http"
"time"
)
type UserAuthRouter interface {
initUserAuthRouter(router *mux.Router)
}
type UserAuthRouterImpl struct {
logger *zap.SugaredLogger
userAuthHandler restHandler.UserAuthHandler
cdProxy func(writer http.ResponseWriter, request *http.Request)
dexProxy func(writer http.ResponseWriter, request *http.Request)
}
func NewUserAuthRouterImpl(logger *zap.SugaredLogger, userAuthHandler restHandler.UserAuthHandler, cdCfg *argocdServer.Config, dexCfg *dex.Config, settings *settings.ArgoCDSettings, userService user.UserService) *UserAuthRouterImpl {
tlsConfig := settings.TLSConfig()
if tlsConfig != nil {
tlsConfig.InsecureSkipVerify = true
}
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsConfig,
Proxy: http.ProxyFromEnvironment,
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
}
dexClient := &http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsConfig,
Proxy: http.ProxyFromEnvironment,
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
}
dexProxy := argocdServer.NewDexHTTPReverseProxy(fmt.Sprintf("%s:%s", dexCfg.Host, dexCfg.Port), dexClient.Transport)
cdProxy := argocdServer.NewCDHTTPReverseProxy(fmt.Sprintf("https://%s:%s", cdCfg.Host, cdCfg.Port), client.Transport, userService.GetUserByToken)
router := &UserAuthRouterImpl{
userAuthHandler: userAuthHandler,
cdProxy: cdProxy,
dexProxy: dexProxy,
logger: logger,
}
return router
}
func (router UserAuthRouterImpl) initUserAuthRouter(userAuthRouter *mux.Router) {
userAuthRouter.Path("/").
HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
router.writeSuccess("Welcome @Devtron", writer)
}).Methods("GET")
userAuthRouter.PathPrefix("/api/dex").HandlerFunc(router.dexProxy)
userAuthRouter.Path("/login").HandlerFunc(router.cdProxy)
userAuthRouter.Path("/auth/login").HandlerFunc(router.cdProxy)
userAuthRouter.PathPrefix("/auth/callback").HandlerFunc(router.cdProxy)
userAuthRouter.Path("/api/v1/session").HandlerFunc(router.userAuthHandler.LoginHandler)
//userAuthRouter.Path("/orchestrator/api/dex/auth").HandlerFunc(router.cdProxy)
//userAuthRouter.Path("/orchestrator/api/dex/auth/google").HandlerFunc(router.cdProxy)
//userAuthRouter.Path("/orchestrator/api/dex/approval").HandlerFunc(router.cdProxy)
//userAuthRouter.Path("/orchestrator/api/dex/callback").HandlerFunc(router.cdProxy)
userAuthRouter.Path("/refresh").HandlerFunc(router.userAuthHandler.RefreshTokenHandler)
// Policies Setup
userAuthRouter.Path("/admin/policy").
HandlerFunc(router.userAuthHandler.AddPolicy).Methods("POST")
userAuthRouter.Path("/admin/policy").
HandlerFunc(router.userAuthHandler.RemovePolicy).Methods("DELETE")
userAuthRouter.Path("/admin/policy/subject").
HandlerFunc(router.userAuthHandler.GetAllSubjectsFromCasbin).Methods("GET")
userAuthRouter.Path("/admin/policy/roles").
Queries("user", "{user}").
HandlerFunc(router.userAuthHandler.GetRolesForUserFromCasbin).Methods("GET")
userAuthRouter.Path("/admin/policy/users").
Queries("role", "{role}").
HandlerFunc(router.userAuthHandler.GetUserByRoleFromCasbin).Methods("GET")
userAuthRouter.Path("/admin/policy/subject").
Queries("user", "{user}").
Queries("role", "{role}").
HandlerFunc(router.userAuthHandler.DeleteRoleForUserFromCasbin).Methods("DELETE")
// Policies mapping in orchestrator
userAuthRouter.Path("/admin/role").
HandlerFunc(router.userAuthHandler.CreateRoleFromOrchestrator).Methods("POST")
userAuthRouter.Path("/admin/role").
HandlerFunc(router.userAuthHandler.UpdateRoleFromOrchestrator).Methods("PUT")
userAuthRouter.Path("/admin/role/user/{userId}").
HandlerFunc(router.userAuthHandler.GetRolesByUserIdFromOrchestrator).Methods("GET")
userAuthRouter.Path("/admin/role").
HandlerFunc(router.userAuthHandler.GetAllRoleFromOrchestrator).Methods("GET")
userAuthRouter.Path("/admin/role/filter").
Queries("team", "{team}", "app", "{app}", "env", "{env}", "act", "{act}").
HandlerFunc(router.userAuthHandler.GetRoleByFilterFromOrchestrator).Methods("GET")
userAuthRouter.Path("/admin/role").
Queries("role", "{role}").
HandlerFunc(router.userAuthHandler.DeleteRoleFromOrchestrator).Methods("DELETE")
userAuthRouter.Path("/admin/policy/default").
Queries("team", "{team}", "app", "{app}", "env", "{env}").
HandlerFunc(router.userAuthHandler.AddDefaultPolicyAndRoles).Methods("POST")
userAuthRouter.Path("/devtron/auth/verify").
HandlerFunc(router.userAuthHandler.AuthVerification).Methods("GET")
userAuthRouter.Path("/sso/create").
HandlerFunc(router.userAuthHandler.CreateSSOLoginConfig).Methods("POST")
userAuthRouter.Path("/sso/update").
HandlerFunc(router.userAuthHandler.UpdateSSOLoginConfig).Methods("PUT")
userAuthRouter.Path("/sso/list").
HandlerFunc(router.userAuthHandler.GetAllSSOLoginConfig).Methods("GET")
userAuthRouter.Path("/sso/{id}").
HandlerFunc(router.userAuthHandler.GetSSOLoginConfig).Methods("GET")
userAuthRouter.Path("/sso").Methods("GET").
Queries("name", "{name}").HandlerFunc(router.userAuthHandler.GetSSOLoginConfigByName)
}
func (router UserAuthRouterImpl) writeSuccess(message string, w http.ResponseWriter) {
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(message))
if err != nil {
router.logger.Error(err)
}
}