Small tool to fetch configured SSH keys for a Github Team
Note: The included authtoken was included as an example, not by mistake and has been revoked long ago
This commit is contained in:
parent
96eccd5807
commit
3516e25029
|
@ -0,0 +1,11 @@
|
|||
Use the Github API to fetch configured SSH keys for a team.
|
||||
|
||||
Build by
|
||||
```
|
||||
go get
|
||||
go build
|
||||
```
|
||||
Run
|
||||
```
|
||||
./fetch-keys --help
|
||||
```
|
|
@ -0,0 +1,3 @@
|
|||
package main
|
||||
|
||||
const AuthToken = "c90e06359175cfccc4db21f60c44b83d93ccbd28"
|
|
@ -0,0 +1,137 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/akamensky/argparse"
|
||||
"github.com/google/go-github/github" // with go modules disabled
|
||||
"golang.org/x/oauth2"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
parser := argparse.NewParser("sshkeys", "Fetch SSH keys for a Github team members")
|
||||
ghTeam := parser.String("t", "team", &argparse.Options{Required: false, Help: "Github Team name", Default: "NOC"})
|
||||
ghOrganization := parser.String("o", "org", &argparse.Options{Required: false, Help: "Github Organization name", Default: "OpenFest"})
|
||||
authToken := parser.String("a", "authtoken", &argparse.Options{Required: false, Help: "Github Auth token", Default: AuthToken})
|
||||
|
||||
// Parse input
|
||||
err := parser.Parse(os.Args)
|
||||
if err != nil {
|
||||
fmt.Print(parser.Usage(err))
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
ts := oauth2.StaticTokenSource(
|
||||
&oauth2.Token{AccessToken: *authToken},
|
||||
)
|
||||
tc := oauth2.NewClient(ctx, ts)
|
||||
|
||||
client := github.NewClient(tc)
|
||||
|
||||
var nocTeam *github.Team
|
||||
var nocMembers []*string
|
||||
|
||||
for nextPage := 0; ; {
|
||||
// list all teams for the specified org
|
||||
opt := &github.ListOptions{nextPage, 50}
|
||||
teams, rsp, err := client.Teams.ListTeams(ctx, *ghOrganization, opt)
|
||||
|
||||
if err != nil {
|
||||
fmt.Errorf("client.ListTeams error: %v", err)
|
||||
os.Exit(-1)
|
||||
}
|
||||
|
||||
if rsp == nil {
|
||||
fmt.Errorf("client.Repositories.List returned empty response: %v", err)
|
||||
}
|
||||
|
||||
for _, team := range teams {
|
||||
if *team.Name == *ghTeam {
|
||||
nocTeam = team
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if rsp.NextPage == 0 || nextPage == rsp.NextPage {
|
||||
break
|
||||
}
|
||||
nextPage = rsp.NextPage
|
||||
}
|
||||
|
||||
if nocTeam == nil {
|
||||
fmt.Errorf("NOC team not found in OpenFest")
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
for nextPage := 0; ; {
|
||||
// list all members for the given organization's team
|
||||
opt := &github.TeamListTeamMembersOptions{
|
||||
Role: "all",
|
||||
ListOptions: github.ListOptions{nextPage, 50},
|
||||
}
|
||||
|
||||
users, rsp, err := client.Teams.ListTeamMembers(context.Background(), *nocTeam.ID, opt)
|
||||
|
||||
if err != nil {
|
||||
fmt.Errorf("client.Teams.ListTeamMembers %v", err)
|
||||
os.Exit(-1)
|
||||
}
|
||||
|
||||
if rsp == nil {
|
||||
fmt.Errorf("client.Teams.ListTeamMembers: %v", err)
|
||||
}
|
||||
|
||||
for _, user := range users {
|
||||
nocMembers = append(nocMembers, user.Login)
|
||||
}
|
||||
|
||||
if rsp.NextPage == 0 || nextPage == rsp.NextPage {
|
||||
break
|
||||
}
|
||||
nextPage = rsp.NextPage
|
||||
}
|
||||
|
||||
for _, user := range nocMembers {
|
||||
fmt.Println("Fetching keys for ", *user)
|
||||
var sshKeys bytes.Buffer
|
||||
|
||||
for nextPage := 0; ; {
|
||||
|
||||
// list all teams an org for the current user
|
||||
opt := &github.ListOptions{nextPage, 50}
|
||||
keys, rsp, err := client.Users.ListKeys(ctx, *user, opt)
|
||||
|
||||
if err != nil {
|
||||
fmt.Errorf("client.Users.ListKeys error: %v", err)
|
||||
os.Exit(-1)
|
||||
}
|
||||
|
||||
if rsp == nil {
|
||||
fmt.Errorf("Users.ListKeyss returned empty response: %v", err)
|
||||
}
|
||||
|
||||
for _, key := range keys {
|
||||
fmt.Println(*key.Key)
|
||||
sshKeys.WriteString(*key.Key)
|
||||
sshKeys.WriteString("\n")
|
||||
}
|
||||
|
||||
if rsp.NextPage == 0 || nextPage == rsp.NextPage {
|
||||
break
|
||||
}
|
||||
nextPage = rsp.NextPage
|
||||
}
|
||||
|
||||
err := ioutil.WriteFile(*user+".key", sshKeys.Bytes(), 0444)
|
||||
if err != nil {
|
||||
fmt.Errorf(*user+".key error %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
os.Exit(0)
|
||||
}
|
Loading…
Reference in New Issue