Last active
July 14, 2023 22:19
-
-
Save ImDevinC/4dd930c7d175df0db462e074b1ab0a63 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This will check for cortex.yaml or cortex.yml in all JupiterOne repositories. | |
// Run with go run main.go <github_pat> | |
package main | |
import ( | |
"context" | |
"fmt" | |
"log" | |
"net/http" | |
"os" | |
"path/filepath" | |
"github.com/google/go-github/github" | |
"golang.org/x/oauth2" | |
) | |
func main() { | |
if len(os.Args) != 2 { | |
filename := filepath.Base(os.Args[0]) | |
log.Fatalf("Missing GitHub token input\nUsage:\n\t%s <token>\n", filename) | |
} | |
token := os.Args[1] | |
ctx := context.Background() | |
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token}) | |
tc := oauth2.NewClient(ctx, ts) | |
client := github.NewClient(tc) | |
// Validate credentials | |
_, _, err := client.Zen(ctx) | |
if err != nil { | |
log.Fatal(err) | |
} | |
page := 1 | |
names := []string{} | |
log.Println("Getting repositories...") | |
for { | |
repos, resp, err := client.Repositories.ListByOrg(ctx, "JupiterOne", &github.RepositoryListByOrgOptions{ListOptions: github.ListOptions{Page: page, PerPage: 100}}) | |
if err != nil { | |
log.Fatal(err) | |
} | |
for _, r := range repos { | |
if !*r.Archived { | |
names = append(names, *r.Name) | |
} | |
} | |
if resp.NextPage == 0 { | |
break | |
} | |
page++ | |
} | |
log.Println("Checking for cortex files...") | |
for _, n := range names { | |
yamlUrl := fmt.Sprintf("https://raw.githubusercontent.com/JupiterOne/%s/main/cortex.yaml", n) | |
ymlUrl := fmt.Sprintf("https://raw.githubusercontent.com/JupiterOne/%s/main/cortex.yml", n) | |
if doesRemoteFileExist(tc, yamlUrl) || doesRemoteFileExist(tc, ymlUrl) { | |
continue | |
} | |
log.Printf("Repository JupiterOne/%s does not have a cortex file\n", n) | |
} | |
} | |
func doesRemoteFileExist(client *http.Client, url string) bool { | |
resp, err := client.Head(url) | |
if err != nil { | |
log.Println(err) | |
} | |
if resp.StatusCode != 200 { | |
return false | |
} | |
return true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment