Merge pull request #2161 from shaowenchen/fix_jenkin_basic
fix jenkins basic auth
This commit is contained in:
@@ -79,7 +79,7 @@ const (
|
||||
|
||||
type NoScmPipeline struct {
|
||||
Name string `json:"name" description:"name of pipeline"`
|
||||
Description string `json:"descriptio,omitempty" description:"description of pipeline"`
|
||||
Description string `json:"description,omitempty" description:"description of pipeline"`
|
||||
Discarder *DiscarderProperty `json:"discarder,omitempty" description:"Discarder of pipeline, managing when to drop a pipeline"`
|
||||
Parameters []Parameter `json:"parameters,omitempty" description:"Parameters define of pipeline,user could pass param when run pipeline"`
|
||||
DisableConcurrent bool `json:"disable_concurrent,omitempty" mapstructure:"disable_concurrent" description:"Whether to prohibit the pipeline from running in parallel"`
|
||||
@@ -90,7 +90,7 @@ type NoScmPipeline struct {
|
||||
|
||||
type MultiBranchPipeline struct {
|
||||
Name string `json:"name" description:"name of pipeline"`
|
||||
Description string `json:"descriptio,omitempty" description:"description of pipeline"`
|
||||
Description string `json:"description,omitempty" description:"description of pipeline"`
|
||||
Discarder *DiscarderProperty `json:"discarder,omitempty" description:"Discarder of pipeline, managing when to drop a pipeline"`
|
||||
TimerTrigger *TimerTrigger `json:"timer_trigger,omitempty" mapstructure:"timer_trigger" description:"Timer to trigger pipeline run"`
|
||||
SourceType string `json:"source_type" description:"type of scm, such as github/git/svn"`
|
||||
|
||||
@@ -532,8 +532,8 @@ func AddPipelineToWebService(webservice *restful.WebService, devopsClient devops
|
||||
Param(webservice.QueryParameter("pageSize", "the item count of one page.").
|
||||
Required(true).
|
||||
DataFormat("pageSize=%d")).
|
||||
Returns(http.StatusOK, RespOK, []devops.OrgRepo{}).
|
||||
Writes([]devops.OrgRepo{}))
|
||||
Returns(http.StatusOK, RespOK, devops.OrgRepo{}).
|
||||
Writes(devops.OrgRepo{}))
|
||||
|
||||
// match "/blue/rest/organizations/jenkins/scm/%s/servers/" create bitbucket server
|
||||
webservice.Route(webservice.POST("/scms/{scm}/servers").
|
||||
|
||||
@@ -269,11 +269,10 @@ func (r *resourceHandler) handleUpdateRouter(request *restful.Request, response
|
||||
}
|
||||
|
||||
func (r *resourceHandler) handleVerifyGitCredential(request *restful.Request, response *restful.Response) {
|
||||
|
||||
var credential api.GitCredential
|
||||
err := request.ReadEntity(&credential)
|
||||
if err != nil {
|
||||
api.HandleBadRequest(response, nil, err)
|
||||
response.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
|
||||
return
|
||||
}
|
||||
var namespace, secretName string
|
||||
@@ -283,11 +282,10 @@ func (r *resourceHandler) handleVerifyGitCredential(request *restful.Request, re
|
||||
}
|
||||
err = r.gitVerifier.VerifyGitCredential(credential.RemoteUrl, namespace, secretName)
|
||||
if err != nil {
|
||||
api.HandleBadRequest(response, nil, err)
|
||||
response.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
|
||||
return
|
||||
}
|
||||
|
||||
response.WriteHeader(http.StatusOK)
|
||||
response.WriteAsJson(errors.None)
|
||||
}
|
||||
|
||||
func (r *resourceHandler) handleVerifyRegistryCredential(request *restful.Request, response *restful.Response) {
|
||||
|
||||
@@ -94,7 +94,7 @@ type DevopsOperator interface {
|
||||
|
||||
GetSCMServers(scmId string, req *http.Request) ([]devops.SCMServer, error)
|
||||
GetSCMOrg(scmId string, req *http.Request) ([]devops.SCMOrg, error)
|
||||
GetOrgRepo(scmId, organizationId string, req *http.Request) ([]devops.OrgRepo, error)
|
||||
GetOrgRepo(scmId, organizationId string, req *http.Request) (devops.OrgRepo, error)
|
||||
CreateSCMServers(scmId string, req *http.Request) (*devops.SCMServer, error)
|
||||
Validate(scmId string, req *http.Request) (*devops.Validates, error)
|
||||
|
||||
@@ -672,12 +672,12 @@ func (d devopsOperator) GetSCMOrg(scmId string, req *http.Request) ([]devops.SCM
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (d devopsOperator) GetOrgRepo(scmId, organizationId string, req *http.Request) ([]devops.OrgRepo, error) {
|
||||
func (d devopsOperator) GetOrgRepo(scmId, organizationId string, req *http.Request) (devops.OrgRepo, error) {
|
||||
|
||||
res, err := d.devopsClient.GetOrgRepo(scmId, organizationId, convertToHttpParameters(req))
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return nil, err
|
||||
return devops.OrgRepo{}, err
|
||||
}
|
||||
|
||||
return res, err
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user