From 3568dc9efa0790770734c453db84ea32f4cd2a46 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Mon, 10 Apr 2017 14:39:10 +0100 Subject: [PATCH] Switch to yaml for our config data (#5) Env vars are getting unwieldy, and it's going to get worse. --- .gitignore | 1 + README.md | 16 +++---- rageshake.yaml | 10 ++++ src/github.com/matrix-org/rageshake/main.go | 52 +++++++++++++++++---- 4 files changed, 59 insertions(+), 20 deletions(-) create mode 100644 rageshake.yaml diff --git a/.gitignore b/.gitignore index 4d250f6..7b791ef 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /bin /bugs /pkg +/rageshake-local.yaml diff --git a/README.md b/README.md index 16aaea7..b7724b1 100644 --- a/README.md +++ b/README.md @@ -9,19 +9,15 @@ To run it, do: ``` go get github.com/constabulary/gb/... gb build -GITHUB_TOKEN= BUGS_USER= BUGS_PASS= ./bin/rageshake +./bin/rageshake ``` -where: +Optional parameters: - * `token` is a GitHub personal access token - (https://github.com/settings/tokens), which will be used to create a GitHub - issue for each report. It requires `public_repo` scope. If omitted, no - issues will be created. - * `user` and `password` are a username/password pair which will be required to - access the bug report listings at `/api/listing`, via HTTP basic auth. - If omitted, there will be *no* authentication on this access! - * `port` is the TCP port to listen on. + * `-config `: The path to a YAML config file; see [./rageshake.yaml] for + more information. + * `-listen
`: TCP network address to listen for HTTP requests + on. Example: `:9110`. ## HTTP endpoints diff --git a/rageshake.yaml b/rageshake.yaml new file mode 100644 index 0000000..bf15201 --- /dev/null +++ b/rageshake.yaml @@ -0,0 +1,10 @@ +# username/password pair which will be required to access the bug report +# listings at `/api/listing`, via HTTP basic auth. If omitted, there will be +# *no* authentication on this access! +listings_auth_user: alice +listings_auth_pass: secret + +# a GitHub personal access token (https://github.com/settings/tokens), which +# will be used to create a GitHub issue for each report. It requires +# `public_repo` scope. If omitted, no issues will be created. +github_token: secrettoken diff --git a/src/github.com/matrix-org/rageshake/main.go b/src/github.com/matrix-org/rageshake/main.go index d0af000..7bfab7f 100644 --- a/src/github.com/matrix-org/rageshake/main.go +++ b/src/github.com/matrix-org/rageshake/main.go @@ -19,14 +19,30 @@ package main import ( "context" "crypto/subtle" + "flag" "fmt" "github.com/google/go-github/github" "golang.org/x/oauth2" + "io/ioutil" "log" "net/http" "os" + + yaml "gopkg.in/yaml.v2" ) +var configPath = flag.String("config", "rageshake.yaml", "The path to the config file. For more information, see the config file in this repository.") +var bindAddr = flag.String("listen", ":9110", "The port to listen on.") + +type config struct { + // Username and password required to access the bug report listings + BugsUser string `yaml:"listings_auth_user"` + BugsPass string `yaml:"listings_auth_pass"` + + // A GitHub personal access token, to create a GitHub issue for each report. + GithubToken string `yaml:"github_token"` +} + func basicAuth(handler http.Handler, username, password, realm string) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { user, pass, ok := r.BasicAuth() // pull creds from the request @@ -44,16 +60,21 @@ func basicAuth(handler http.Handler, username, password, realm string) http.Hand } func main() { - ghToken := os.Getenv("GITHUB_TOKEN") + flag.Parse() + + cfg, err := loadConfig(*configPath) + if err != nil { + log.Fatalf("Invalid config file: %s", err) + } var ghClient *github.Client - if ghToken == "" { - fmt.Println("No GITHUB_TOKEN env var set. Reporting bugs to github is disabled.") + if cfg.GithubToken == "" { + fmt.Println("No github_token configured. Reporting bugs to github is disabled.") } else { ctx := context.Background() ts := oauth2.StaticTokenSource( - &oauth2.Token{AccessToken: ghToken}, + &oauth2.Token{AccessToken: cfg.GithubToken}, ) tc := oauth2.NewClient(ctx, ts) ghClient = github.NewClient(tc) @@ -69,17 +90,28 @@ func main() { fs := http.StripPrefix("/api/listing/", ls) // set auth if env vars exist - usr := os.Getenv("BUGS_USER") - pass := os.Getenv("BUGS_PASS") + usr := cfg.BugsUser + pass := cfg.BugsPass if usr == "" || pass == "" { - fmt.Println("BUGS_USER and BUGS_PASS env vars not found. No authentication is running for /api/listing") + fmt.Println("No listings_auth_user/pass configured. No authentication is running for /api/listing") } else { fs = basicAuth(fs, usr, pass, "Riot bug reports") } http.Handle("/api/listing/", fs) - port := os.Args[1] - log.Println("Listening on port", port) + log.Println("Listening on", *bindAddr) - log.Fatal(http.ListenAndServe(":"+port, nil)) + log.Fatal(http.ListenAndServe(*bindAddr, nil)) +} + +func loadConfig(configPath string) (*config, error) { + contents, err := ioutil.ReadFile(configPath) + if err != nil { + return nil, err + } + var cfg config + if err = yaml.Unmarshal(contents, &cfg); err != nil { + return nil, err + } + return &cfg, nil }