mirror of
https://github.com/goharbor/harbor.git
synced 2025-01-05 07:27:50 +01:00
Fix ldap ping issue #3653
This commit is contained in:
parent
0abd30f9f4
commit
9393d26fdc
@ -17,6 +17,5 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMain(t *testing.T) {
|
func TestMain(m *testing.M) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -190,8 +190,14 @@ func (session *Session) ConnectionTest() error {
|
|||||||
//ConnectionTestWithConfig - test ldap session connection, out of the scope of normal session create/close
|
//ConnectionTestWithConfig - test ldap session connection, out of the scope of normal session create/close
|
||||||
func ConnectionTestWithConfig(ldapConfig models.LdapConf) error {
|
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 no password present, use the system default password
|
||||||
if ldapConfig.LdapSearchPassword == "" {
|
if ldapConfig.LdapSearchPassword == "" && authMode == "ldap_auth" {
|
||||||
|
|
||||||
session, err := LoadSystemLdapConfig()
|
session, err := LoadSystemLdapConfig()
|
||||||
|
|
||||||
|
@ -88,31 +88,31 @@ var adminServerDefaultConfigWithVerifyCert = map[string]interface{}{
|
|||||||
common.WithClair: false,
|
common.WithClair: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMain(t *testing.T) {
|
func TestMain(m *testing.M) {
|
||||||
server, err := test.NewAdminserver(adminServerLdapTestConfig)
|
server, err := test.NewAdminserver(adminServerLdapTestConfig)
|
||||||
if err != nil {
|
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()
|
defer server.Close()
|
||||||
|
|
||||||
if err := os.Setenv("ADMINSERVER_URL", server.URL); err != nil {
|
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"
|
secretKeyPath := "/tmp/secretkey"
|
||||||
_, err = test.GenerateKey(secretKeyPath)
|
_, err = test.GenerateKey(secretKeyPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("failed to generate secret key: %v", err)
|
log.Errorf("failed to generate secret key: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer os.Remove(secretKeyPath)
|
defer os.Remove(secretKeyPath)
|
||||||
|
|
||||||
if err := os.Setenv("KEY_PATH", secretKeyPath); err != nil {
|
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 {
|
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()
|
database, err := uiConfig.Database()
|
||||||
@ -123,6 +123,9 @@ func TestMain(t *testing.T) {
|
|||||||
if err := dao.InitDatabase(database); err != nil {
|
if err := dao.InitDatabase(database); err != nil {
|
||||||
log.Fatalf("failed to initialize database: %v", err)
|
log.Fatalf("failed to initialize database: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
os.Exit(m.Run())
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLoadSystemLdapConfig(t *testing.T) {
|
func TestLoadSystemLdapConfig(t *testing.T) {
|
||||||
@ -173,10 +176,21 @@ func TestCreateUIConfig(t *testing.T) {
|
|||||||
LdapScope: 1,
|
LdapScope: 1,
|
||||||
LdapURL: "ldaps://127.0.0.1",
|
LdapURL: "ldaps://127.0.0.1",
|
||||||
}, 0},
|
}, 0},
|
||||||
|
{
|
||||||
|
models.LdapConf{
|
||||||
|
LdapScope: 1,
|
||||||
|
LdapURL: "ldaps://127.0.0.1:abc",
|
||||||
|
}, -1},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, val := range testConfigs {
|
for _, val := range testConfigs {
|
||||||
session, err := CreateWithUIConfig(val.config)
|
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 {
|
if err != nil {
|
||||||
t.Fatalf("Can not create with ui config, err:%v", err)
|
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) {
|
func TestFormatURL(t *testing.T) {
|
||||||
|
|
||||||
var invalidURL = "http://localhost:389"
|
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"},
|
{"ldaps://127.0.0.1:389", "ldaps://127.0.0.1:389"},
|
||||||
{"ldap://127.0.0.1:636", "ldaps://127.0.0.1:636"},
|
{"ldap://127.0.0.1:636", "ldaps://127.0.0.1:636"},
|
||||||
{"112.122.122.122", "ldap://112.122.122.122:389"},
|
{"112.122.122.122", "ldap://112.122.122.122:389"},
|
||||||
|
{"ldap:\\wrong url", ""},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, u := range urls {
|
for _, u := range urls {
|
||||||
goodURL, err := formatURL(u.rawURL)
|
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 {
|
if err != nil || goodURL != u.goodURL {
|
||||||
t.Fatalf("Faild on URL: raw=%v, expected:%v, actual:%v", u.rawURL, u.goodURL, goodURL)
|
t.Fatalf("Faild on URL: raw=%v, expected:%v, actual:%v", u.rawURL, u.goodURL, goodURL)
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,5 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMain(t *testing.T) {
|
func TestMain(m *testing.M) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,6 +17,5 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMain(t *testing.T) {
|
func TestMain(m *testing.M) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,6 +17,5 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMain(t *testing.T) {
|
func TestMain(m *testing.M) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,19 +48,18 @@ func (l *LdapAPI) Prepare() {
|
|||||||
// Ping ...
|
// Ping ...
|
||||||
func (l *LdapAPI) Ping() {
|
func (l *LdapAPI) Ping() {
|
||||||
var ldapConfs models.LdapConf
|
var ldapConfs models.LdapConf
|
||||||
|
var err error
|
||||||
var ldapSession *ldapUtils.Session
|
var ldapSession *ldapUtils.Session
|
||||||
|
|
||||||
l.Ctx.Input.CopyBody(1 << 32)
|
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) == "" {
|
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()
|
err = ldapSession.ConnectionTest()
|
||||||
} else {
|
} else {
|
||||||
l.DecodeJSONReqAndValidate(&ldapConfs)
|
l.DecodeJSONReqAndValidate(&ldapConfs)
|
||||||
|
@ -23,6 +23,7 @@ import (
|
|||||||
"github.com/vmware/harbor/src/common/utils/test"
|
"github.com/vmware/harbor/src/common/utils/test"
|
||||||
|
|
||||||
"github.com/vmware/harbor/src/common/models"
|
"github.com/vmware/harbor/src/common/models"
|
||||||
|
"github.com/vmware/harbor/src/common/utils/ldap"
|
||||||
"github.com/vmware/harbor/src/ui/auth"
|
"github.com/vmware/harbor/src/ui/auth"
|
||||||
uiConfig "github.com/vmware/harbor/src/ui/config"
|
uiConfig "github.com/vmware/harbor/src/ui/config"
|
||||||
)
|
)
|
||||||
@ -64,31 +65,31 @@ var adminServerTestConfig = map[string]interface{}{
|
|||||||
common.AdminInitialPassword: "password",
|
common.AdminInitialPassword: "password",
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMain(t *testing.T) {
|
func TestMain(m *testing.M) {
|
||||||
server, err := test.NewAdminserver(adminServerTestConfig)
|
server, err := test.NewAdminserver(adminServerTestConfig)
|
||||||
if err != nil {
|
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()
|
defer server.Close()
|
||||||
|
|
||||||
if err := os.Setenv("ADMINSERVER_URL", server.URL); err != nil {
|
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"
|
secretKeyPath := "/tmp/secretkey"
|
||||||
_, err = test.GenerateKey(secretKeyPath)
|
_, err = test.GenerateKey(secretKeyPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("failed to generate secret key: %v", err)
|
log.Fatalf("failed to generate secret key: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer os.Remove(secretKeyPath)
|
defer os.Remove(secretKeyPath)
|
||||||
|
|
||||||
if err := os.Setenv("KEY_PATH", secretKeyPath); err != nil {
|
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 {
|
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()
|
database, err := uiConfig.Database()
|
||||||
@ -150,3 +151,22 @@ func TestAuthenticateHelperSearchUser(t *testing.T) {
|
|||||||
t.Error("Failed to search user admin")
|
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
BIN
src/ui/auth/db/debug.test
Executable file
Binary file not shown.
@ -65,31 +65,31 @@ var adminServerLdapTestConfig = map[string]interface{}{
|
|||||||
common.AdminInitialPassword: "password",
|
common.AdminInitialPassword: "password",
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMain(t *testing.T) {
|
func TestMain(m *testing.M) {
|
||||||
server, err := test.NewAdminserver(adminServerLdapTestConfig)
|
server, err := test.NewAdminserver(adminServerLdapTestConfig)
|
||||||
if err != nil {
|
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()
|
defer server.Close()
|
||||||
|
|
||||||
if err := os.Setenv("ADMINSERVER_URL", server.URL); err != nil {
|
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"
|
secretKeyPath := "/tmp/secretkey"
|
||||||
_, err = test.GenerateKey(secretKeyPath)
|
_, err = test.GenerateKey(secretKeyPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("failed to generate secret key: %v", err)
|
log.Errorf("failed to generate secret key: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer os.Remove(secretKeyPath)
|
defer os.Remove(secretKeyPath)
|
||||||
|
|
||||||
if err := os.Setenv("KEY_PATH", secretKeyPath); err != nil {
|
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 {
|
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()
|
database, err := uiConfig.Database()
|
||||||
@ -100,6 +100,9 @@ func TestMain(t *testing.T) {
|
|||||||
if err := dao.InitDatabase(database); err != nil {
|
if err := dao.InitDatabase(database); err != nil {
|
||||||
log.Fatalf("failed to initialize database: %v", err)
|
log.Fatalf("failed to initialize database: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
retCode := m.Run()
|
||||||
|
os.Exit(retCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAuthenticate(t *testing.T) {
|
func TestAuthenticate(t *testing.T) {
|
||||||
|
@ -72,7 +72,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TestMain is a sample to run an endpoint test
|
// TestMain is a sample to run an endpoint test
|
||||||
func TestMain(t *testing.T) {
|
func TestAll(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
// v := url.Values{}
|
// v := url.Values{}
|
||||||
|
@ -17,6 +17,5 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMain(t *testing.T) {
|
func TestMain(m *testing.M) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,13 +12,3 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
package main
|
package main
|
||||||
|
|
||||||
/*
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestMain(t *testing.T) {
|
|
||||||
}
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
@ -17,6 +17,5 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMain(t *testing.T) {
|
func TestMain(m *testing.M) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
1
tests/common
Symbolic link
1
tests/common
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../make/common
|
Loading…
Reference in New Issue
Block a user