Skip to content

Commit

Permalink
feat(providers): Added BasicAuth support
Browse files Browse the repository at this point in the history
  • Loading branch information
Porsh33 committed Apr 6, 2022
1 parent b3c3e99 commit 82d39e1
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 34 deletions.
2 changes: 2 additions & 0 deletions pb/api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ message Job {
string sshPrivateKey = 21;
bool sshClone = 22;
string branch = 23;
string providerHttpUser = 24;
string providerHttpPass = 25;
}

message Command {
Expand Down
4 changes: 4 additions & 0 deletions server/api/provider/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down
16 changes: 10 additions & 6 deletions server/api/provider/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 != "" {
Expand Down
2 changes: 2 additions & 0 deletions server/core/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
47 changes: 27 additions & 20 deletions server/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 4 additions & 1 deletion server/store/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,30 @@ <h2>Git SCM Provider Integration</h2>
Host URL is URL where SCM provider can access Abstruse to trigger webhooks.
</span>
</div>
<div class="field">
<label class="label">Http Basic auth</label>
<input
type="text"
name="httpuser"
class="input"
placeholder="Http username for clone"
formControlName="httpUser"
autocomplete="off"
style="display: inline;box-sizing: border-box;width: calc(50% - 2px);"
/>
<input
type="text"
name="httppass"
class="input"
placeholder="Http password for clone"
formControlName="httpPass"
autocomplete="off"
style="display: inline;box-sizing: border-box;width: calc(50% - 2px);"
/>
<span class="form-helper">
Basic auth for clone operations via HTTP
</span>
</div>
<div class="field">
<label class="label">Access Token</label>
<input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,16 @@ export class ProvidersModalComponent implements OnInit {

this.error = null;
this.saving = true;
console.log('im here');
console.log(this.form.controls);
let data: any = {
name: this.form.controls.name.value,
url: this.form.controls.url.value,
host: this.form.controls.host.value,
accessToken: this.form.controls.accessToken.value,
secret: this.form.controls.secret.value
secret: this.form.controls.secret.value,
httpUser: this.form.controls.httpUser.value,
httpPass: this.form.controls.httpPass.value
};
if (this.provider && this.provider.id) {
data = { ...data, ...{ id: this.provider.id } };
Expand Down Expand Up @@ -141,7 +145,9 @@ export class ProvidersModalComponent implements OnInit {
[Validators.required]
],
accessToken: [(this.provider && this.provider.accessToken) || null],
secret: [(this.provider && this.provider.secret) || null, [Validators.required]]
secret: [(this.provider && this.provider.secret) || null, [Validators.required]],
httpUser: [(this.provider && this.provider.HttpUser) || null],
httpPass: [(this.provider && this.provider.HttpPass) || null]
});
}
}
4 changes: 4 additions & 0 deletions web/abstruse/src/app/providers/shared/provider.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export class Provider {
public host?: string,
public secret?: string,
public accessToken?: string,
public HttpUser?: string,
public HttpPass?: string,
public lastSync?: Date | null,
public userID?: number,
public createdAt?: Date,
Expand Down Expand Up @@ -51,6 +53,8 @@ export const generateProvider = (data: any): Provider => {
data.host,
data.secret,
data.accessToken,
data.HttpUser,
data.HttpPass,
data.lastSync ? new Date(data.lastSync) : null,
data.userID,
new Date(data.createdAt),
Expand Down
12 changes: 11 additions & 1 deletion worker/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
8 changes: 4 additions & 4 deletions worker/git/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 82d39e1

Please sign in to comment.