update verify func
This commit is contained in:
@@ -11,34 +11,45 @@ import (
|
||||
)
|
||||
|
||||
type AuthInfo struct {
|
||||
RemoteUrl string `json:"remoteUrl"`
|
||||
RemoteUrl string `json:"remoteUrl"`
|
||||
SecretRef *corev1.SecretReference `json:"secretRef,omitempty"`
|
||||
}
|
||||
|
||||
func GitReadVerify(namespace string, name string, authInfo AuthInfo) error {
|
||||
secret, err := informers.SharedInformerFactory().Core().V1().Secrets().Lister().Secrets(namespace).Get(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
username, ok := secret.Data[corev1.BasicAuthUsernameKey]
|
||||
if !ok {
|
||||
return fmt.Errorf("could not get username in secret %s", secret.Name)
|
||||
}
|
||||
password, ok := secret.Data[corev1.BasicAuthPasswordKey]
|
||||
if !ok {
|
||||
return fmt.Errorf("could not get password in secret %s", secret.Name)
|
||||
func GitReadVerify(namespace string, authInfo AuthInfo) error {
|
||||
username := ""
|
||||
password := ""
|
||||
if authInfo.SecretRef != nil {
|
||||
secret, err := informers.SharedInformerFactory().Core().V1().Secrets().Lister().
|
||||
Secrets(authInfo.SecretRef.Namespace).Get(authInfo.SecretRef.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
usernameBytes, ok := secret.Data[corev1.BasicAuthUsernameKey]
|
||||
if !ok {
|
||||
return fmt.Errorf("could not get username in secret %s", secret.Name)
|
||||
}
|
||||
passwordBytes, ok := secret.Data[corev1.BasicAuthPasswordKey]
|
||||
if !ok {
|
||||
return fmt.Errorf("could not get password in secret %s", secret.Name)
|
||||
}
|
||||
username = string(usernameBytes)
|
||||
password = string(passwordBytes)
|
||||
}
|
||||
|
||||
return gitReadVerifyWithBasicAuth(string(username), string(password), authInfo.RemoteUrl)
|
||||
}
|
||||
|
||||
func gitReadVerifyWithBasicAuth(username string, password string, remote string) error {
|
||||
r, _ := git.Init(memory.NewStorage(), nil)
|
||||
|
||||
// Add a new remote, with the default fetch refspec
|
||||
origin, err := r.CreateRemote(&config.RemoteConfig{
|
||||
Name: git.DefaultRemoteName,
|
||||
URLs: []string{authInfo.RemoteUrl},
|
||||
URLs: []string{remote},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = origin.List(&git.ListOptions{Auth:
|
||||
&http.BasicAuth{Username: string(username), Password: string(password)}})
|
||||
_, err = origin.List(&git.ListOptions{Auth: &http.BasicAuth{Username: string(username), Password: string(password)}})
|
||||
return err
|
||||
}
|
||||
|
||||
51
pkg/models/git/git_test.go
Normal file
51
pkg/models/git/git_test.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package git
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGitReadVerifyWithBasicAuth(t *testing.T) {
|
||||
shouldSuccess := []map[string]string{
|
||||
{
|
||||
"username": "",
|
||||
"password": "",
|
||||
"remote": "https://github.com/kubesphere/kubesphere",
|
||||
},
|
||||
}
|
||||
shouldFailed := []map[string]string{
|
||||
{
|
||||
"username": "",
|
||||
"password": "",
|
||||
"remote": "https://github.com/kubesphere/kubesphere12222",
|
||||
},
|
||||
{
|
||||
"username": "",
|
||||
"password": "",
|
||||
"remote": "git@github.com:kubesphere/kubesphere.git",
|
||||
},
|
||||
{
|
||||
"username": "runzexia",
|
||||
"password": "",
|
||||
"remote": "git@github.com:kubesphere/kubesphere.git",
|
||||
},
|
||||
{
|
||||
"username": "",
|
||||
"password": "",
|
||||
"remote": "git@fdsfs41342`@@@2414!!!!github.com:kubesphere/kubesphere.git",
|
||||
},
|
||||
}
|
||||
for _, item := range shouldSuccess {
|
||||
err := gitReadVerifyWithBasicAuth(item["username"], item["password"], item["remote"])
|
||||
if err != nil {
|
||||
|
||||
t.Errorf("should could access repo [%s] with %s:%s, %v", item["username"], item["password"], item["remote"], err)
|
||||
}
|
||||
}
|
||||
|
||||
for _, item := range shouldFailed {
|
||||
err := gitReadVerifyWithBasicAuth(item["username"], item["password"], item["remote"])
|
||||
if err == nil {
|
||||
t.Errorf("should could access repo [%s] with %s:%s ", item["username"], item["password"], item["remote"])
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user