@@ -5,10 +5,15 @@ import (
5
5
"bridgr/internal/app/bridgr/assets"
6
6
"bytes"
7
7
"context"
8
+ "encoding/base64"
9
+ "encoding/json"
8
10
"io"
9
11
"io/ioutil"
12
+ "net/url"
10
13
"os"
14
+ "strings"
11
15
16
+ "github.com/docker/distribution/reference"
12
17
"github.com/docker/docker/api/types"
13
18
"github.com/docker/docker/api/types/container"
14
19
"github.com/docker/docker/client"
@@ -39,8 +44,25 @@ func cleanContainer(cli client.ContainerAPIClient, name string) error {
39
44
return cli .ContainerRemove (context .Background (), name , types.ContainerRemoveOptions {Force : true })
40
45
}
41
46
42
- func pullImage (cli client.ImageAPIClient , image string ) error {
43
- output , err := cli .ImagePull (context .Background (), image , types.ImagePullOptions {})
47
+ func pullImage (cli client.ImageAPIClient , image reference.Named ) error {
48
+ imgDomain := "https://" + reference .Domain (image ) // by putting scheme in front, it forces url.Parse to correctly identify the host portion
49
+ bridgr .Debugf ("Got image domain of %s" , imgDomain )
50
+ url , err := url .Parse (imgDomain )
51
+ bridgr .Debugf ("Parsed URL: %s" , url )
52
+ encodedAuth := ""
53
+ if err == nil {
54
+ username , password := credentials (url )
55
+ if username != "" && password != "" {
56
+ imgAuth := types.AuthConfig {
57
+ Username : username ,
58
+ Password : password ,
59
+ }
60
+ bridgr .Debugf ("Docker: Found credentials for %s" , url .Hostname ())
61
+ jsonAuth , _ := json .Marshal (imgAuth )
62
+ encodedAuth = base64 .URLEncoding .EncodeToString (jsonAuth )
63
+ }
64
+ }
65
+ output , err := cli .ImagePull (context .Background (), image .String (), types.ImagePullOptions {RegistryAuth : encodedAuth })
44
66
writer := ioutil .Discard
45
67
if err != nil {
46
68
return err
@@ -56,9 +78,10 @@ func pullImage(cli client.ImageAPIClient, image string) error {
56
78
func runContainer (name string , containerConfig * container.Config , hostConfig * container.HostConfig , script string ) error {
57
79
ctx := context .Background ()
58
80
cli , _ := client .NewClientWithOpts (client .FromEnv )
81
+ img , _ := reference .ParseNormalizedNamed (containerConfig .Image )
59
82
// log.Printf("%+v", cli)
60
83
_ = cleanContainer (cli , name )
61
- _ = pullImage (cli , "docker.io/" + containerConfig . Image )
84
+ _ = pullImage (cli , img )
62
85
63
86
resp , err := cli .ContainerCreate (ctx , containerConfig , hostConfig , nil , name )
64
87
if err != nil {
@@ -88,3 +111,25 @@ func runContainer(name string, containerConfig *container.Config, hostConfig *co
88
111
89
112
return nil
90
113
}
114
+
115
+ func credentials (url * url.URL ) (string , string ) {
116
+ basename := "BRIDGR_" + strings .ToUpper (strings .ReplaceAll (url .Hostname (), "." , "_" ))
117
+ uservar := basename + "_USER"
118
+ passwdvar := basename + "_PASS"
119
+ bridgr .Debugf ("Looking for env var: %s" , uservar )
120
+ if value , ok := os .LookupEnv (uservar ); ok {
121
+ return value , os .Getenv (passwdvar )
122
+ }
123
+ bridgr .Debugf ("Env Var %s was not found :(" , uservar )
124
+ return "" , ""
125
+ }
126
+
127
+ func credentialsConjoined (url * url.URL ) string {
128
+ u , p := credentials (url )
129
+ return u + ":" + p
130
+ }
131
+
132
+ func credentialsBase64 (url * url.URL ) string {
133
+ v := credentialsConjoined (url )
134
+ return base64 .StdEncoding .EncodeToString ([]byte (v ))
135
+ }
0 commit comments