2017-04-04 21:29:18 +05:30
|
|
|
/*
|
|
|
|
Copyright 2017 Vector Creations Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-04-04 15:55:00 +05:30
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-04-07 20:13:19 +05:30
|
|
|
"context"
|
2017-04-04 15:55:00 +05:30
|
|
|
"crypto/subtle"
|
2017-04-10 19:09:10 +05:30
|
|
|
"flag"
|
2017-04-04 15:55:00 +05:30
|
|
|
"fmt"
|
2017-04-10 19:09:10 +05:30
|
|
|
"io/ioutil"
|
2017-04-04 15:55:00 +05:30
|
|
|
"log"
|
2017-04-12 19:36:40 +05:30
|
|
|
"net"
|
2017-04-04 15:55:00 +05:30
|
|
|
"net/http"
|
|
|
|
"os"
|
2017-04-12 19:36:40 +05:30
|
|
|
"strings"
|
|
|
|
"time"
|
2017-04-10 19:09:10 +05:30
|
|
|
|
2020-09-16 14:55:56 +05:30
|
|
|
"github.com/google/go-github/github"
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
|
2017-04-10 19:09:10 +05:30
|
|
|
yaml "gopkg.in/yaml.v2"
|
2017-04-04 15:55:00 +05:30
|
|
|
)
|
|
|
|
|
2017-04-10 19:09:10 +05:30
|
|
|
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"`
|
|
|
|
|
2017-04-12 19:36:40 +05:30
|
|
|
// External URI to /api
|
|
|
|
APIPrefix string `yaml:"api_prefix"`
|
|
|
|
|
2017-04-10 19:09:10 +05:30
|
|
|
// A GitHub personal access token, to create a GitHub issue for each report.
|
|
|
|
GithubToken string `yaml:"github_token"`
|
2017-04-12 19:36:40 +05:30
|
|
|
|
|
|
|
GithubProjectMappings map[string]string `yaml:"github_project_mappings"`
|
2019-03-07 19:12:08 +05:30
|
|
|
|
|
|
|
SlackWebhookURL string `yaml:"slack_webhook_url"`
|
2020-09-16 14:55:56 +05:30
|
|
|
|
|
|
|
EmailAddresses []string `yaml:"email_addresses"`
|
|
|
|
|
|
|
|
EmailFrom string `yaml:"email_from"`
|
|
|
|
|
|
|
|
SMTPServer string `yaml:"smtp_server"`
|
|
|
|
|
|
|
|
SMTPUsername string `yaml:"smtp_username"`
|
|
|
|
|
|
|
|
SMTPPassword string `yaml:"smtp_password"`
|
2017-04-10 19:09:10 +05:30
|
|
|
}
|
|
|
|
|
2017-04-04 15:55:00 +05:30
|
|
|
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
|
|
|
|
|
|
|
|
// check user and pass securely
|
|
|
|
if !ok || subtle.ConstantTimeCompare([]byte(user), []byte(username)) != 1 || subtle.ConstantTimeCompare([]byte(pass), []byte(password)) != 1 {
|
|
|
|
w.Header().Set("WWW-Authenticate", `Basic realm="`+realm+`"`)
|
|
|
|
w.WriteHeader(401)
|
|
|
|
w.Write([]byte("Unauthorised.\n"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
handler.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2017-04-10 19:09:10 +05:30
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
cfg, err := loadConfig(*configPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Invalid config file: %s", err)
|
|
|
|
}
|
2017-04-07 20:13:19 +05:30
|
|
|
|
|
|
|
var ghClient *github.Client
|
|
|
|
|
2017-04-10 19:09:10 +05:30
|
|
|
if cfg.GithubToken == "" {
|
|
|
|
fmt.Println("No github_token configured. Reporting bugs to github is disabled.")
|
2017-04-07 20:13:19 +05:30
|
|
|
} else {
|
|
|
|
ctx := context.Background()
|
|
|
|
ts := oauth2.StaticTokenSource(
|
2017-04-10 19:09:10 +05:30
|
|
|
&oauth2.Token{AccessToken: cfg.GithubToken},
|
2017-04-07 20:13:19 +05:30
|
|
|
)
|
|
|
|
tc := oauth2.NewClient(ctx, ts)
|
2017-04-12 19:36:40 +05:30
|
|
|
tc.Timeout = time.Duration(5) * time.Minute
|
2017-04-07 20:13:19 +05:30
|
|
|
ghClient = github.NewClient(tc)
|
|
|
|
}
|
|
|
|
|
2019-03-07 19:12:08 +05:30
|
|
|
var slack *slackClient
|
|
|
|
|
|
|
|
if cfg.SlackWebhookURL == "" {
|
|
|
|
fmt.Println("No slack_webhook_url configured. Reporting bugs to slack is disabled.")
|
|
|
|
} else {
|
2020-06-04 17:22:39 +05:30
|
|
|
slack = newSlackClient(cfg.SlackWebhookURL)
|
2019-03-07 19:12:08 +05:30
|
|
|
}
|
|
|
|
|
2020-09-16 14:55:56 +05:30
|
|
|
if len(cfg.EmailAddresses) > 0 && cfg.SMTPServer == "" {
|
|
|
|
log.Fatal("Email address(es) specified but no smtp_server configured. Wrong configuration, aborting...")
|
|
|
|
}
|
|
|
|
|
2017-04-12 19:36:40 +05:30
|
|
|
apiPrefix := cfg.APIPrefix
|
|
|
|
if apiPrefix == "" {
|
|
|
|
_, port, err := net.SplitHostPort(*bindAddr)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
apiPrefix = fmt.Sprintf("http://localhost:%s/api", port)
|
|
|
|
} else {
|
|
|
|
// remove trailing /
|
|
|
|
apiPrefix = strings.TrimRight(apiPrefix, "/")
|
|
|
|
}
|
|
|
|
log.Printf("Using %s/listing as public URI", apiPrefix)
|
|
|
|
|
2020-09-16 14:55:56 +05:30
|
|
|
http.Handle("/api/submit", &submitServer{ghClient, apiPrefix, slack, cfg})
|
2017-04-04 15:55:00 +05:30
|
|
|
|
|
|
|
// Make sure bugs directory exists
|
|
|
|
_ = os.Mkdir("bugs", os.ModePerm)
|
|
|
|
|
|
|
|
// serve files under "bugs"
|
2017-04-06 16:50:07 +05:30
|
|
|
ls := &logServer{"bugs"}
|
|
|
|
fs := http.StripPrefix("/api/listing/", ls)
|
2017-04-04 15:55:00 +05:30
|
|
|
|
|
|
|
// set auth if env vars exist
|
2017-04-10 19:09:10 +05:30
|
|
|
usr := cfg.BugsUser
|
|
|
|
pass := cfg.BugsPass
|
2017-04-04 15:55:00 +05:30
|
|
|
if usr == "" || pass == "" {
|
2017-04-10 19:09:10 +05:30
|
|
|
fmt.Println("No listings_auth_user/pass configured. No authentication is running for /api/listing")
|
2017-04-04 15:55:00 +05:30
|
|
|
} else {
|
|
|
|
fs = basicAuth(fs, usr, pass, "Riot bug reports")
|
|
|
|
}
|
|
|
|
http.Handle("/api/listing/", fs)
|
|
|
|
|
2017-04-10 19:09:10 +05:30
|
|
|
log.Println("Listening on", *bindAddr)
|
|
|
|
|
|
|
|
log.Fatal(http.ListenAndServe(*bindAddr, nil))
|
|
|
|
}
|
2017-04-06 16:50:07 +05:30
|
|
|
|
2017-04-10 19:09:10 +05:30
|
|
|
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
|
2017-04-04 15:55:00 +05:30
|
|
|
}
|