-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmain.go
192 lines (164 loc) · 4.57 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
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
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"context"
"errors"
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"os/signal"
"os/user"
"path/filepath"
"strings"
"syscall"
"time"
"github.com/genuinetools/pkg/cli"
"github.com/genuinetools/sshb0t/version"
"github.com/sirupsen/logrus"
)
const (
defaultKeyURI string = "%s/%s.keys"
defaultSSHAuthorizedKeysFile string = ".ssh/authorized_keys"
)
var (
home string
authorizedKeysFile string
enturl string
users stringSlice
interval time.Duration
once bool
debug bool
)
// stringSlice is a slice of strings
type stringSlice []string
// implement the flag interface for stringSlice
func (s *stringSlice) String() string {
return fmt.Sprintf("%s", *s)
}
func (s *stringSlice) Set(value string) error {
*s = append(*s, value)
return nil
}
func main() {
var err error
// get the home directory
home, err = getHomeDir()
if err != nil {
logrus.Fatalf("getHomeDir failed: %v", err)
}
// Create a new cli program.
p := cli.NewProgram()
p.Name = "sshb0t"
p.Description = "A bot for keeping your ssh authorized_keys up to date with user's GitHub keys"
// Set the GitCommit and Version.
p.GitCommit = version.GITCOMMIT
p.Version = version.VERSION
// Setup the global flags.
p.FlagSet = flag.NewFlagSet("global", flag.ExitOnError)
p.FlagSet.StringVar(&authorizedKeysFile, "keyfile", filepath.Join(home, defaultSSHAuthorizedKeysFile), "file to update the authorized_keys")
p.FlagSet.StringVar(&enturl, "url", "https://github.com", "GitHub Enterprise URL")
p.FlagSet.Var(&users, "user", "GitHub usernames for which to fetch keys")
p.FlagSet.DurationVar(&interval, "interval", 30*time.Second, "update interval (ex. 5ms, 10s, 1m, 3h)")
p.FlagSet.BoolVar(&once, "once", false, "run once and exit, do not run as a daemon")
p.FlagSet.BoolVar(&debug, "d", false, "enable debug logging")
// Set the before function.
p.Before = func(ctx context.Context) error {
// Set the log level.
if debug {
logrus.SetLevel(logrus.DebugLevel)
}
if len(users) < 1 {
return errors.New("you must pass at least one username")
}
if len(authorizedKeysFile) < 1 {
return errors.New("you must pass a file to save the authorized keys into or use the default")
}
return nil
}
// Set the main program action.
p.Action = func(ctx context.Context, args []string) error {
ticker := time.NewTicker(interval)
// On ^C, or SIGTERM handle exit.
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
signal.Notify(c, syscall.SIGTERM)
go func() {
for sig := range c {
ticker.Stop()
logrus.Infof("Received %s, exiting.", sig.String())
os.Exit(0)
}
}()
// If the user passed the once flag, just do the run once and exit.
if once {
run()
os.Exit(0)
}
logrus.Infof("Starting bot to update %s every %s for users %s", authorizedKeysFile, interval, strings.Join(users, ", "))
for range ticker.C {
run()
}
return nil
}
// Run our program.
p.Run()
}
func run() {
// fetch the keys for each user
var (
keys string
successes int
)
for _, user := range users {
// fetch the url
uri := fmt.Sprintf(defaultKeyURI, enturl, user)
baseURL, err := url.Parse(uri)
if err != nil {
logrus.Fatalf("parsing url %s failed: %v", uri, err)
}
uri = baseURL.String()
logrus.Debugf("Fetching keys for user %s from %s", user, uri)
resp, err := http.Get(uri)
if err != nil {
logrus.Warnf("Fetching keys for user %s from %s failed: %v", user, uri, err)
continue
}
// make sure we got status 200
if http.StatusOK != resp.StatusCode {
logrus.Warnf("Expected status code 200 from %s but got %d for user %s", uri, resp.StatusCode, user)
continue
}
// read the body
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
logrus.Fatalf("Reading response body from %s for user %s failed: %v", uri, user, err)
continue
}
// append to keys variable with a new line
keys += string(b)
successes++
}
if successes < 1 {
logrus.Warnf("Unable to fetch any user keys, not updating authorized keys file")
return
}
// update the authorized key file
logrus.Infof("Updating authorized key file %s with keys from %s", authorizedKeysFile, strings.Join(users, ", "))
if err := ioutil.WriteFile(authorizedKeysFile, []byte(keys), 0600); err != nil {
logrus.Fatalf("Writing to file %s failed: %v", authorizedKeysFile, err)
}
logrus.Info("Successfully updated keys")
}
func getHomeDir() (string, error) {
home := os.Getenv(homeKey)
if home != "" {
return home, nil
}
u, err := user.Current()
if err != nil {
return "", err
}
return u.HomeDir, nil
}