Merge pull request #2161 from shaowenchen/fix_jenkin_basic

fix jenkins basic auth
This commit is contained in:
KubeSphere CI Bot
2020-06-09 11:21:39 +08:00
committed by GitHub
9 changed files with 45 additions and 21 deletions

View File

@@ -254,8 +254,8 @@ func (d *Devops) GetSCMServers(scmId string, httpParameters *devops.HttpParamete
func (d *Devops) GetSCMOrg(scmId string, httpParameters *devops.HttpParameters) ([]devops.SCMOrg, error) {
return nil, nil
}
func (d *Devops) GetOrgRepo(scmId, organizationId string, httpParameters *devops.HttpParameters) ([]devops.OrgRepo, error) {
return nil, nil
func (d *Devops) GetOrgRepo(scmId, organizationId string, httpParameters *devops.HttpParameters) (devops.OrgRepo, error) {
return devops.OrgRepo{}, nil
}
func (d *Devops) CreateSCMServers(scmId string, httpParameters *devops.HttpParameters) (*devops.SCMServer, error) {
return nil, nil

View File

@@ -658,7 +658,7 @@ func (j *Jenkins) GetSCMServers(scmId string, httpParameters *devops.HttpParamet
PipelineOjb := &Pipeline{
HttpParameters: httpParameters,
Jenkins: j,
Path: GetSCMServersUrl,
Path: fmt.Sprintf(GetSCMServersUrl, scmId),
}
res, err := PipelineOjb.GetSCMServers()
return res, err
@@ -674,7 +674,7 @@ func (j *Jenkins) GetSCMOrg(scmId string, httpParameters *devops.HttpParameters)
return res, err
}
func (j *Jenkins) GetOrgRepo(scmId, organizationId string, httpParameters *devops.HttpParameters) ([]devops.OrgRepo, error) {
func (j *Jenkins) GetOrgRepo(scmId, organizationId string, httpParameters *devops.HttpParameters) (devops.OrgRepo, error) {
PipelineOjb := &Pipeline{
HttpParameters: httpParameters,
Jenkins: j,

View File

@@ -523,16 +523,16 @@ func (p *Pipeline) GetSCMOrg() ([]devops.SCMOrg, error) {
return SCMOrg, err
}
func (p *Pipeline) GetOrgRepo() ([]devops.OrgRepo, error) {
func (p *Pipeline) GetOrgRepo() (devops.OrgRepo, error) {
res, err := p.Jenkins.SendPureRequest(p.Path, p.HttpParameters)
if err != nil {
klog.Error(err)
}
var OrgRepo []devops.OrgRepo
var OrgRepo devops.OrgRepo
err = json.Unmarshal(res, &OrgRepo)
if err != nil {
klog.Error(err)
return nil, err
return devops.OrgRepo{}, err
}
return OrgRepo, err

View File

@@ -1,10 +1,15 @@
package jenkins
import (
"encoding/base64"
"fmt"
"github.com/dgrijalva/jwt-go"
"k8s.io/klog"
authtoken "kubesphere.io/kubesphere/pkg/apiserver/authentication/token"
"kubesphere.io/kubesphere/pkg/simple/client/devops"
"net/http"
"net/url"
"strings"
"time"
)
@@ -15,6 +20,8 @@ func (j *Jenkins) SendPureRequest(path string, httpParameters *devops.HttpParame
return resBody, err
}
// provider request header to call jenkins api.
// transfer bearer token to basic token for inner Oauth and Jeknins
func (j *Jenkins) SendPureRequestWithHeaderResp(path string, httpParameters *devops.HttpParameters) ([]byte, http.Header, error) {
Url, err := url.Parse(j.Server + path)
if err != nil {
@@ -24,10 +31,29 @@ func (j *Jenkins) SendPureRequestWithHeaderResp(path string, httpParameters *dev
client := &http.Client{Timeout: 30 * time.Second}
header := httpParameters.Header
bearTokenArray := strings.Split(header.Get("Authorization"), " ")
bearFlag := bearTokenArray[0]
if strings.ToLower(bearFlag) == "bearer" {
bearToken := bearTokenArray[1]
if err != nil {
klog.Error(err)
return nil, nil, err
}
claim := authtoken.Claims{}
parser := jwt.Parser{}
_, _, err = parser.ParseUnverified(bearToken, &claim)
if err != nil {
return nil, nil, err
}
creds := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", claim.Username, bearToken)))
header.Set("Authorization", fmt.Sprintf("Basic %s", creds))
}
newRequest := &http.Request{
Method: httpParameters.Method,
URL: Url,
Header: httpParameters.Header,
Header: header,
Body: httpParameters.Body,
Form: httpParameters.Form,
PostForm: httpParameters.PostForm,

View File

@@ -1107,7 +1107,7 @@ type PipelineOperator interface {
// SCM operator interface
GetSCMServers(scmId string, httpParameters *HttpParameters) ([]SCMServer, error)
GetSCMOrg(scmId string, httpParameters *HttpParameters) ([]SCMOrg, error)
GetOrgRepo(scmId, organizationId string, httpParameters *HttpParameters) ([]OrgRepo, error)
GetOrgRepo(scmId, organizationId string, httpParameters *HttpParameters) (OrgRepo, error)
CreateSCMServers(scmId string, httpParameters *HttpParameters) (*SCMServer, error)
Validate(scmId string, httpParameters *HttpParameters) (*Validates, error)