Fix ldap ping issue #3653

This commit is contained in:
stonezdj 2017-12-14 18:54:32 +08:00
parent 0abd30f9f4
commit 9393d26fdc
15 changed files with 84 additions and 66 deletions

View File

@ -17,6 +17,5 @@ import (
"testing"
)
func TestMain(t *testing.T) {
func TestMain(m *testing.M) {
}

View File

@ -190,8 +190,14 @@ func (session *Session) ConnectionTest() error {
//ConnectionTestWithConfig - test ldap session connection, out of the scope of normal session create/close
func ConnectionTestWithConfig(ldapConfig models.LdapConf) error {
authMode, err := config.AuthMode()
if err != nil {
log.Errorf("Connection test failed %v", err)
return err
}
//If no password present, use the system default password
if ldapConfig.LdapSearchPassword == "" {
if ldapConfig.LdapSearchPassword == "" && authMode == "ldap_auth" {
session, err := LoadSystemLdapConfig()

View File

@ -88,31 +88,31 @@ var adminServerDefaultConfigWithVerifyCert = map[string]interface{}{
common.WithClair: false,
}
func TestMain(t *testing.T) {
func TestMain(m *testing.M) {
server, err := test.NewAdminserver(adminServerLdapTestConfig)
if err != nil {
t.Fatalf("failed to create a mock admin server: %v", err)
log.Fatalf("failed to create a mock admin server: %v", err)
}
defer server.Close()
if err := os.Setenv("ADMINSERVER_URL", server.URL); err != nil {
t.Fatalf("failed to set env %s: %v", "ADMINSERVER_URL", err)
log.Fatalf("failed to set env %s: %v", "ADMINSERVER_URL", err)
}
secretKeyPath := "/tmp/secretkey"
_, err = test.GenerateKey(secretKeyPath)
if err != nil {
t.Errorf("failed to generate secret key: %v", err)
log.Errorf("failed to generate secret key: %v", err)
return
}
defer os.Remove(secretKeyPath)
if err := os.Setenv("KEY_PATH", secretKeyPath); err != nil {
t.Fatalf("failed to set env %s: %v", "KEY_PATH", err)
log.Fatalf("failed to set env %s: %v", "KEY_PATH", err)
}
if err := uiConfig.Init(); err != nil {
t.Fatalf("failed to initialize configurations: %v", err)
log.Fatalf("failed to initialize configurations: %v", err)
}
database, err := uiConfig.Database()
@ -123,6 +123,9 @@ func TestMain(t *testing.T) {
if err := dao.InitDatabase(database); err != nil {
log.Fatalf("failed to initialize database: %v", err)
}
os.Exit(m.Run())
}
func TestLoadSystemLdapConfig(t *testing.T) {
@ -173,10 +176,21 @@ func TestCreateUIConfig(t *testing.T) {
LdapScope: 1,
LdapURL: "ldaps://127.0.0.1",
}, 0},
{
models.LdapConf{
LdapScope: 1,
LdapURL: "ldaps://127.0.0.1:abc",
}, -1},
}
for _, val := range testConfigs {
session, err := CreateWithUIConfig(val.config)
if val.internalValue < 0 {
if err == nil {
t.Fatalf("Should have error with url :%v", val.config)
}
continue
}
if err != nil {
t.Fatalf("Can not create with ui config, err:%v", err)
}
@ -212,22 +226,6 @@ func TestSearchUser(t *testing.T) {
}
func InitTest(ldapTestConfig map[string]interface{}, t *testing.T) {
server, err := test.NewAdminserver(ldapTestConfig)
if err != nil {
t.Fatalf("failed to create a mock admin server: %v", err)
}
defer server.Close()
if err := os.Setenv("ADMIN_SERVER_URL", server.URL); err != nil {
t.Fatalf("failed to set env %s:%v", "ADMIN_SERVER_URL", err)
}
if err := uiConfig.Init(); err != nil {
t.Fatalf("failed to initialize configurations: %v ", err)
}
}
func TestFormatURL(t *testing.T) {
var invalidURL = "http://localhost:389"
@ -246,10 +244,17 @@ func TestFormatURL(t *testing.T) {
{"ldaps://127.0.0.1:389", "ldaps://127.0.0.1:389"},
{"ldap://127.0.0.1:636", "ldaps://127.0.0.1:636"},
{"112.122.122.122", "ldap://112.122.122.122:389"},
{"ldap:\\wrong url", ""},
}
for _, u := range urls {
goodURL, err := formatURL(u.rawURL)
if u.goodURL == "" {
if err == nil {
t.Fatalf("Should failed on wrong url, %v", u.rawURL)
}
continue
}
if err != nil || goodURL != u.goodURL {
t.Fatalf("Faild on URL: raw=%v, expected:%v, actual:%v", u.rawURL, u.goodURL, goodURL)
}

View File

@ -17,6 +17,5 @@ import (
"testing"
)
func TestMain(t *testing.T) {
func TestMain(m *testing.M) {
}

View File

@ -17,6 +17,5 @@ import (
"testing"
)
func TestMain(t *testing.T) {
func TestMain(m *testing.M) {
}

View File

@ -17,6 +17,5 @@ import (
"testing"
)
func TestMain(t *testing.T) {
func TestMain(m *testing.M) {
}

View File

@ -48,19 +48,18 @@ func (l *LdapAPI) Prepare() {
// Ping ...
func (l *LdapAPI) Ping() {
var ldapConfs models.LdapConf
var err error
var ldapSession *ldapUtils.Session
l.Ctx.Input.CopyBody(1 << 32)
ldapSession, err := ldapUtils.LoadSystemLdapConfig()
if err != nil {
log.Errorf("Can't load system configuration, error: %v", err)
l.RenderError(http.StatusInternalServerError, fmt.Sprintf("can't load system configuration: %v", err))
return
}
if string(l.Ctx.Input.RequestBody) == "" {
ldapSession, err = ldapUtils.LoadSystemLdapConfig()
if err != nil {
log.Errorf("Can't load system configuration, error: %v", err)
l.RenderError(http.StatusInternalServerError, fmt.Sprintf("can't load system configuration: %v", err))
return
}
err = ldapSession.ConnectionTest()
} else {
l.DecodeJSONReqAndValidate(&ldapConfs)

View File

@ -23,6 +23,7 @@ import (
"github.com/vmware/harbor/src/common/utils/test"
"github.com/vmware/harbor/src/common/models"
"github.com/vmware/harbor/src/common/utils/ldap"
"github.com/vmware/harbor/src/ui/auth"
uiConfig "github.com/vmware/harbor/src/ui/config"
)
@ -64,31 +65,31 @@ var adminServerTestConfig = map[string]interface{}{
common.AdminInitialPassword: "password",
}
func TestMain(t *testing.T) {
func TestMain(m *testing.M) {
server, err := test.NewAdminserver(adminServerTestConfig)
if err != nil {
t.Fatalf("failed to create a mock admin server: %v", err)
log.Fatalf("failed to create a mock admin server: %v", err)
}
defer server.Close()
if err := os.Setenv("ADMINSERVER_URL", server.URL); err != nil {
t.Fatalf("failed to set env %s: %v", "ADMINSERVER_URL", err)
log.Fatalf("failed to set env %s: %v", "ADMINSERVER_URL", err)
}
secretKeyPath := "/tmp/secretkey"
_, err = test.GenerateKey(secretKeyPath)
if err != nil {
t.Errorf("failed to generate secret key: %v", err)
log.Fatalf("failed to generate secret key: %v", err)
return
}
defer os.Remove(secretKeyPath)
if err := os.Setenv("KEY_PATH", secretKeyPath); err != nil {
t.Fatalf("failed to set env %s: %v", "KEY_PATH", err)
log.Fatalf("failed to set env %s: %v", "KEY_PATH", err)
}
if err := uiConfig.Init(); err != nil {
t.Fatalf("failed to initialize configurations: %v", err)
log.Fatalf("failed to initialize configurations: %v", err)
}
database, err := uiConfig.Database()
@ -150,3 +151,22 @@ func TestAuthenticateHelperSearchUser(t *testing.T) {
t.Error("Failed to search user admin")
}
}
func TestLdapConnectionTest(t *testing.T) {
var ldapConfig = models.LdapConf{
LdapURL: "ldap://127.0.0.1",
LdapSearchDn: "cn=admin,dc=example,dc=com",
LdapSearchPassword: "admin",
LdapBaseDn: "dc=example,dc=com",
LdapFilter: "",
LdapUID: "cn",
LdapScope: 3,
LdapConnectionTimeout: 10,
LdapVerifyCert: false,
}
//Test ldap connection under auth_mod is db_auth
err := ldap.ConnectionTestWithConfig(ldapConfig)
if err != nil {
t.Fatalf("Failed to test ldap server! error %v", err)
}
}

BIN
src/ui/auth/db/debug.test Executable file

Binary file not shown.

View File

@ -65,31 +65,31 @@ var adminServerLdapTestConfig = map[string]interface{}{
common.AdminInitialPassword: "password",
}
func TestMain(t *testing.T) {
func TestMain(m *testing.M) {
server, err := test.NewAdminserver(adminServerLdapTestConfig)
if err != nil {
t.Fatalf("failed to create a mock admin server: %v", err)
log.Fatalf("failed to create a mock admin server: %v", err)
}
defer server.Close()
if err := os.Setenv("ADMINSERVER_URL", server.URL); err != nil {
t.Fatalf("failed to set env %s: %v", "ADMINSERVER_URL", err)
log.Fatalf("failed to set env %s: %v", "ADMINSERVER_URL", err)
}
secretKeyPath := "/tmp/secretkey"
_, err = test.GenerateKey(secretKeyPath)
if err != nil {
t.Errorf("failed to generate secret key: %v", err)
log.Errorf("failed to generate secret key: %v", err)
return
}
defer os.Remove(secretKeyPath)
if err := os.Setenv("KEY_PATH", secretKeyPath); err != nil {
t.Fatalf("failed to set env %s: %v", "KEY_PATH", err)
log.Fatalf("failed to set env %s: %v", "KEY_PATH", err)
}
if err := uiConfig.Init(); err != nil {
t.Fatalf("failed to initialize configurations: %v", err)
log.Fatalf("failed to initialize configurations: %v", err)
}
database, err := uiConfig.Database()
@ -100,6 +100,9 @@ func TestMain(t *testing.T) {
if err := dao.InitDatabase(database); err != nil {
log.Fatalf("failed to initialize database: %v", err)
}
retCode := m.Run()
os.Exit(retCode)
}
func TestAuthenticate(t *testing.T) {

View File

@ -72,7 +72,7 @@ func init() {
}
// TestMain is a sample to run an endpoint test
func TestMain(t *testing.T) {
func TestAll(t *testing.T) {
assert := assert.New(t)
// v := url.Values{}

View File

@ -17,6 +17,5 @@ import (
"testing"
)
func TestMain(t *testing.T) {
func TestMain(m *testing.M) {
}

View File

@ -12,13 +12,3 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package main
/*
import (
"testing"
)
func TestMain(t *testing.T) {
}
*/

View File

@ -17,6 +17,5 @@ import (
"testing"
)
func TestMain(t *testing.T) {
func TestMain(m *testing.M) {
}

1
tests/common Symbolic link
View File

@ -0,0 +1 @@
../make/common