This repository has been archived by the owner on Oct 18, 2022. It is now read-only.
/
docker-registries.go
124 lines (105 loc) · 3.21 KB
/
docker-registries.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package build
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"os"
"strings"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/velocity-ci/velocity/backend/pkg/exec"
"github.com/velocity-ci/velocity/backend/pkg/velocity/logging"
"go.uber.org/zap"
)
type TaskDocker struct {
Registries []DockerRegistry `json:"registries"`
}
type DockerRegistry struct {
Address string `json:"address"`
Use string `json:"use"`
Arguments map[string]string `json:"arguments"`
AuthorizationToken string `json:"authToken"`
}
func dockerLogin(registry DockerRegistry, writer io.Writer, task *Task) (r DockerRegistry, _ error) {
type registryAuthConfig struct {
Username string `json:"username"`
Password string `json:"password"`
ServerAddress string `json:"serverAddress"`
Error string `json:"error"`
State string `json:"state"`
}
bin, err := getBinary(task.ProjectRoot, registry.Use, writer)
if err != nil {
return r, err
}
extraEnv := []string{}
for k, v := range registry.Arguments {
for _, pV := range task.parameters {
v = strings.Replace(v, fmt.Sprintf("${%s}", pV.Name), pV.Value, -1)
k = strings.Replace(k, fmt.Sprintf("${%s}", pV.Name), pV.Value, -1)
}
extraEnv = append(extraEnv, fmt.Sprintf("%s=%s", k, v))
}
s := exec.Run([]string{bin}, "", append(os.Environ(), extraEnv...), BlankWriter{})
if s.Error != nil {
return r, err
}
var dOutput registryAuthConfig
json.Unmarshal([]byte(s.Stdout[0]), &dOutput)
if dOutput.State != "success" {
return r, fmt.Errorf("registry auth error: %s", dOutput.Error)
}
cli, _ := client.NewEnvClient()
ctx := context.Background()
_, err = cli.RegistryLogin(ctx, types.AuthConfig{
Username: dOutput.Username,
Password: dOutput.Password,
ServerAddress: dOutput.ServerAddress,
})
if err != nil {
return r, err
}
authConfig := types.AuthConfig{
Username: dOutput.Username,
Password: dOutput.Password,
}
encodedJSON, err := json.Marshal(authConfig)
if err != nil {
return r, err
}
registry.AuthorizationToken = base64.URLEncoding.EncodeToString(encodedJSON)
registry.Address = dOutput.ServerAddress
return registry, nil
}
type dockerLoginOutput struct {
State string `json:"state"`
Error string `json:"error"`
AuthorizationToken string `json:"authToken"`
Address string `json:"address"`
}
func GetAuthConfigsMap(dockerRegistries []DockerRegistry) map[string]types.AuthConfig {
authConfigs := map[string]types.AuthConfig{}
for _, r := range dockerRegistries {
jsonAuthConfig, err := base64.URLEncoding.DecodeString(r.AuthorizationToken)
if err != nil {
logging.GetLogger().Error(
"could not decode registry auth config",
zap.String("err", err.Error()),
zap.String("registry", r.Address),
)
}
var authConfig types.AuthConfig
err = json.Unmarshal(jsonAuthConfig, &authConfig)
authConfigs[r.Address] = authConfig
}
return authConfigs
}
func GetAddressAuthTokensMap(dockerRegistries []DockerRegistry) (r map[string]string) {
r = map[string]string{}
for _, dR := range dockerRegistries {
r[dR.Address] = dR.AuthorizationToken
}
return r
}