-
Notifications
You must be signed in to change notification settings - Fork 224
/
docker.go
180 lines (153 loc) · 4.19 KB
/
docker.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package auth
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/config"
"github.com/superfly/flyctl/iostreams"
)
func newDocker() *cobra.Command {
const (
long = `Adds registry.fly.io to the docker daemon's authenticated
registries. This allows you to push images directly to fly from
the docker cli.
`
short = "Authenticate docker"
)
return command.New("docker", short, long, runDocker,
command.RequireSession)
}
// ensureDockerConfigDir checks to see if the "${HOME}/.docker" directory exists,
// it creates the dir if it doesn't.
func ensureDockerConfigDir(home string) error {
dockerDir := filepath.Join(home, ".docker")
fi, err := os.Stat(dockerDir)
if err != nil {
if !os.IsNotExist(err) {
return err
}
// It needs to be readable by Docker, if it gets installed in the
// future.
if err := os.Mkdir(dockerDir, 0o755); err != nil {
return err
}
} else if !fi.IsDir() {
return errors.New("~/.docker is not a dir")
}
return nil
}
// addFlyAuthToDockerConfig adds the fly registry to the provided JSON object
// and returns the updated JSON.
//
// The config.json is structured as follows:
// {
// "auths": {
// "registry.fly.io": {
// "auth": "x:..."
// }
// }
// }
func addFlyAuthToDockerConfig(cfg *config.Config, configJSON []byte) ([]byte, error) {
var dockerConfig map[string]json.RawMessage
if len(configJSON) == 0 {
dockerConfig = make(map[string]json.RawMessage)
} else if err := json.Unmarshal(configJSON, &dockerConfig); err != nil {
return nil, err
}
var dockerAuthProviders map[string]json.RawMessage
if a, ok := dockerConfig["auths"]; ok {
if err := json.Unmarshal(a, &dockerAuthProviders); err != nil {
return nil, err
}
} else {
dockerAuthProviders = make(map[string]json.RawMessage)
}
var flyAuth map[string]interface{}
if a, ok := dockerAuthProviders[cfg.RegistryHost]; ok {
if err := json.Unmarshal(a, &flyAuth); err != nil {
return nil, err
}
} else {
flyAuth = make(map[string]interface{})
}
flyAuth["auth"] = base64.URLEncoding.EncodeToString([]byte("x:" + cfg.AccessToken))
b, err := json.Marshal(flyAuth)
if err != nil {
return nil, err
}
dockerAuthProviders[cfg.RegistryHost] = b
b, err = json.Marshal(dockerAuthProviders)
if err != nil {
return nil, err
}
dockerConfig["auths"] = b
return json.Marshal(dockerConfig)
}
// configureDockerJSON adds the fly registry to the docker config.json.
func configureDockerJSON(cfg *config.Config) error {
if runtime.GOOS == "windows" {
return errors.New("unsuppported")
}
home, err := os.UserHomeDir()
if err != nil {
return err
}
if err := ensureDockerConfigDir(home); err != nil {
return err
}
configPath := filepath.Join(home, ".docker", "config.json")
configJSON, err := os.ReadFile(configPath)
if err != nil && !os.IsNotExist(err) {
return err
}
updatedJSON, err := addFlyAuthToDockerConfig(cfg, configJSON)
if err != nil {
return err
}
// It needs to be readable by Docker, if it gets installed in the future.
return os.WriteFile(configPath, updatedJSON, 0o644)
}
func runDocker(ctx context.Context) (err error) {
cfg := config.FromContext(ctx)
binary, err := exec.LookPath("docker")
if err != nil {
// Try configuring the JSON directly.
if err := configureDockerJSON(cfg); err == nil {
return nil
}
return fmt.Errorf("docker cli not found - make sure it's installed and try again: %w", err)
}
host := cfg.RegistryHost
var out bytes.Buffer
cmd := exec.CommandContext(ctx, binary, "login", "--username=x", "--password-stdin", host)
cmd.Stdout = &out
cmd.Stderr = &out
var in io.WriteCloser
if in, err = cmd.StdinPipe(); err != nil {
return
}
go func() {
defer in.Close()
fmt.Fprint(in, cfg.AccessToken)
}()
if err = cmd.Start(); err != nil {
return
}
if err = cmd.Wait(); err != nil {
err = fmt.Errorf("failed authenticating with %s: %v", host, out.String())
return
}
io := iostreams.FromContext(ctx)
fmt.Fprintf(io.Out, "Authentication successful. You can now tag and push images to %s/{your-app}\n", host)
return
}