From ae0756bf5c269d6dc74c9ca68429877bf93371b2 Mon Sep 17 00:00:00 2001 From: Shteryana Shopova Date: Sat, 20 Jul 2019 23:26:13 +0300 Subject: [PATCH] Fetch also configured PGP keys if requested --- access/fetch-keys/main.go | 42 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/access/fetch-keys/main.go b/access/fetch-keys/main.go index b4a6d33..b3384fe 100644 --- a/access/fetch-keys/main.go +++ b/access/fetch-keys/main.go @@ -25,8 +25,9 @@ func main() { parser := argparse.NewParser("sshkeys", "Fetch SSH keys for a Github team members") authToken := parser.String("a", "authtoken", &argparse.Options{Required: false, Help: "Github Auth token", Default: AuthToken}) keysDir := parser.String("d", "directory", &argparse.Options{Required: false, Help: "Path where to store the key files", Default: "./"}) - quiet := parser.Flag("q", "quiet", &argparse.Options{Required: false, Help: "Skip output to stdout", Default: false}) ghOrganization := parser.String("o", "org", &argparse.Options{Required: false, Help: "Github Organization name", Default: "OpenFest"}) + fetchPgp := parser.Flag("p", "pgp-keys", &argparse.Options{Required: false, Help: "Fetch configured PGP key ids", Default: false}) + quiet := parser.Flag("q", "quiet", &argparse.Options{Required: false, Help: "Skip output to stdout", Default: false}) ghTeam := parser.String("t", "team", &argparse.Options{Required: false, Help: "Github Team name, 'all' for all members of the organization", Default: "NOC"}) // Parse input @@ -62,6 +63,7 @@ func main() { fmt.Println("Fetching keys for ", *user) } var sshKeys bytes.Buffer + var pgpKeys bytes.Buffer for nextPage := 0; ; { @@ -99,6 +101,44 @@ func main() { if err != nil { fmt.Println(*user+".key error ", err) } + + if *fetchPgp == true { + for nextPage := 0; ; { + + // list all teams an org for the current user + opt := &github.ListOptions{nextPage, 50} + keys, rsp, err := client.Users.ListGPGKeys(ctx, *user, opt) + + if err != nil { + fmt.Println("client.Users.ListGPGKeys error: ", err) + os.Exit(-1) + } + + if rsp == nil { + fmt.Println("Users.ListGPGKeys returned empty response: ", err) + } + + for _, key := range keys { + if *quiet == false { + fmt.Println(*key.KeyID) + } + pgpKeys.WriteString(*key.KeyID) + pgpKeys.WriteString("\n") + } + + if rsp.NextPage == 0 || nextPage == rsp.NextPage { + break + } + nextPage = rsp.NextPage + } + if *quiet == false { + fmt.Println("Writing to", *keysDir + "/" + *user+".gpg") + } + err = ioutil.WriteFile(*keysDir + "/" + *user+".gpg", pgpKeys.Bytes(), 0444) + if err != nil { + fmt.Println(*user+".gpg error ", err) + } + } } os.Exit(0)