Switch to yaml for our config data (#5)

Env vars are getting unwieldy, and it's going to get worse.
This commit is contained in:
Richard van der Hoff 2017-04-10 14:39:10 +01:00 committed by GitHub
parent 4af3e2a302
commit 3568dc9efa
4 changed files with 59 additions and 20 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
/bin /bin
/bugs /bugs
/pkg /pkg
/rageshake-local.yaml

View file

@ -9,19 +9,15 @@ To run it, do:
``` ```
go get github.com/constabulary/gb/... go get github.com/constabulary/gb/...
gb build gb build
GITHUB_TOKEN=<token> BUGS_USER=<user> BUGS_PASS=<password> ./bin/rageshake <port> ./bin/rageshake
``` ```
where: Optional parameters:
* `token` is a GitHub personal access token * `-config <path>`: The path to a YAML config file; see [./rageshake.yaml] for
(https://github.com/settings/tokens), which will be used to create a GitHub more information.
issue for each report. It requires `public_repo` scope. If omitted, no * `-listen <address>`: TCP network address to listen for HTTP requests
issues will be created. on. Example: `:9110`.
* `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.
## HTTP endpoints ## HTTP endpoints

10
rageshake.yaml Normal file
View file

@ -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

View file

@ -19,14 +19,30 @@ package main
import ( import (
"context" "context"
"crypto/subtle" "crypto/subtle"
"flag"
"fmt" "fmt"
"github.com/google/go-github/github" "github.com/google/go-github/github"
"golang.org/x/oauth2" "golang.org/x/oauth2"
"io/ioutil"
"log" "log"
"net/http" "net/http"
"os" "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 { func basicAuth(handler http.Handler, username, password, realm string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user, pass, ok := r.BasicAuth() // pull creds from the 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() { 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 var ghClient *github.Client
if ghToken == "" { if cfg.GithubToken == "" {
fmt.Println("No GITHUB_TOKEN env var set. Reporting bugs to github is disabled.") fmt.Println("No github_token configured. Reporting bugs to github is disabled.")
} else { } else {
ctx := context.Background() ctx := context.Background()
ts := oauth2.StaticTokenSource( ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: ghToken}, &oauth2.Token{AccessToken: cfg.GithubToken},
) )
tc := oauth2.NewClient(ctx, ts) tc := oauth2.NewClient(ctx, ts)
ghClient = github.NewClient(tc) ghClient = github.NewClient(tc)
@ -69,17 +90,28 @@ func main() {
fs := http.StripPrefix("/api/listing/", ls) fs := http.StripPrefix("/api/listing/", ls)
// set auth if env vars exist // set auth if env vars exist
usr := os.Getenv("BUGS_USER") usr := cfg.BugsUser
pass := os.Getenv("BUGS_PASS") pass := cfg.BugsPass
if usr == "" || pass == "" { 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 { } else {
fs = basicAuth(fs, usr, pass, "Riot bug reports") fs = basicAuth(fs, usr, pass, "Riot bug reports")
} }
http.Handle("/api/listing/", fs) http.Handle("/api/listing/", fs)
port := os.Args[1] log.Println("Listening on", *bindAddr)
log.Println("Listening on port", port)
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
} }