Skip to content
This repository has been archived by the owner on Feb 27, 2020. It is now read-only.

Commit

Permalink
plugins/relengapi: proxy all services to new url (#397)
Browse files Browse the repository at this point in the history
plugins/relengapi: proxy all services to new url
  • Loading branch information
garbas authored and walac committed Jul 3, 2018
1 parent d3a0d81 commit 4c9135b
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 26 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ Installing From Source
----------------------

1. [Install go 1.10](https://golang.org/doc/install) or higher
2. `go get -u -t -d github.com/taskclustertaskcluster-worker/...`
3. `cd "${GOPATH}/src/github.com/taskcluster-worker"`
2. `go get -u -t -d github.com/taskcluster/taskcluster-worker/...`
3. `cd "${GOPATH}/src/github.com/taskcluster/taskcluster-worker"`
4. `go get -u github.com/kardianos/govendor`
5. `govendor sync`
6. `make rebuild`
Expand Down
4 changes: 2 additions & 2 deletions examples/docker-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ config:
success: {}
watchdog: {}
relengapi:
host: "https://api.pub.build.mozilla.org/"
domain: "mozilla-releng.net"
token: "<token>"
temporaryFolder: /tmp/tc-worker-tmp
webHookServer:
Expand All @@ -49,4 +49,4 @@ config:
provisionerId: "provisionerId"
workerType: "workerType"
workerGroup: "workerGroup"
workerId: "workerId"
workerId: "workerId"
10 changes: 5 additions & 5 deletions plugins/relengapi/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
)

type config struct {
Host string `json:"host"`
Token string `json:"token"`
Domain string `json:"domain"`
Token string `json:"token"`
}

var configSchema = schematypes.Object{
Expand All @@ -17,10 +17,10 @@ var configSchema = schematypes.Object{
[Releng API](https://wiki.mozilla.org/ReleaseEngineering/Applications/RelengAPI).
`),
Properties: schematypes.Properties{
"host": schematypes.String{
Title: "Releng API host endpoint",
"domain": schematypes.String{
Title: "Releng API base domain",
Description: util.Markdown(`
The releng API host endpoint. Default: api.pub.build.mozilla.org/.
The releng API base domain. Default: mozilla-releng.net.
`),
},
"token": schematypes.String{
Expand Down
48 changes: 35 additions & 13 deletions plugins/relengapi/relengapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net/http"
"net/http/httputil"
"strings"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -33,7 +34,7 @@ type taskPlugin struct {
tmpToken string
tmpTokenGoodUntil time.Time
permissions []string
relengapiHost string
relengapiDomain string
}

func init() {
Expand All @@ -51,8 +52,8 @@ func (p *plugin) PayloadSchema() schematypes.Object {
func (provider) NewPlugin(options plugins.PluginOptions) (plugins.Plugin, error) {
var c config
schematypes.MustValidateAndMap(configSchema, options.Config, &c)
if c.Host == "" {
c.Host = "api.pub.build.mozilla.org"
if c.Domain == "" {
c.Domain = "mozilla-releng.net"
}

if c.Token == "" {
Expand Down Expand Up @@ -99,17 +100,39 @@ func (p *plugin) NewTaskPlugin(options plugins.TaskPluginOptions) (plugins.TaskP
}

tp := &taskPlugin{
monitor: options.Monitor,
context: options.TaskContext,
permissions: perms,
relengapiHost: p.config.Host,
monitor: options.Monitor,
context: options.TaskContext,
permissions: perms,
relengapiDomain: p.config.Domain,
}

// stolen from relengapi-proxy
director := func(req *http.Request) {
req.URL.Scheme = "https"
req.URL.Host = p.config.Host
req.Host = p.config.Host
if strings.HasPrefix(req.URL.Path, "/tooltool") {
req.URL.Scheme = "https"
req.URL.Path = strings.TrimPrefix(req.URL.Path, "/tooltool")
req.URL.RawPath = ""
host := fmt.Sprintf("tooltool.%s", p.config.Domain)
req.URL.Host = host
req.Host = host
} else if strings.HasPrefix(req.URL.Path, "/treestatus") {
req.URL.Scheme = "https"
req.URL.Path = strings.TrimPrefix(req.URL.Path, "/treestatus")
req.URL.RawPath = ""
host := fmt.Sprintf("treestatus.%s", p.config.Domain)
req.URL.Host = host
req.Host = host
} else if strings.HasPrefix(req.URL.Path, "/mapper") {
req.URL.Scheme = "https"
req.URL.Path = strings.TrimPrefix(req.URL.Path, "/mapper")
req.URL.RawPath = ""
host := fmt.Sprintf("mapper.%s", p.config.Domain)
req.URL.Host = host
req.Host = host
} else {
// ignore everything else
return
}
tok, err := tp.getToken(p.config.Token)
if err != nil {
err = errors.Wrap(err, "Error retrieving token")
Expand Down Expand Up @@ -157,9 +180,8 @@ func (p *taskPlugin) getToken(issuingToken string) (string, error) {
if now.After(p.tmpTokenGoodUntil) {
expires := now.Add(tmpTokenLifetime)
debug("Generating new temporary token; expires at " + expires.String())
urlPrefix := fmt.Sprintf("https://%s", p.relengapiHost)
tok, err := getTmpToken(
urlPrefix, issuingToken, expires, p.permissions)
url := fmt.Sprintf("https://tokens.%s", p.relengapiDomain)
tok, err := getTmpToken(url, issuingToken, expires, p.permissions)
if err != nil {
return "", err
}
Expand Down
4 changes: 2 additions & 2 deletions plugins/relengapi/relengapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestTCProxySuccess(t *testing.T) {
Payload: `{
"delay": 100,
"function": "ping-proxy",
"argument": "http://relengapi/versions",
"argument": "https://relengapi/tooltool/docs/index.html",
"enableRelengAPIProxy": true
}`,
Plugin: "relengapi",
Expand All @@ -32,7 +32,7 @@ func TestTCProxyFail(t *testing.T) {
Payload: `{
"delay": 100,
"function": "ping-proxy",
"argument": "http://relengapi/foo/bar",
"argument": "https://relengapi/foo/bar",
"enableRelengAPIProxy": true
}`,
Plugin: "relengapi",
Expand Down
4 changes: 2 additions & 2 deletions plugins/relengapi/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type relengapiTokenJSON struct {
Token string `json:"token,omitempty"`
}

func getTmpToken(urlPrefix string, issuingToken string, expires time.Time, perms []string) (tok string, err error) {
func getTmpToken(url string, issuingToken string, expires time.Time, perms []string) (tok string, err error) {
request := relengapiTokenJSON{
Typ: "tmp",
Expires: &expires,
Expand All @@ -36,7 +36,7 @@ func getTmpToken(urlPrefix string, issuingToken string, expires time.Time, perms
}

client := &http.Client{}
reqURL := fmt.Sprintf("%s/tokenauth/tokens", urlPrefix)
reqURL := fmt.Sprintf("%s/tokens", url)
req, err := http.NewRequest("POST", reqURL, bytes.NewBuffer(reqbody))
if err != nil {
return
Expand Down

0 comments on commit 4c9135b

Please sign in to comment.