added individual auth secrets

This commit is contained in:
ston1th 2019-12-14 14:49:48 +01:00
commit 7081607b7f
2 changed files with 39 additions and 6 deletions

View file

@ -43,6 +43,8 @@ metadata:
type: Opaque type: Opaque
stringData: stringData:
github.com: "<username>:<password>" github.com: "<username>:<password>"
github.com_foo: "<foo username>:<foo password>"
github.com_foo_bar: "<bar username>:<bar password>"
EOF EOF
``` ```

43
main.go
View file

@ -49,6 +49,14 @@ func getNS() (string, error) {
return "default", nil return "default", nil
} }
func makeKeys(host, owner, repo string) []string {
return []string{
host + secretDelim + owner + secretDelim + repo,
host + secretDelim + owner,
host,
}
}
func mapGitAPI(s string) string { func mapGitAPI(s string) string {
switch s { switch s {
case "ghe", "github": case "ghe", "github":
@ -62,7 +70,7 @@ func mapGitAPI(s string) string {
func mapState(s string) (state, description string) { func mapState(s string) (state, description string) {
switch s { switch s {
case "Running": case "Running":
return "pending", "Pending - Job triggered." return "pending", "Job triggered."
case "PipelineRunCancelled": case "PipelineRunCancelled":
return "error", "Job cancelled." return "error", "Job cancelled."
case "Failed": case "Failed":
@ -221,6 +229,8 @@ func statusUpdate(obj interface{}, s *StatusCache, sec typedcorev1.SecretInterfa
const ( const (
contentType = "Content-Type" contentType = "Content-Type"
jsonType = "application/json" jsonType = "application/json"
secretDelim = "_"
) )
type GitStatus struct { type GitStatus struct {
@ -230,13 +240,22 @@ type GitStatus struct {
TargetURL string `json:"target_url"` TargetURL string `json:"target_url"`
} }
func getSecret(sec *corev1.Secret, host string) (username, password string, err error) { func getSecret(sec *corev1.Secret, host, owner, repo string) (username, password string, err error) {
s, ok := sec.Data[host] var (
secret []byte
ok bool
)
for _, v := range makeKeys(host, owner, repo) {
secret, ok = sec.Data[v]
if ok {
break
}
}
if !ok { if !ok {
err = fmt.Errorf("no key found for host %s in secret %s/%s", host, sec.Namespace, sec.Name) err = fmt.Errorf("no key found for host %s in secret %s/%s", host, sec.Namespace, sec.Name)
return return
} }
a := strings.SplitN(string(s), ":", 2) a := strings.SplitN(string(secret), ":", 2)
if len(a) < 2 { if len(a) < 2 {
err = fmt.Errorf("missing credentials for host %s in secret %s/%s", host, sec.Namespace, sec.Name) err = fmt.Errorf("missing credentials for host %s in secret %s/%s", host, sec.Namespace, sec.Name)
return return
@ -246,6 +265,16 @@ func getSecret(sec *corev1.Secret, host string) (username, password string, err
return return
} }
func getOwnerRepo(uri string) (owner, repo string) {
a := strings.SplitN(uri, "/", 2)
if len(a) < 2 {
return
}
owner = a[0]
repo = a[1]
return
}
func postStatus(pr *PipelineRun, gitUrl, sha, status string, sec *corev1.Secret) error { func postStatus(pr *PipelineRun, gitUrl, sha, status string, sec *corev1.Secret) error {
u, err := url.Parse(gitUrl) u, err := url.Parse(gitUrl)
if err != nil { if err != nil {
@ -284,7 +313,8 @@ func postStatus(pr *PipelineRun, gitUrl, sha, status string, sec *corev1.Secret)
return err return err
} }
req.Header.Set(contentType, jsonType) req.Header.Set(contentType, jsonType)
user, pass, err := getSecret(sec, u.Host) owner, repo := getOwnerRepo(uri)
user, pass, err := getSecret(sec, u.Host, owner, repo)
if err != nil { if err != nil {
return err return err
} }
@ -321,7 +351,8 @@ func postComment(pr *PipelineRun, gitUrl, issue string, sec *corev1.Secret) erro
} }
scheme := getParam(pr, "scheme") scheme := getParam(pr, "scheme")
preview := fmt.Sprintf("Preview Environment: [Link](%s%s)", scheme, previewurl) preview := fmt.Sprintf("Preview Environment: [Link](%s%s)", scheme, previewurl)
user, pass, err := getSecret(sec, u.Host) owner, repo := getOwnerRepo(uri)
user, pass, err := getSecret(sec, u.Host, owner, repo)
if err != nil { if err != nil {
return err return err
} }