-
Notifications
You must be signed in to change notification settings - Fork 0
/
registration.go
235 lines (215 loc) · 7.25 KB
/
registration.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package apiserver
import (
"crypto/rand"
"encoding/json"
"io"
"net/http"
"github.com/go-macaroon-bakery/macaroon-bakery/v3/httpbakery"
"github.com/juju/errors"
"github.com/juju/names/v4"
"golang.org/x/crypto/nacl/secretbox"
"gopkg.in/macaroon.v2"
"github.com/juju/juju/apiserver/common"
coremacaroon "github.com/juju/juju/core/macaroon"
"github.com/juju/juju/environs"
"github.com/juju/juju/rpc/params"
"github.com/juju/juju/state"
"github.com/juju/juju/state/stateenvirons"
)
const (
secretboxNonceLength = 24
secretboxKeyLength = 32
)
// registerUserHandler is an http.Handler for the "/register" endpoint. This is
// used to complete a secure user registration process, and provide controller
// login credentials.
type registerUserHandler struct {
ctxt httpContext
}
// ServeHTTP implements the http.Handler interface.
func (h *registerUserHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if req.Method != "POST" {
err := sendError(w, errors.MethodNotAllowedf("unsupported method: %q", req.Method))
if err != nil {
logger.Errorf("%v", err)
}
return
}
st, err := h.ctxt.stateForRequestUnauthenticated(req)
if err != nil {
if err := sendError(w, err); err != nil {
logger.Errorf("%v", err)
}
return
}
defer st.Release()
userTag, response, err := h.processPost(req, st.State)
if err != nil {
if err := sendError(w, err); err != nil {
logger.Errorf("%v", err)
}
return
}
// Set a short-lived macaroon as a cookie on the response,
// which the client can use to obtain a discharge macaroon.
m, err := h.ctxt.srv.authenticator.CreateLocalLoginMacaroon(req.Context(), userTag, httpbakery.RequestVersion(req))
if err != nil {
if err := sendError(w, err); err != nil {
logger.Errorf("%v", err)
}
return
}
cookie, err := httpbakery.NewCookie(coremacaroon.MacaroonNamespace, macaroon.Slice{m})
if err != nil {
if err := sendError(w, err); err != nil {
logger.Errorf("%v", err)
}
return
}
http.SetCookie(w, cookie)
if err := sendStatusAndJSON(w, http.StatusOK, response); err != nil {
logger.Errorf("%v", err)
}
}
// The client will POST to the "/register" endpoint with a JSON-encoded
// params.SecretKeyLoginRequest. This contains the tag of the user they
// are registering, a (supposedly) unique nonce, and a ciphertext which
// is the result of concatenating the user and nonce values, and then
// encrypting and authenticating them with the NaCl Secretbox algorithm.
//
// If the server can decrypt the ciphertext, then it knows the client
// has the required secret key; thus they are authenticated. The client
// does not have the CA certificate for communicating securely with the
// server, and so must also authenticate the server. The server will
// similarly generate a unique nonce and encrypt the response payload
// using the same secret key as the client. If the client can decrypt
// the payload, it knows the server has the required secret key; thus
// it is also authenticated.
//
// NOTE(axw) it is important that the client and server choose their
// own nonces, because reusing a nonce means that the key-stream can
// be revealed.
func (h *registerUserHandler) processPost(req *http.Request, st *state.State) (
names.UserTag, *params.SecretKeyLoginResponse, error,
) {
failure := func(err error) (names.UserTag, *params.SecretKeyLoginResponse, error) {
return names.UserTag{}, nil, err
}
data, err := io.ReadAll(req.Body)
if err != nil {
return failure(err)
}
var loginRequest params.SecretKeyLoginRequest
if err := json.Unmarshal(data, &loginRequest); err != nil {
return failure(err)
}
// Basic validation: ensure that the request contains a valid user tag,
// nonce, and ciphertext of the expected length.
userTag, err := names.ParseUserTag(loginRequest.User)
if err != nil {
return failure(err)
}
if len(loginRequest.Nonce) != secretboxNonceLength {
return failure(errors.NotValidf("nonce"))
}
// Decrypt the ciphertext with the user's secret key (if it has one).
user, err := st.User(userTag)
if err != nil {
return failure(err)
}
if len(user.SecretKey()) != secretboxKeyLength {
return failure(errors.NotFoundf("secret key for user %q", user.Name()))
}
var key [secretboxKeyLength]byte
var nonce [secretboxNonceLength]byte
copy(key[:], user.SecretKey())
copy(nonce[:], loginRequest.Nonce)
payloadBytes, ok := secretbox.Open(nil, loginRequest.PayloadCiphertext, &nonce, &key)
if !ok {
// Cannot decrypt the ciphertext, which implies that the secret
// key specified by the client is invalid.
return failure(errors.NotValidf("secret key"))
}
// Unmarshal the request payload, which contains the new password to
// set for the user.
var requestPayload params.SecretKeyLoginRequestPayload
if err := json.Unmarshal(payloadBytes, &requestPayload); err != nil {
return failure(errors.Annotate(err, "cannot unmarshal payload"))
}
if err := user.SetPassword(requestPayload.Password); err != nil {
return failure(errors.Annotate(err, "setting new password"))
}
// Respond with the CA-cert and password, encrypted again with the
// secret key.
responsePayload, err := h.getSecretKeyLoginResponsePayload(st, userTag)
if err != nil {
return failure(errors.Trace(err))
}
payloadBytes, err = json.Marshal(responsePayload)
if err != nil {
return failure(errors.Trace(err))
}
if _, err := rand.Read(nonce[:]); err != nil {
return failure(errors.Trace(err))
}
response := ¶ms.SecretKeyLoginResponse{
Nonce: nonce[:],
PayloadCiphertext: secretbox.Seal(nil, payloadBytes, &nonce, &key),
}
return userTag, response, nil
}
func getConnectorInfoer(model stateenvirons.Model) (environs.ConnectorInfo, error) {
configGetter := stateenvirons.EnvironConfigGetter{Model: model}
environ, err := common.EnvironFuncForModel(model, configGetter)()
if err != nil {
return nil, errors.Trace(err)
}
if connInfo, ok := environ.(environs.ConnectorInfo); ok {
return connInfo, nil
}
return nil, errors.NotSupportedf("environ %q", environ.Config().Type())
}
// For testing.
var GetConnectorInfoer = getConnectorInfoer
// getSecretKeyLoginResponsePayload returns the information required by the
// client to login to the controller securely.
func (h *registerUserHandler) getSecretKeyLoginResponsePayload(
st *state.State, userTag names.UserTag,
) (*params.SecretKeyLoginResponsePayload, error) {
if !st.IsController() {
return nil, errors.New("state is not for a controller")
}
controllerConfig, err := st.ControllerConfig()
if err != nil {
return nil, errors.Trace(err)
}
caCert, _ := controllerConfig.CACert()
payload := params.SecretKeyLoginResponsePayload{
CACert: caCert,
ControllerUUID: st.ControllerUUID(),
}
model, err := st.Model()
if err != nil {
return nil, errors.Trace(err)
}
connInfo, err := GetConnectorInfoer(model)
if errors.Is(err, errors.NotSupported) { // Not all providers support this.
return &payload, nil
}
if err != nil {
return nil, errors.Trace(err)
}
proxier, err := connInfo.ConnectionProxyInfo()
if errors.Is(err, errors.NotFound) {
return &payload, nil
}
if err != nil {
return nil, errors.Trace(err)
}
if payload.ProxyConfig, err = params.NewProxy(proxier); err != nil {
return nil, errors.Trace(err)
}
return &payload, nil
}