/
main.go
71 lines (61 loc) · 2.03 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright 2016 The Upspin Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Keyserver is a wrapper for a key implementation that presents it as an HTTP
// interface.
package main // import "upspin.io/cmd/keyserver"
import (
"flag"
"upspin.io/factotum"
"upspin.io/flags"
"upspin.io/log"
"upspin.io/serverutil"
"upspin.io/serverutil/keyserver"
"upspin.io/upspin"
// Load required transports
_ "upspin.io/key/transports"
// Possible storage backends.
"upspin.io/cloud/https"
_ "upspin.io/cloud/storage/disk"
)
var (
testUser = flag.String("test_user", "", "initialize a test `user` (localhost, inprocess only)")
testSecrets = flag.String("test_secrets", "", "initialize test user with the secrets in this `directory`")
)
func main() {
keyserver.Main(setupTestUser)
https.ListenAndServeFromFlags(nil)
}
// setupTestUser uses the -test_user and -test_secrets flags to bootstrap the
// inprocess key server with an initial user.
func setupTestUser(key upspin.KeyServer) {
if *testUser == "" {
return
}
if *testSecrets == "" {
log.Fatalf("cannot set up a test user without specifying -test_secrets")
}
// Sanity checks to make sure we're not doing this in production.
if key.Endpoint().Transport != upspin.InProcess {
log.Fatalf("cannot use testuser for endpoint %q", key.Endpoint())
}
if flags.InsecureHTTP {
if !serverutil.IsLoopback(flags.HTTPAddr) {
log.Fatal("cannot use -test_user flag on an insecure connection except on -http=localhost:port")
}
} else if !serverutil.IsLoopback(flags.HTTPSAddr) {
log.Fatal("cannot use -test_user flag on a secure connection except on -https=localhost:port")
}
f, err := factotum.NewFromDir(*testSecrets)
if err != nil {
log.Fatalf("unable to initialize factotum for %q: %v", *testUser, err)
}
userStruct := &upspin.User{
Name: upspin.UserName(*testUser),
PublicKey: f.PublicKey(),
}
err = key.Put(userStruct)
if err != nil {
log.Fatalf("Put %q failed: %v", *testUser, err)
}
}