-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
137 lines (121 loc) · 2.94 KB
/
main.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
package main
import (
"bufio"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"log"
"os"
"strings"
"syscall"
"github.com/urfave/cli/v2"
"golang.org/x/crypto/ssh/terminal"
)
var version = "0.0.1"
func main() {
app := &cli.App{
Name: "docker auth generator",
Usage: "create a docker auth json",
Authors: []*cli.Author{{
Name: "Martin Radile",
Email: "[email protected]",
}},
Description: "the tool creates a docker auth json with the login and password base64 encoded",
Version: version,
Copyright: "Copyright (c) 2020 Martin Radile",
Action: func(c *cli.Context) error {
return Run(
c.String("registry"),
c.String("login"),
c.String("password"),
c.Bool("password-stdin"),
)
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "registry",
Aliases: []string{"r"},
Usage: "registry host with port",
EnvVars: []string{"REGISTRY"},
Required: true,
},
&cli.StringFlag{
Name: "login",
Aliases: []string{"l"},
Usage: "login",
EnvVars: []string{"LOGIN"},
Required: true,
},
&cli.StringFlag{
Name: "password",
Aliases: []string{"p"},
Usage: "provide a password as a flag",
EnvVars: []string{"PASSWORD"},
},
&cli.BoolFlag{
Name: "password-stdin",
Aliases: []string{"s"},
Usage: "read password from stdin",
EnvVars: []string{"PASSWORD_STDIN"},
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
type DockerAuth struct {
Auths map[string]map[string]string `json:"auths"`
}
func Run(registry, login, password string, pwStdin bool) error {
if pwStdin {
pw, err := ReadPasswordFromStdin()
if err != nil {
return err
}
password = pw
} else if password == "" {
pw, err := ReadPasswordFromTerminal()
if err != nil {
return err
}
password = pw
}
if password == "" {
return errors.New("password must not be empty")
}
auths := &DockerAuth{
Auths: make(map[string]map[string]string),
}
lp := fmt.Sprintf("%s:%s", login, password)
lp64 := base64.StdEncoding.EncodeToString([]byte(lp))
auths.Auths[registry] = map[string]string{"auth": lp64}
data, err := json.MarshalIndent(auths, "", " ")
if err != nil {
return fmt.Errorf("could not marshal to json: %w", err)
}
fmt.Println(string(data))
return nil
}
func ReadPasswordFromStdin() (string, error) {
reader := bufio.NewReader(os.Stdin)
bytePassword, _, err := reader.ReadLine()
if err != nil {
return "", fmt.Errorf("could not read password from stding: %w", err)
}
password := string(bytePassword)
password = strings.TrimSpace(password)
return password, nil
}
func ReadPasswordFromTerminal() (string, error) {
fmt.Println("Enter password:")
bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
return "", fmt.Errorf("could not read password from terminal: %w", err)
}
password := string(bytePassword)
password = strings.TrimSpace(password)
return password, nil
}