This repository has been archived by the owner on Apr 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
382 lines (364 loc) · 11.1 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package main
import (
"crypto/md5"
"database/sql"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
_ "github.com/mattn/go-sqlite3"
homedir "github.com/mitchellh/go-homedir"
"github.com/schollz/progressbar"
"github.com/urfave/cli"
resty "gopkg.in/resty.v1"
)
// SSHKey is an SSH key reprensentation used for the API
type SSHKey struct {
ID float64
Key string
Comment string
User User
LastEdit float64
}
// User is a user representation used for the API
type User struct {
ID float64
Name string
Email string
Keys []SSHKey
}
// cfe panic in case of an error
func cfe(err error) bool {
if err != nil {
log.Panicln(err)
return false
}
return true
}
// fetchUser return User named `user` on the `server`
func fetchUser(user string, server string) (User, error) {
resp, err := resty.R().
SetHeader("Content-Type", "application/x-www-form-urlencoded").
SetHeader("Accept", "application/json").
Get(server + "/api/user/match/" + user)
cfe(err)
var f User
err = json.Unmarshal(resp.Body(), &f)
return f, err
}
// fetchUserSpecificKey returns User named `user` on the `server`, only with a specific key
func fetchUserSpecificKey(user string, key string, server string) (User, error) {
resp, err := resty.R().
SetHeader("Content-Type", "application/x-www-form-urlencoded").
SetHeader("Accept", "application/json").
// api path + user/email + filter to search + comment
Get(server + "/api/user/match/" + user + "?filter=" + key)
cfe(err)
var f User
err = json.Unmarshal(resp.Body(), &f)
return f, err
}
// CreateDirIfNotExist creates `dir` if it not exists
func CreateDirIfNotExist(dir string) {
if _, err := os.Stat(dir); os.IsNotExist(err) {
err = os.MkdirAll(dir, 0755)
cfe(err)
}
}
func initFileDB(storagepath string, keyfilepath string) (*sql.DB, error) {
var dbpath string
if os.Getenv("AKMEY_STORAGE") != "" {
fmt.Println("Warning : Databases path is manually configured, this is not recommended.")
storagepath = os.Getenv("AKMEY_STORAGE")
}
CreateDirIfNotExist(storagepath)
fullfilepath, err := filepath.Abs(keyfilepath)
hash := md5.Sum([]byte(fullfilepath))
dbpath = storagepath + "/keys_" + hex.EncodeToString(hash[:]) + ".db"
db, err := sql.Open("sqlite3", "file:"+dbpath+"?cache=shared&mode=rwc")
cfe(err)
sqlStmt := `
create table if not exists users (id integer not null, name text, email text);
create table if not exists keys (id integer not null, comment text, value text, user_id integer not null);
`
_, err = db.Exec(sqlStmt)
return db, err
}
func main() {
var server string
var dest string
re := regexp.MustCompile("#-- Akmey START --\n((?:.|\n)+)\n#-- Akmey STOP --")
defaultdest, err := homedir.Expand("~/.ssh/authorized_keys")
cfe(err)
storage, err := homedir.Expand("~/.akmey")
cfe(err)
defaultserv := "https://akmey.leonekmi.fr"
app := cli.NewApp()
app.Name = "akmey"
app.Usage = "Add/Remove SSH keys to grant access to your friends, coworkers, etc..."
app.Version = "0.1.8-alpha"
app.Copyright = "The Unlicense"
app.Author = "Akmey contributors"
app.Email = "[email protected]"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "dest, d",
Value: defaultdest,
Usage: "Where Akmey should act (your authorized_keys file)",
Destination: &dest,
},
cli.StringFlag{
Name: "server, s",
Value: defaultserv,
Usage: "Specify a custom Akmey server here",
Destination: &server,
},
}
app.Commands = []cli.Command{
{
Name: "install",
Aliases: []string{"i", "get", "add"},
Usage: "Install someone's key(s), sepcifying its e-mail or its username",
Action: func(c *cli.Context) error {
// use "akmey install user key" instead of user --key=, way simplier
// we can't just homedir.Expand("~/.ssh/authorized_e=keys") because it will fail if the file doesn't exist, so we basically just get user's home directory and add "/.ssh" at it
home, err := homedir.Expand("~/")
sshfolder := home + "/.ssh"
_ = os.Mkdir(sshfolder, 755) // create the dir (w/ correct permissions) and ignores errors, according to stackoverflow. It's not that good but hey, it works ¯\_(ツ)_/¯
keyfile := sshfolder + "/authorized_keys"
os.OpenFile(keyfile, os.O_RDONLY|os.O_CREATE, 0755) // create the file (w/ corrects permissions) if it doesn't already exist, a bit better than for the ssh dir
db, err := initFileDB(storage, dest)
defer db.Close()
tx, err := db.Begin()
cfe(err)
checkstmt, err := tx.Prepare("select name from users where email = ? or name = ?")
cfe(err)
var check string
err = checkstmt.QueryRow(c.Args().First(), c.Args().First()).Scan(&check)
if check != "" {
fmt.Println("This user is already installed.")
os.Exit(0)
}
err = nil
stmt, err := tx.Prepare("insert into users(id, name, email) values(?, ?, ?)")
cfe(err)
// id = key id on server's side, value = the key itself, comment = key name, userid = user's id, btw the uid is not working rn
stmt2, err := tx.Prepare("insert into keys(id, value, comment, user_id) values(?, ?, ?, ?)")
cfe(err)
defer checkstmt.Close()
defer stmt.Close()
defer stmt2.Close()
bar := progressbar.New(3)
var tobeinserted string
// Step 1 : fetch the user
// let's verify if a key has been wanted
if c.Args().Get(1) != "" {
user, err := fetchUserSpecificKey(c.Args().First(), c.Args().Get(1), server)
cfe(err)
for _, key := range user.Keys {
stmt2.Exec(key.ID, key.Key, key.Comment, user.ID)
tobeinserted += key.Key + " " + key.Comment + "\n"
}
stmt.Exec(user.ID, user.Name, user.Email)
} else {
user, err := fetchUser(c.Args().First(), server)
cfe(err)
for _, key := range user.Keys {
stmt2.Exec(key.ID, key.Key, key.Comment, user.ID)
tobeinserted += key.Key + " " + key.Comment + "\n"
}
stmt.Exec(user.ID, user.Name, user.Email)
}
bar.Add(1)
//fmt.Println(user)
// Step 2 : Fetch the keys in a beautiful string
if tobeinserted == "" {
fmt.Println("\nThis user does not exist or doesn't have keys registered.")
os.Exit(1)
}
bar.Add(1)
dat, err := ioutil.ReadFile(dest)
cfe(err)
match := re.FindStringSubmatch(string(dat))
if match == nil {
tobeinserted = "\n#-- Akmey START --\n" + tobeinserted
tobeinserted += "#-- Akmey STOP --\n"
f, err := os.OpenFile(dest, os.O_APPEND|os.O_WRONLY, 0600)
cfe(err)
defer f.Close()
_, err = f.WriteString(tobeinserted)
cfe(err)
} else {
tobeinserted = match[1] + tobeinserted
newContent := strings.Replace(string(dat), match[1], tobeinserted, -1)
err = ioutil.WriteFile(dest, []byte(newContent), 0)
cfe(err)
}
tx.Commit()
bar.Add(1)
fmt.Println("\n")
return nil
},
},
{
Name: "uninstall",
Aliases: []string{"u", "r", "remove"},
Usage: "Uninstall someone's key(s), specifying his e-mail or his username",
Action: func(c *cli.Context) error {
db, err := initFileDB(storage, dest)
defer db.Close()
tx, err := db.Begin()
cfe(err)
checkstmt, err := tx.Prepare("select id from users where email = ? or name = ? collate nocase")
cfe(err)
var check string
err = checkstmt.QueryRow(c.Args().First(), c.Args().First()).Scan(&check)
if check == "" {
fmt.Println("This user is not installed.")
os.Exit(0)
}
err = nil
stmt, err := tx.Prepare("delete from users where email = ? or name = ?")
cfe(err)
stmt2, err := tx.Prepare("delete from keys where value = ?")
cfe(err)
stmt3, err := tx.Prepare("select * from keys where user_id = ?")
cfe(err)
defer checkstmt.Close()
defer stmt.Close()
defer stmt2.Close()
defer stmt3.Close()
bar := progressbar.New(4)
// Step 1 : Fetch installed keys
rows, err := stmt3.Query(check)
cfe(err)
defer rows.Close()
toberemoved := map[int]string{}
bar.Add(1)
//fmt.Println(user)
// Step 2 : Parse the keys in a beautiful map
for rows.Next() {
var id int
var value string
var comment string
err = rows.Scan(&id, &value, &comment)
stmt2.Exec(value)
toberemoved[id] = "\n" + value + " " + comment
//tobeinserted += key.Key + " " + key.Comment + "\n"
}
err = rows.Err()
cfe(err)
bar.Add(1)
if len(toberemoved) == 0 {
fmt.Println("\nThis user does not exist or doesn't have keys registered.")
os.Exit(1)
}
stmt.Exec(c.Args().First(), c.Args().First())
bar.Add(1)
dat, err := ioutil.ReadFile(dest)
newContent := ""
cfe(err)
match := re.FindStringSubmatch(string(dat))
if match == nil {
fmt.Println("Akmey is not present in this file")
os.Exit(0)
}
for _, torm := range toberemoved {
if newContent == "" {
newContent = strings.Replace(string(dat), match[1], torm, -1)
} else {
newContent = strings.Replace(newContent, match[1], torm, -1)
}
}
err = ioutil.WriteFile(dest, []byte(newContent), 0)
cfe(err)
tx.Commit()
bar.Add(1)
fmt.Println("\n")
return nil
},
},
{
Name: "reset",
Aliases: []string{"u-all", "remove-all"},
Usage: "Uninstall ALL keys (these from Akmey only)",
Action: func(c *cli.Context) error {
db, err := initFileDB(storage, dest)
defer db.Close()
tx, err := db.Begin()
cfe(err)
stmt, err := tx.Prepare("delete from users")
cfe(err)
stmt2, err := tx.Prepare("delete from keys")
cfe(err)
stmt3, err := tx.Prepare("select * from keys")
cfe(err)
defer stmt.Close()
defer stmt2.Close()
defer stmt3.Close()
bar := progressbar.New(4)
// Step 1 : Fetch installed keys
rows, err := stmt3.Query()
cfe(err)
defer rows.Close()
toberemoved := map[int]string{}
bar.Add(1)
//fmt.Println(user)
// Step 2 : Parse the keys in a beautiful map
for rows.Next() {
var id int
var value string
var comment string
err = rows.Scan(&id, &value, &comment)
toberemoved[id] = "\n" + value + " " + comment
//tobeinserted += key.Key + " " + key.Comment + "\n"
}
err = rows.Err()
stmt2.Exec()
cfe(err)
bar.Add(1)
if len(toberemoved) == 0 {
fmt.Println("\nThere is no keys installed by Akmey here.")
os.Exit(1)
}
stmt.Exec()
bar.Add(1)
dat, err := ioutil.ReadFile(dest)
newContent := ""
cfe(err)
match := re.FindStringSubmatch(string(dat))
if match == nil {
fmt.Println("Akmey is not present in this file")
os.Exit(0)
}
for _, torm := range toberemoved {
if newContent == "" {
newContent = strings.Replace(string(dat), match[1], torm, -1)
} else {
newContent = strings.Replace(newContent, match[1], torm, -1)
}
}
err = ioutil.WriteFile(dest, []byte(newContent), 0)
cfe(err)
tx.Commit()
bar.Add(1)
println("\n")
return nil
},
},
}
sort.Sort(cli.FlagsByName(app.Flags))
app.Action = func(c *cli.Context) error {
fmt.Println("It looks like you've entered an unknown command, try `akmey help`.")
return nil
}
cfe(err)
apperr := app.Run(os.Args)
cfe(apperr)
}