mirror of
https://github.com/goharbor/harbor
synced 2024-09-21 02:49:59 +00:00
ping email server API enhancement
This commit is contained in:
parent
3b53b354d3
commit
b25f5f9692
|
@ -2082,7 +2082,7 @@ definitions:
|
||||||
type: string
|
type: string
|
||||||
description: The host of email server.
|
description: The host of email server.
|
||||||
email_port:
|
email_port:
|
||||||
type: string
|
type: integer
|
||||||
description: The port of email server.
|
description: The port of email server.
|
||||||
email_username:
|
email_username:
|
||||||
type: string
|
type: string
|
||||||
|
@ -2091,7 +2091,7 @@ definitions:
|
||||||
type: string
|
type: string
|
||||||
description: The password of email server.
|
description: The password of email server.
|
||||||
email_ssl:
|
email_ssl:
|
||||||
type: string
|
type: boolean
|
||||||
description: Use ssl/tls or not.
|
description: Use ssl/tls or not.
|
||||||
email_identity:
|
email_identity:
|
||||||
type: string
|
type: string
|
||||||
|
|
|
@ -16,13 +16,11 @@
|
||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/vmware/harbor/src/common/api"
|
"github.com/vmware/harbor/src/common/api"
|
||||||
comcfg "github.com/vmware/harbor/src/common/config"
|
|
||||||
"github.com/vmware/harbor/src/common/dao"
|
"github.com/vmware/harbor/src/common/dao"
|
||||||
"github.com/vmware/harbor/src/common/utils/email"
|
"github.com/vmware/harbor/src/common/utils/email"
|
||||||
"github.com/vmware/harbor/src/common/utils/log"
|
"github.com/vmware/harbor/src/common/utils/log"
|
||||||
|
@ -54,63 +52,35 @@ func (e *EmailAPI) Prepare() {
|
||||||
|
|
||||||
// Ping tests connection and authentication with email server
|
// Ping tests connection and authentication with email server
|
||||||
func (e *EmailAPI) Ping() {
|
func (e *EmailAPI) Ping() {
|
||||||
m := map[string]string{}
|
settings := &struct {
|
||||||
e.DecodeJSONReq(&m)
|
Host string `json:"email_host"`
|
||||||
|
Port *int `json:"email_port"`
|
||||||
|
Username string `json:"email_username"`
|
||||||
|
Password *string `json:"email_password"`
|
||||||
|
SSL bool `json:"email_ssl"`
|
||||||
|
Identity string `json:"email_identity"`
|
||||||
|
}{}
|
||||||
|
e.DecodeJSONReq(&settings)
|
||||||
|
|
||||||
settings, err := config.Email()
|
if len(settings.Host) == 0 || settings.Port == nil {
|
||||||
if err != nil {
|
e.CustomAbort(http.StatusBadRequest, "empty host or port")
|
||||||
e.CustomAbort(http.StatusInternalServerError,
|
|
||||||
http.StatusText(http.StatusInternalServerError))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
host, ok := m[comcfg.EmailHost]
|
if settings.Password == nil {
|
||||||
if ok {
|
cfg, err := config.Email()
|
||||||
if len(host) == 0 {
|
if err != nil {
|
||||||
e.CustomAbort(http.StatusBadRequest, "empty email server host")
|
log.Errorf("failed to get email configurations: %v", err)
|
||||||
|
e.CustomAbort(http.StatusInternalServerError,
|
||||||
|
http.StatusText(http.StatusInternalServerError))
|
||||||
}
|
}
|
||||||
settings.Host = host
|
|
||||||
|
*settings.Password = cfg.Password
|
||||||
}
|
}
|
||||||
|
|
||||||
port, ok := m[comcfg.EmailPort]
|
addr := net.JoinHostPort(settings.Host, strconv.Itoa(*settings.Port))
|
||||||
if ok {
|
|
||||||
if len(port) == 0 {
|
|
||||||
e.CustomAbort(http.StatusBadRequest, "empty email server port")
|
|
||||||
}
|
|
||||||
p, err := strconv.Atoi(port)
|
|
||||||
if err != nil || p <= 0 {
|
|
||||||
e.CustomAbort(http.StatusBadRequest, "invalid email server port")
|
|
||||||
}
|
|
||||||
settings.Port = p
|
|
||||||
}
|
|
||||||
|
|
||||||
username, ok := m[comcfg.EmailUsername]
|
|
||||||
if ok {
|
|
||||||
settings.Username = username
|
|
||||||
}
|
|
||||||
|
|
||||||
password, ok := m[comcfg.EmailPassword]
|
|
||||||
if ok {
|
|
||||||
settings.Password = password
|
|
||||||
}
|
|
||||||
|
|
||||||
identity, ok := m[comcfg.EmailIdentity]
|
|
||||||
if ok {
|
|
||||||
settings.Identity = identity
|
|
||||||
}
|
|
||||||
|
|
||||||
ssl, ok := m[comcfg.EmailSSL]
|
|
||||||
if ok {
|
|
||||||
if ssl != "0" && ssl != "1" {
|
|
||||||
e.CustomAbort(http.StatusBadRequest,
|
|
||||||
fmt.Sprintf("%s should be 0 or 1", comcfg.EmailSSL))
|
|
||||||
}
|
|
||||||
settings.SSL = ssl == "1"
|
|
||||||
}
|
|
||||||
|
|
||||||
addr := net.JoinHostPort(settings.Host, strconv.Itoa(settings.Port))
|
|
||||||
if err := email.Ping(
|
if err := email.Ping(
|
||||||
addr, settings.Identity, settings.Username,
|
addr, settings.Identity, settings.Username,
|
||||||
settings.Password, pingEmailTimeout, settings.SSL, false); err != nil {
|
*settings.Password, pingEmailTimeout, settings.SSL, false); err != nil {
|
||||||
log.Debugf("ping %s failed: %v", addr, err)
|
log.Debugf("ping %s failed: %v", addr, err)
|
||||||
e.CustomAbort(http.StatusBadRequest, err.Error())
|
e.CustomAbort(http.StatusBadRequest, err.Error())
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,6 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
comcfg "github.com/vmware/harbor/src/common/config"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestPingEmail(t *testing.T) {
|
func TestPingEmail(t *testing.T) {
|
||||||
|
@ -38,17 +37,17 @@ func TestPingEmail(t *testing.T) {
|
||||||
|
|
||||||
assert.Equal(401, code, "the status code of ping email server with non-admin user should be 401")
|
assert.Equal(401, code, "the status code of ping email server with non-admin user should be 401")
|
||||||
|
|
||||||
settings := map[string]string{
|
|
||||||
comcfg.EmailHost: "smtp.gmail.com",
|
|
||||||
comcfg.EmailPort: "465",
|
|
||||||
comcfg.EmailIdentity: "",
|
|
||||||
comcfg.EmailUsername: "wrong_username",
|
|
||||||
comcfg.EmailPassword: "wrong_password",
|
|
||||||
comcfg.EmailSSL: "1",
|
|
||||||
}
|
|
||||||
|
|
||||||
//case 2: secure connection with admin role
|
//case 2: secure connection with admin role
|
||||||
code, body, err := apiTest.PingEmail(*admin, settings)
|
settings := `{
|
||||||
|
"email_host": "smtp.gmail.com",
|
||||||
|
"email_port": 465,
|
||||||
|
"email_identity": "",
|
||||||
|
"email_username": "wrong_username",
|
||||||
|
"email_password": "wrong_password",
|
||||||
|
"email_ssl": true
|
||||||
|
}`
|
||||||
|
|
||||||
|
code, body, err := apiTest.PingEmail(*admin, []byte(settings))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("failed to test ping email server: %v", err)
|
t.Errorf("failed to test ping email server: %v", err)
|
||||||
return
|
return
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
@ -1029,8 +1030,8 @@ func (a testapi) PutConfig(authInfo usrInfo, cfg map[string]string) (int, error)
|
||||||
return code, err
|
return code, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a testapi) PingEmail(authInfo usrInfo, settings map[string]string) (int, string, error) {
|
func (a testapi) PingEmail(authInfo usrInfo, settings []byte) (int, string, error) {
|
||||||
_sling := sling.New().Base(a.basePath).Post("/api/email/ping").BodyJSON(settings)
|
_sling := sling.New().Base(a.basePath).Post("/api/email/ping").Body(bytes.NewReader(settings))
|
||||||
|
|
||||||
code, body, err := request(_sling, jsonAcceptHeader, authInfo)
|
code, body, err := request(_sling, jsonAcceptHeader, authInfo)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user