From 82d39e10b61509c781ea6a6467d69e38d17e507f Mon Sep 17 00:00:00 2001 From: Porsh33 Date: Wed, 6 Apr 2022 20:51:38 +0300 Subject: [PATCH] feat(providers): Added BasicAuth support --- pb/api.proto | 2 + server/api/provider/create.go | 4 ++ server/api/provider/update.go | 16 ++++--- server/core/provider.go | 2 + server/scheduler/scheduler.go | 47 +++++++++++-------- server/store/provider/provider.go | 5 +- .../providers-modal.component.html | 24 ++++++++++ .../providers-modal.component.ts | 10 +++- .../app/providers/shared/provider.class.ts | 4 ++ worker/app/server.go | 12 ++++- worker/git/clone.go | 8 ++-- 11 files changed, 100 insertions(+), 34 deletions(-) diff --git a/pb/api.proto b/pb/api.proto index 0922bca11..1e60c8094 100644 --- a/pb/api.proto +++ b/pb/api.proto @@ -69,6 +69,8 @@ message Job { string sshPrivateKey = 21; bool sshClone = 22; string branch = 23; + string providerHttpUser = 24; + string providerHttpPass = 25; } message Command { diff --git a/server/api/provider/create.go b/server/api/provider/create.go index a8956ca4e..03675c704 100644 --- a/server/api/provider/create.go +++ b/server/api/provider/create.go @@ -19,6 +19,8 @@ func HandleCreate(providers core.ProviderStore) http.HandlerFunc { Host string `json:"host" valid:"url,required"` AccessToken string `json:"accessToken" valid:"stringlength(12|50),required"` Secret string `json:"secret" valid:"stringlength(5|50),required"` + HttpUser string `json:"HttpUser"` + HttpPass string `json:"HttpPass"` } return func(w http.ResponseWriter, r *http.Request) { @@ -44,6 +46,8 @@ func HandleCreate(providers core.ProviderStore) http.HandlerFunc { AccessToken: f.AccessToken, Secret: f.Secret, UserID: claims.ID, + HttpUser: f.HttpUser, + HttpPass: f.HttpPass, } if err := providers.Create(provider); err != nil { diff --git a/server/api/provider/update.go b/server/api/provider/update.go index 5b4925f63..3e35176fb 100644 --- a/server/api/provider/update.go +++ b/server/api/provider/update.go @@ -20,6 +20,8 @@ func HandleUpdate(providers core.ProviderStore, users core.UserStore) http.Handl Host string `json:"host" valid:"url,required"` AccessToken string `json:"accessToken"` Secret string `json:"secret" valid:"stringlength(5|50),required"` + HttpUser string `json:"HttpUser"` + HttpPass string `json:"HttpPass"` } return func(w http.ResponseWriter, r *http.Request) { @@ -52,12 +54,14 @@ func HandleUpdate(providers core.ProviderStore, users core.UserStore) http.Handl if p.UserID == claims.ID || user.Role == "admin" { provider := &core.Provider{ - ID: f.ID, - Name: f.Name, - URL: f.URL, - Host: f.Host, - Secret: f.Secret, - UserID: claims.ID, + ID: f.ID, + Name: f.Name, + URL: f.URL, + Host: f.Host, + Secret: f.Secret, + UserID: claims.ID, + HttpUser: f.HttpUser, + HttpPass: f.HttpPass, } if f.AccessToken != "" { diff --git a/server/core/provider.go b/server/core/provider.go index 9ea79da34..940a586e3 100644 --- a/server/core/provider.go +++ b/server/core/provider.go @@ -13,6 +13,8 @@ type ( Name string `gorm:"not null" json:"name"` URL string `gorm:"not null" json:"url"` AccessToken string `gorm:"not null" json:"-"` + HttpUser string `json:"HttpUser"` + HttpPass string `json:"HttpPass"` Secret string `gorm:"not null" json:"secret"` Host string `gorm:"not null" json:"host"` LastSync *time.Time `json:"lastSync"` diff --git a/server/scheduler/scheduler.go b/server/scheduler/scheduler.go index f2eb10658..a3e447bb8 100644 --- a/server/scheduler/scheduler.go +++ b/server/scheduler/scheduler.go @@ -312,27 +312,34 @@ func (s *scheduler) startJob(job *core.Job, worker *core.Worker) { s.logger.Errorf("error parsing commands for job %d: %s", job.ID, err.Error()) } + url := job.Build.Repository.Clone + if url == "" { + url = job.Build.Repository.URL + } + j := &pb.Job{ - Id: uint64(job.ID), - BuildId: uint64(job.BuildID), - Commands: commands.Commands, - Image: job.Image, - Env: envs, - Url: job.Build.Repository.URL, - SshURL: job.Build.Repository.CloneSSH, - ProviderName: job.Build.Repository.Provider.Name, - ProviderURL: job.Build.Repository.Provider.URL, - ProviderToken: job.Build.Repository.Provider.AccessToken, - Ref: job.Build.Ref, - CommitSHA: job.Build.Commit, - Branch: job.Build.Branch, - RepoName: job.Build.Repository.FullName, - Action: pb.Job_JobStart, - WorkerId: worker.ID, - Cache: strings.Split(job.Cache, ","), - Mount: strings.Split(job.Mount, ","), - SshPrivateKey: job.Build.Repository.SSHPrivateKey, - SshClone: job.Build.Repository.UseSSH, + Id: uint64(job.ID), + BuildId: uint64(job.BuildID), + Commands: commands.Commands, + Image: job.Image, + Env: envs, + Url: url, + SshURL: job.Build.Repository.CloneSSH, + ProviderName: job.Build.Repository.Provider.Name, + ProviderURL: job.Build.Repository.Provider.URL, + ProviderToken: job.Build.Repository.Provider.AccessToken, + Ref: job.Build.Ref, + CommitSHA: job.Build.Commit, + Branch: job.Build.Branch, + RepoName: job.Build.Repository.FullName, + Action: pb.Job_JobStart, + WorkerId: worker.ID, + Cache: strings.Split(job.Cache, ","), + Mount: strings.Split(job.Mount, ","), + SshPrivateKey: job.Build.Repository.SSHPrivateKey, + SshClone: job.Build.Repository.UseSSH, + ProviderHttpUser: job.Build.Repository.Provider.HttpUser, + ProviderHttpPass: job.Build.Repository.Provider.HttpPass, } s.mu.Lock() diff --git a/server/store/provider/provider.go b/server/store/provider/provider.go index a1b72dcd4..4b86ae9d3 100644 --- a/server/store/provider/provider.go +++ b/server/store/provider/provider.go @@ -44,7 +44,10 @@ func (s providerStore) Create(provider *core.Provider) error { } func (s providerStore) Update(provider *core.Provider) error { - return s.db.Model(provider).Updates(&provider).Error + updateHttp := make(map[string]interface{}) + updateHttp["HttpUser"] = provider.HttpUser + updateHttp["HttpPass"] = provider.HttpPass + return s.db.Model(provider).Updates(updateHttp).Updates(&provider).Error } func (s providerStore) Delete(provider *core.Provider) error { diff --git a/web/abstruse/src/app/providers/providers-modal/providers-modal.component.html b/web/abstruse/src/app/providers/providers-modal/providers-modal.component.html index 97f3b4290..d53ff4107 100644 --- a/web/abstruse/src/app/providers/providers-modal/providers-modal.component.html +++ b/web/abstruse/src/app/providers/providers-modal/providers-modal.component.html @@ -51,6 +51,30 @@

Git SCM Provider Integration

Host URL is URL where SCM provider can access Abstruse to trigger webhooks. +
+ + + + + Basic auth for clone operations via HTTP + +
{ data.host, data.secret, data.accessToken, + data.HttpUser, + data.HttpPass, data.lastSync ? new Date(data.lastSync) : null, data.userID, new Date(data.createdAt), diff --git a/worker/app/server.go b/worker/app/server.go index 3e2270bd0..99c603d0b 100644 --- a/worker/app/server.go +++ b/worker/app/server.go @@ -219,11 +219,21 @@ func (s *Server) StartJob(job *pb.Job, stream pb.API_StartJobServer) error { logch <- []byte(yellow(fmt.Sprintf("==> Cloning repository %s ref: %s sha: %s... ", job.GetSshURL(), job.GetRef(), job.GetCommitSHA()))) } + HttpUser := job.GetProviderHttpUser() + if HttpUser == "" { + HttpUser = "user" + } + HttpPass := job.GetProviderHttpPass() + if HttpPass == "" { + HttpPass = job.GetProviderToken() + } + if err := git.CloneRepository( job.GetUrl(), job.GetRef(), job.GetCommitSHA(), - job.GetProviderToken(), + HttpUser, + HttpPass, dir, job.GetSshURL(), []byte(job.GetSshPrivateKey()), diff --git a/worker/git/clone.go b/worker/git/clone.go index f82935d82..89f1df0b0 100644 --- a/worker/git/clone.go +++ b/worker/git/clone.go @@ -14,14 +14,14 @@ import ( ) // CloneRepository clones repository contents to specified path. -func CloneRepository(url, ref, commit, token, dir, sshURL string, sshKey []byte, useSSH bool) error { +func CloneRepository(url, ref, commit, user, pass, dir, sshURL string, sshKey []byte, useSSH bool) error { var auth transport.AuthMethod var err error - if token != "" && !useSSH { + if pass != "" && !useSSH { auth = &http.BasicAuth{ - Username: "user", - Password: token, + Username: user, + Password: pass, } } else if useSSH { url = sshURL