forked from forj-oss/forjj-jenkins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-registry-creds.go
60 lines (50 loc) · 1.49 KB
/
docker-registry-creds.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
log "forjj-jenkins/reportlogs"
"encoding/base64"
"fmt"
"regexp"
"strings"
"github.com/forj-oss/forjj-modules/trace"
)
type DockerAuths struct {
Auths map[string]DockerRegistryCredsInfo
}
type DockerRegistryCredsInfo struct {
user string
password string
}
func (a *DockerAuths) authenticate(server string) error {
if _, found := a.Auths[server]; !found {
return fmt.Errorf("Unable to authenticate to docker registry '%s'. Server not found", server)
}
auth := a.Auths[server]
gotrace.SetDebug()
if cmdlog, err := run_cmd("sudo", nil, "docker", "login", "-u", auth.user, "-p", auth.password, server); err != nil {
log.Reportf("Unable to authenticate. %s. docker output: %s", err, cmdlog)
} else {
log.Printf("%s", cmdlog)
}
return nil
}
func NewDockerAuths(auths string) (a *DockerAuths) {
auths_s := strings.Split(auths, ",")
auth_reg, _ := regexp.Compile(`([^:]+):(\w+=*)`)
a = new(DockerAuths)
a.Auths = make(map[string]DockerRegistryCredsInfo)
for _, v := range auths_s {
if substr := auth_reg.FindStringSubmatch(v); substr != nil {
var user_pwd []string
if v, err := base64.StdEncoding.DecodeString(substr[2]); err != nil {
log.Printf("Unable to decode base64 '%s'", substr[2])
return
} else {
user_pwd = strings.Split(strings.TrimSpace(string(v)), ":")
}
auth := DockerRegistryCredsInfo{user_pwd[0], user_pwd[1]}
a.Auths[substr[1]] = auth
log.Printf("'%s' registry server credential added.", substr[1])
}
}
return
}