Skip to content

Commit b283b4c

Browse files
authored
Merge pull request #1 from alexzyWu/master
Add SA User & Fix LDAP BUG
2 parents ba65a0c + 3c079ef commit b283b4c

File tree

113 files changed

+4641
-512
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

113 files changed

+4641
-512
lines changed

cc/go.sum

Lines changed: 0 additions & 157 deletions
Large diffs are not rendered by default.

cc/pkg/controller/loginController.go

Lines changed: 80 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,18 @@
1616
package controller
1717

1818
import (
19+
"crypto/rand"
20+
"crypto/rsa"
21+
"crypto/x509"
1922
"encoding/base64"
2023
"encoding/json"
24+
"encoding/pem"
2125
"errors"
26+
"fmt"
2227
"github.com/go-ldap/ldap/v3"
2328
"github.com/go-openapi/runtime"
2429
"github.com/go-openapi/runtime/middleware"
30+
"github.com/spf13/viper"
2531
"mlss-controlcenter-go/pkg/common"
2632
"mlss-controlcenter-go/pkg/logger"
2733
"mlss-controlcenter-go/pkg/models"
@@ -32,6 +38,11 @@ import (
3238
"strings"
3339
)
3440

41+
const (
42+
ldapPubKey = "ldapPubKey"
43+
ldapPrivKey = "ldapPrivKey"
44+
)
45+
3546
func UMLogin(params logins.UMLoginParams) middleware.Responder {
3647
username := params.Username
3748
password := params.Password
@@ -93,13 +104,34 @@ func UMLogin(params logins.UMLoginParams) middleware.Responder {
93104

94105
func LDAPLogin(params logins.LDAPLoginParams) middleware.Responder {
95106
username := params.LoginRequest.Username
96-
password := params.LoginRequest.Password
107+
decodeBytes, err := base64.StdEncoding.DecodeString(params.LoginRequest.Password)
108+
if err != nil {
109+
logger.Logger().Error("failed to login, base64 decode failed:%v", err.Error())
110+
return ResponderFunc(http.StatusInternalServerError, "failed to login, base64 decode failed:", err.Error())
111+
}
112+
decryptPassword, err := RsaDecrypt(decodeBytes)
113+
if err != nil {
114+
logger.Logger().Error("failed to login, rsa decrypt failed:%v", err.Error())
115+
return ResponderFunc(http.StatusInternalServerError, "failed to login, rsa decrypt failed:", err.Error())
116+
}
97117

98-
isAccess, err := LDAPAuth(username, password)
99-
if err != nil || isAccess == false {
118+
isAccess := false
119+
if username == common.GetAppConfig().Application.Admin.User {
120+
if string(decryptPassword) == common.GetAppConfig().Application.Admin.Password {
121+
isAccess = true
122+
}
123+
} else {
124+
isAccess, err = LDAPAuth(username, string(decryptPassword))
125+
if err != nil {
126+
logger.Logger().Error("Failed to login, LDAP Auth Error:", err.Error())
127+
return ResponderFunc(http.StatusBadRequest, "Failed to login, LDAP auth failed:", err.Error())
128+
}
129+
}
130+
if isAccess == false {
100131
return ResponderFunc(http.StatusBadRequest, "failed to login", "failed to login")
101132
}
102133

134+
// Check system permission
103135
p, err := service.CheckUserPermission(username)
104136
if err != nil {
105137
return ResponderFunc(http.StatusInternalServerError, "failed to login", err.Error())
@@ -108,21 +140,20 @@ func LDAPLogin(params logins.LDAPLoginParams) middleware.Responder {
108140
return ResponderFunc(http.StatusUnauthorized, "failed to login, ", "User does not have system permissions")
109141
}
110142

143+
//Set Session User for Return
144+
isSA := service.GetSAByName(username).Name == username
111145
sessionUser := models.SessionUser{
112146
UserName: username,
113-
IsSuperadmin: service.GetSAByName(username).Name == username,
147+
IsSuperadmin: isSA,
114148
}
115-
116149
logger.Logger().Debugf("sessionUser: %v", sessionUser)
117-
118150
marshal, _ := json.Marshal(sessionUser)
119151
var result = models.Result{
120152
Code: "200",
121153
Message: "success",
122154
Result: json.RawMessage(marshal),
123155
}
124156

125-
//authcache.TokenCache.Set(token, sessionUser, cache.DefaultExpiration)
126157
return middleware.ResponderFunc(func(w http.ResponseWriter, _ runtime.Producer) {
127158
cookie := service.LDAPLogin(w, common.GetAppConfig().Core.Cookie.Path, sessionUser)
128159
http.SetCookie(w, &cookie)
@@ -132,29 +163,60 @@ func LDAPLogin(params logins.LDAPLoginParams) middleware.Responder {
132163
}
133164

134165
func LDAPAuth(username string, password string) (bool, error) {
135-
l, err := ldap.DialURL(common.GetAppConfig().Application.LDAP)
166+
address := common.GetAppConfig().Application.LDAP.Address
167+
baseDN := common.GetAppConfig().Application.LDAP.BaseDN
168+
169+
//Dial LDAP Server
170+
l, err := ldap.DialURL(address)
136171
if err != nil {
137-
logger.Logger().Errorf("ldap server dial error" + err.Error())
172+
logger.Logger().Errorf("LDAP Dial Fail:%v",err.Error())
138173
return false, err
139174
}
140175
if l == nil {
141-
logger.Logger().Errorf("ldap server dial error, connection is nil")
142176
return false, errors.New("ldap server dial error,connection is nil")
143177
}
144178

145-
passwordDecode, err := base64.StdEncoding.DecodeString(password)
179+
//Search User in LDAP Server
180+
nsr := ldap.NewSearchRequest(baseDN, ldap.ScopeBaseObject, ldap.NeverDerefAliases,
181+
0, 0, false,
182+
fmt.Sprintf("(&(objectClass=organizationalPerson)(uid=%s))", username), []string{"dn"}, nil)
183+
sr, err := l.Search(nsr)
146184
if err != nil {
147-
logger.Logger().Errorf("Password decode Error" + err.Error())
185+
logger.Logger().Errorf("LDAP Search Fail:%v",err.Error())
186+
return false, err
148187
}
149188

150-
_, err = l.SimpleBind(&ldap.SimpleBindRequest{
151-
Username: username,
152-
Password: string(passwordDecode),
153-
})
189+
//Auth User Password
190+
userDN := sr.Entries[0].DN
191+
err = l.Bind(userDN, password)
154192
if err != nil {
155-
logger.Logger().Errorf("LDAP Server Auth Error: %s\n", err)
156193
return false, err
157194
}
158195
defer l.Close()
159-
return true, err
196+
return true, nil
197+
}
198+
199+
func GetRsaPubKey(params logins.GetRsaPubKeyParams) middleware.Responder {
200+
return middleware.ResponderFunc(func(w http.ResponseWriter, _ runtime.Producer) {
201+
var result = models.Result{
202+
Code: "200",
203+
Message: "success",
204+
Result: viper.GetString(ldapPubKey),
205+
}
206+
payload, _ := json.Marshal(result)
207+
w.Write(payload)
208+
})
209+
}
210+
211+
func RsaDecrypt(context []byte) ([]byte, error) {
212+
privateKey := viper.GetString(ldapPrivKey)
213+
block, _ := pem.Decode([]byte(privateKey))
214+
if block == nil {
215+
return nil, errors.New("private key error")
216+
}
217+
priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
218+
if err != nil {
219+
return nil, err
220+
}
221+
return rsa.DecryptPKCS1v15(rand.Reader, priv, context)
160222
}

cc/pkg/middleware/authInterceptor.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/gin-gonic/gin"
2020
"mlss-controlcenter-go/pkg/common"
2121
"mlss-controlcenter-go/pkg/constants"
22+
"mlss-controlcenter-go/pkg/logger"
2223
)
2324

2425
func AuthInterceptor() gin.HandlerFunc {

cc/pkg/models/appConfig.go

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,15 @@ type Application struct {
4242
Port string `yaml:"port"`
4343
Db string `yaml:"db"`
4444
}
45-
LDAP string `yaml:"ldap"`
45+
Admin struct{
46+
User string `yaml:"user"`
47+
Password string `yaml:"password"`
48+
49+
}
50+
LDAP struct{
51+
Address string `yaml:"server"`
52+
BaseDN string `yaml:"baseDN"`
53+
}
4654
}
4755

4856
type Server struct {
@@ -60,26 +68,6 @@ type Interceptor struct {
6068
DefaultTimestampTimeout string `yaml:"defaultTimestampTimeout"`
6169
}
6270

63-
// Yaml2 struct of yaml
64-
//type Yaml2 struct {
65-
// Mysql `yaml:"mysql,inline"`
66-
// authcache `yaml:"authcache,inline"`
67-
//}
68-
69-
// Mysql struct of mysql conf
70-
//type Mysql struct {
71-
// User string `yaml:"user"`
72-
// Host string `yaml:"host"`
73-
// Password string `yaml:"password"`
74-
// Port string `yaml:"port"`
75-
// Name string `yaml:"name"`
76-
//}
77-
78-
// authcache struct of authcache conf
79-
//type authcache struct {
80-
// Enable bool `yaml:"enable"`
81-
// List []string `yaml:"list,flow"`
82-
//}
8371
type InterceptorConfig struct {
8472
Name string `yaml:"name"`
8573
Add []string `yaml:"add,flow"`
@@ -127,12 +115,6 @@ type NamespacedResourceConfig struct {
127115
DefaultRQGpu string `yaml:"defaultRQGpu"`
128116
}
129117

130-
//type Gateway struct {
131-
// BdpAddress string `yaml:"bdpAddress"`
132-
// BdapAddress string `yaml:"bdapAddress"`
133-
// BdapsafeAddress string `yaml:"bdapsafeAddress"`
134-
//}
135-
136118
type AuthAddress struct {
137119
User string `yaml:"user"`
138120
Auth string `yaml:"auth"`

cc/pkg/repo/hdfs_privs_repo.go

Lines changed: 0 additions & 57 deletions
This file was deleted.

cc/pkg/restapi/restapi/configure_mlss_cc.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,9 @@ func configureAPI(api *operations.MlssCcAPI) http.Handler {
291291
api.LoginsLDAPLoginHandler = logins.LDAPLoginHandlerFunc(func(params logins.LDAPLoginParams) middleware.Responder {
292292
return controller.LDAPLogin(params)
293293
})
294-
294+
api.LoginsGetRsaPubKeyHandler = logins.GetRsaPubKeyHandlerFunc(func(params logins.GetRsaPubKeyParams) middleware.Responder {
295+
return controller.GetRsaPubKey(params)
296+
})
295297
api.ServerShutdown = func() {}
296298

297299
return setupGlobalMiddleware(api.Serve(setupMiddlewares))

cc/pkg/restapi/restapi/doc.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cc/pkg/restapi/restapi/embedded_spec.go

Lines changed: 54 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)