forked from upspin/upspin
/
setupserver.go
189 lines (170 loc) · 5.79 KB
/
setupserver.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright 2017 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.
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net"
"net/http"
"path/filepath"
"strings"
"github.com/palager/upspin/bind"
"github.com/palager/upspin/config"
"github.com/palager/upspin/factotum"
"github.com/palager/upspin/flags"
"github.com/palager/upspin/subcmd"
"github.com/palager/upspin/upspin"
)
func (s *State) setupserver(args ...string) {
const (
help = `
Setupserver is the final step of setting up an upspinserver.
It assumes that you have run 'setupdomain' and (optionally) 'setupstorage'.
It registers the user created by 'setupdomain' domain with the key server,
copies the configuration files from $where/$domain to the upspinserver and
restarts it, puts the Writers file, and makes the root for the calling user.
The calling user and the server user are included in the Writers file by
default (giving them write access to the store and directory). You may specify
additional writers with the -writers flag. For instance, if you want all users
@example.com to be able to access storage, specify "-writers=*@example.com".
The calling user must be the same one that ran 'upspin setupdomain'.
`
)
fs := flag.NewFlagSet("setupserver", flag.ExitOnError)
where := fs.String("where", filepath.Join(config.Home(), "upspin", "deploy"), "`directory` to store private configuration files")
domain := fs.String("domain", "", "domain `name` for this Upspin installation")
host := fs.String("host", "", "host `name` of upspinserver (empty implies the cluster dir.domain and store.domain)")
writers := fs.String("writers", "", "additional `users` to be given write access to this server")
s.ParseFlags(fs, args, help, "setupserver -domain=<domain> -host=<host> [-where=$HOME/upspin/deploy] [-writers=user,...]")
if *domain == "" || *host == "" {
s.Failf("the -domain and -host flags must be provided")
usageAndExit(fs)
}
cfgPath := filepath.Join(subcmd.Tilde(*where), *domain)
cfg := s.ReadServerConfig(cfgPath)
// Stash the provided host name in the server config file.
if !strings.Contains(*host, ":") {
*host += ":443"
}
_, _, err := net.SplitHostPort(*host)
if err != nil {
s.Exitf("invalid -host argument %q: %v", *host, err)
}
cfg.Addr = upspin.NetAddr(*host)
s.WriteServerConfig(cfgPath, cfg)
ep := upspin.Endpoint{
Transport: upspin.Remote,
NetAddr: cfg.Addr,
}
// Put the server user to the key server.
key, err := bind.KeyServer(s.Config, s.Config.KeyEndpoint())
if err != nil {
s.Exit(err)
}
local, err := userFor(cfgPath, cfg)
if err != nil {
s.Exit(err)
}
remote, err := key.Lookup(cfg.User)
if err == nil {
// TODO(adg): compare local and remote for discrepancies.
_ = remote
fmt.Fprintf(s.Stderr, "User %q already exists on key server.\n", cfg.User)
} else {
if err := key.Put(local); err != nil {
// TODO(adg): Check whether the TXT record for this
// domain is in place.
s.Exit(err)
}
fmt.Fprintf(s.Stderr, "Successfully put %q to the key server.\n", cfg.User)
}
// Create Writers file.
var buf bytes.Buffer
fmt.Fprintf(&buf, "%s\n%s\n", s.Config.UserName(), cfg.User)
for _, user := range strings.Split(*writers, ",") {
user = strings.TrimSpace(user)
if user == "" {
continue
}
fmt.Fprintf(&buf, "%s\n", user)
}
if err := ioutil.WriteFile(filepath.Join(cfgPath, "Writers"), buf.Bytes(), 0644); err != nil {
s.Exit(err)
}
// Write Upspin config file for the server user,
// for convenience when acting as the server user.
configFile := filepath.Join(cfgPath, "config")
configBody := new(bytes.Buffer)
if err := configTemplate.Execute(configBody, configData{
UserName: cfg.User,
Store: &ep,
Dir: &ep,
SecretDir: cfgPath,
Packing: "ee",
}); err != nil {
s.Exit(err)
}
if err := ioutil.WriteFile(configFile, configBody.Bytes(), 0644); err != nil {
s.Exit(err)
}
// Put server config to the remote upspinserver.
s.configureServer(cfgPath, cfg)
fmt.Fprintf(s.Stderr, "Configured upspinserver at %q.\n", cfg.Addr)
// Check that the current configuration points to our new server.
// If not, ask the user to change it and update the key server.
if s.Config.DirEndpoint() != ep || s.Config.StoreEndpoint() != ep {
fmt.Fprintf(s.Stderr, "Your current configuration in %q has these values:\n", flags.Config)
fmt.Fprintf(s.Stderr, "\tdirserver: %v\n\tstoreserver: %v\n\n", s.Config.DirEndpoint(), s.Config.StoreEndpoint())
fmt.Fprintf(s.Stderr, "To use the server we are setting up now, these values should be\n")
fmt.Fprintf(s.Stderr, "\tdirserver: %v\n\tstoreserver: %v\n\n", ep, ep)
return
}
// Make the current user root.
root := string(s.Config.UserName())
s.mkdir(root)
fmt.Fprintf(s.Stderr, "Created root %q.\n", root)
}
func userFor(cfgPath string, cfg *subcmd.ServerConfig) (*upspin.User, error) {
ep := []upspin.Endpoint{{
Transport: upspin.Remote,
NetAddr: cfg.Addr,
}}
fact, err := factotum.NewFromDir(cfgPath)
if err != nil {
return nil, err
}
return &upspin.User{
Name: cfg.User,
Dirs: ep,
Stores: ep,
PublicKey: fact.PublicKey(),
}, nil
}
func (s *State) configureServer(cfgPath string, cfg *subcmd.ServerConfig) {
files := map[string][]byte{}
for _, name := range subcmd.SetupServerFiles {
b, err := ioutil.ReadFile(filepath.Join(cfgPath, name))
if err != nil {
s.Exit(err)
}
files[name] = b
}
b, err := json.Marshal(files)
if err != nil {
s.Exit(err)
}
u := "https://" + string(cfg.Addr) + "/setupserver"
resp, err := http.Post(u, "application/octet-stream", bytes.NewReader(b))
if err != nil {
s.Exit(err)
}
b, _ = ioutil.ReadAll(resp.Body)
resp.Body.Close()
if resp.StatusCode != http.StatusOK {
s.Exitf("upspinserver returned status %v:\n%s", resp.Status, b)
}
}