Skip to content

Commit 4aee3e9

Browse files
committed
app/appengine, cmd/genbuilderkey: move key gen from App Engine to new tool
This moves the /key handler to generate build keys to a standalone tool. The old way has been largely broken for years (when using the build.golang.org domain) due to internal App Engine changes. We have to access it via https://build-dot-golang-org.appspot.com/key instead to get authenticated. Also, the App Engine go112 runtime doesn't support authenticated handlers, so more reason to move off App Engine. This CL is part of a series to move off the the build.golang.org App Engine app that mirrors the git history into Datastore Entities, which is full of complication and bugs. These early steps are about removing a bunch of code from the App Engine app so the important bits are easy to see and refactor. Updates golang/go#34744 Change-Id: Iaf8e2bf458b5fea45bf05026d8a6eaf0ead88ec2 Reviewed-on: https://go-review.googlesource.com/c/build/+/208320 Reviewed-by: Bryan C. Mills <[email protected]> Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent cf930f6 commit 4aee3e9

File tree

4 files changed

+51
-12
lines changed

4 files changed

+51
-12
lines changed

app/appengine/app.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ handlers:
55
- url: /static
66
static_dir: app/appengine/static
77
secure: always
8-
- url: /(init|buildtest|key|_ah/queue/go/delay)
8+
- url: /(init|buildtest|_ah/queue/go/delay)
99
script: auto
1010
login: admin
1111
secure: always

app/appengine/dash.go

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ func main() {
1919

2020
// admin handlers
2121
handleFunc("/init", initHandler)
22-
handleFunc("/key", keyHandler)
2322

2423
// authenticated handlers
2524
handleFunc("/building", AuthHandler(buildingHandler))

app/appengine/handler.go

-10
Original file line numberDiff line numberDiff line change
@@ -603,16 +603,6 @@ func AuthHandler(h dashHandler) http.HandlerFunc {
603603
}
604604
}
605605

606-
func keyHandler(w http.ResponseWriter, r *http.Request) {
607-
builder := r.FormValue("builder")
608-
if builder == "" {
609-
logErr(w, r, errors.New("must supply builder in query string"))
610-
return
611-
}
612-
c := contextForRequest(r)
613-
fmt.Fprint(w, builderKey(c, builder))
614-
}
615-
616606
func validHash(hash string) bool {
617607
// TODO(adg): correctly validate a hash
618608
return hash != ""

cmd/genbuilderkey/genbuilderkey.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2019 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// The genbuilderkey binary generates a builder key or gomote user key
6+
// from the build system's master key.
7+
package main
8+
9+
import (
10+
"bytes"
11+
"crypto/hmac"
12+
"crypto/md5"
13+
"flag"
14+
"fmt"
15+
"io"
16+
"io/ioutil"
17+
"log"
18+
"os"
19+
"path/filepath"
20+
"strings"
21+
22+
"cloud.google.com/go/compute/metadata"
23+
)
24+
25+
func main() {
26+
flag.Parse()
27+
if flag.NArg() != 1 {
28+
log.Fatalf("expect one argument")
29+
}
30+
fmt.Println(key(flag.Arg(0)))
31+
}
32+
33+
func key(principal string) string {
34+
h := hmac.New(md5.New, getMasterKey())
35+
io.WriteString(h, principal)
36+
return fmt.Sprintf("%x", h.Sum(nil))
37+
}
38+
39+
func getMasterKey() []byte {
40+
v, err := metadata.ProjectAttributeValue("builder-master-key")
41+
if err == nil {
42+
return []byte(strings.TrimSpace(v))
43+
}
44+
key, err := ioutil.ReadFile(filepath.Join(os.Getenv("HOME"), "keys/gobuilder-master.key"))
45+
if err == nil {
46+
return bytes.TrimSpace(key)
47+
}
48+
log.Fatalf("no builder master key found")
49+
panic("not reachable")
50+
}

0 commit comments

Comments
 (0)