Merge pull request #1831 from soulseen/release-2.1.1/verify

verify secret
This commit is contained in:
KubeSphere CI Bot
2020-02-11 15:05:32 +08:00
committed by GitHub
3 changed files with 52 additions and 0 deletions

View File

@@ -5,6 +5,7 @@ import (
"time"
)
// TODO: deprecated, use github.com/docker/docker/api/types.AuthConfig instead
type AuthInfo struct {
Username string `json:"username" description:"username"`
Password string `json:"password" description:"password"`

View File

@@ -28,6 +28,8 @@ import (
"k8s.io/klog"
log "k8s.io/klog"
"kubesphere.io/kubesphere/pkg/informers"
"net/http"
"strings"
)
const (
@@ -68,6 +70,23 @@ func RegistryVerify(authInfo AuthInfo) error {
ServerAddress: authInfo.ServerHost,
}
// Sometimes request DockerHub V2 API will timeout in China. Skipped.
if authInfo.ServerHost != DefaultDockerHub {
var checkAPIVersionUrl string
if strings.Contains(authInfo.ServerHost, "://") {
checkAPIVersionUrl = authInfo.ServerHost + "/v2/"
} else {
// default use HTTPS protocol
checkAPIVersionUrl = "https://" + authInfo.ServerHost + "/v2/"
}
_, err := http.Get(checkAPIVersionUrl)
if err != nil {
return err
}
}
// TODO: deprecated, use native Docker Registry v2 authentication.
resp, err := cli.RegistryLogin(ctx, config)
cli.Close()

View File

@@ -0,0 +1,32 @@
package registries
import (
"testing"
)
func TestRegistryVerify(t *testing.T) {
type testRegistry struct {
Auth AuthInfo
Result bool
}
// some registry can not login with guest.
registries := []testRegistry{
{Auth: AuthInfo{Username: "guest", Password: "guest", ServerHost: "docker.io"}, Result: true},
{Auth: AuthInfo{Username: "guest", Password: "guest", ServerHost: "dockerhub.qingcloud.com"}, Result: true},
{Auth: AuthInfo{Username: "guest", Password: "guest", ServerHost: "https://dockerhub.qingcloud.com"}, Result: true},
{Auth: AuthInfo{Username: "guest", Password: "guest", ServerHost: "http://dockerhub.qingcloud.com"}, Result: false},
{Auth: AuthInfo{Username: "guest", Password: "guest", ServerHost: "registry.cn-hangzhou.aliyuncs.com"}, Result: false},
}
for _, registry := range registries {
err := RegistryVerify(registry.Auth)
if registry.Result == true && err != nil {
t.Fatalf("Get err %s", err)
}
if registry.Result == false && err == nil {
t.Fatalf("Input Wrong data but without any error.")
}
}
}