Do not display internal error message to user

This commit is contained in:
stonezdj 2017-12-27 19:22:17 +08:00
parent 944fd1d97a
commit fba68ed000

View File

@ -15,9 +15,7 @@
package api package api
import ( import (
"fmt"
"net/http" "net/http"
"strings"
"github.com/vmware/harbor/src/common/models" "github.com/vmware/harbor/src/common/models"
ldapUtils "github.com/vmware/harbor/src/common/utils/ldap" ldapUtils "github.com/vmware/harbor/src/common/utils/ldap"
@ -30,7 +28,13 @@ type LdapAPI struct {
BaseController BaseController
} }
const metaChars = "&|!=~*<>()" const (
pingErrorMessage = "LDAP connection test failed!"
loadSystemErrorMessage = "Can't load system configuration!"
canNotOpenLdapSession = "Can't open LDAP session!"
searchLdapFailMessage = "LDAP search failed!"
importUserError = "Found internal error when importing LDAP user!"
)
// Prepare ... // Prepare ...
func (l *LdapAPI) Prepare() { func (l *LdapAPI) Prepare() {
@ -57,7 +61,7 @@ func (l *LdapAPI) Ping() {
ldapSession, err = ldapUtils.LoadSystemLdapConfig() ldapSession, err = ldapUtils.LoadSystemLdapConfig()
if err != nil { if err != nil {
log.Errorf("Can't load system configuration, error: %v", err) log.Errorf("Can't load system configuration, error: %v", err)
l.RenderError(http.StatusInternalServerError, fmt.Sprintf("can't load system configuration: %v", err)) l.RenderError(http.StatusInternalServerError, pingErrorMessage)
return return
} }
err = ldapSession.ConnectionTest() err = ldapSession.ConnectionTest()
@ -68,7 +72,7 @@ func (l *LdapAPI) Ping() {
if err != nil { if err != nil {
log.Errorf("ldap connect fail, error: %v", err) log.Errorf("ldap connect fail, error: %v", err)
l.RenderError(http.StatusBadRequest, fmt.Sprintf("ldap connect fail: %v", err)) l.RenderError(http.StatusBadRequest, pingErrorMessage)
return return
} }
} }
@ -84,7 +88,7 @@ func (l *LdapAPI) Search() {
ldapSession, err = ldapUtils.LoadSystemLdapConfig() ldapSession, err = ldapUtils.LoadSystemLdapConfig()
if err != nil { if err != nil {
log.Errorf("can't load system configuration, error: %v", err) log.Errorf("can't load system configuration, error: %v", err)
l.RenderError(http.StatusInternalServerError, fmt.Sprintf("can't load system configuration: %v", err)) l.RenderError(http.StatusInternalServerError, loadSystemErrorMessage)
return return
} }
} else { } else {
@ -94,28 +98,18 @@ func (l *LdapAPI) Search() {
if err = ldapSession.Open(); err != nil { if err = ldapSession.Open(); err != nil {
log.Errorf("can't Open ldap session, error: %v", err) log.Errorf("can't Open ldap session, error: %v", err)
l.RenderError(http.StatusInternalServerError, fmt.Sprintf("can't open ldap session: %v", err)) l.RenderError(http.StatusInternalServerError, canNotOpenLdapSession)
return return
} }
defer ldapSession.Close() defer ldapSession.Close()
searchName := l.GetString("username") searchName := l.GetString("username")
if searchName != "" {
for _, c := range metaChars {
if strings.ContainsRune(searchName, c) {
log.Errorf("the search username contains meta char: %q", c)
l.RenderError(http.StatusBadRequest, fmt.Sprintf("the search username contains meta char: %q", c))
return
}
}
}
ldapUsers, err = ldapSession.SearchUser(searchName) ldapUsers, err = ldapSession.SearchUser(searchName)
if err != nil { if err != nil {
log.Errorf("Ldap search fail, error: %v", err) log.Errorf("Ldap search fail, error: %v", err)
l.RenderError(http.StatusBadRequest, fmt.Sprintf("ldap search fail: %v", err)) l.RenderError(http.StatusBadRequest, searchLdapFailMessage)
return return
} }
@ -136,13 +130,13 @@ func (l *LdapAPI) ImportUser() {
if err != nil { if err != nil {
log.Errorf("Ldap import user fail, error: %v", err) log.Errorf("Ldap import user fail, error: %v", err)
l.RenderError(http.StatusBadRequest, fmt.Sprintf("ldap import user fail: %v", err)) l.RenderError(http.StatusBadRequest, importUserError)
return return
} }
if len(ldapFailedImportUsers) > 0 { if len(ldapFailedImportUsers) > 0 {
log.Errorf("Import ldap user have internal error") log.Errorf("Import ldap user have internal error")
l.RenderError(http.StatusInternalServerError, fmt.Sprintf("import ldap user have internal error")) l.RenderError(http.StatusInternalServerError, importUserError)
l.Data["json"] = ldapFailedImportUsers l.Data["json"] = ldapFailedImportUsers
l.ServeJSON() l.ServeJSON()
return return
@ -175,13 +169,6 @@ func importUsers(ldapConfs models.LdapConf, ldapImportUsers []string) ([]models.
continue continue
} }
for _, c := range metaChars {
if strings.ContainsRune(u.UID, c) {
u.Error = "invaild_username"
break
}
}
if u.Error != "" { if u.Error != "" {
failedImportUser = append(failedImportUser, u) failedImportUser = append(failedImportUser, u)
continue continue