2019-06-20 19:14:58 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-06-24 22:49:28 +00:00
|
|
|
"crypto/tls"
|
2019-10-11 18:56:59 +00:00
|
|
|
"errors"
|
2019-06-20 19:14:58 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
|
2019-06-23 01:51:52 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/tlsutil"
|
2019-06-20 19:14:58 +00:00
|
|
|
"github.com/hashicorp/vault/vault"
|
|
|
|
)
|
|
|
|
|
|
|
|
func handleSysRaftJoin(core *vault.Core) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
switch r.Method {
|
|
|
|
case "POST", "PUT":
|
|
|
|
handleSysRaftJoinPost(core, w, r)
|
|
|
|
default:
|
|
|
|
respondError(w, http.StatusMethodNotAllowed, nil)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleSysRaftJoinPost(core *vault.Core, w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Parse the request
|
|
|
|
var req JoinRequest
|
2019-10-15 04:55:31 +00:00
|
|
|
if _, err := parseRequest(core.PerfStandby(), r, w, &req); err != nil && err != io.EOF {
|
2019-06-20 19:14:58 +00:00
|
|
|
respondError(w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-11 18:56:59 +00:00
|
|
|
if req.NonVoter && !nonVotersAllowed {
|
|
|
|
respondError(w, http.StatusBadRequest, errors.New("non-voting nodes not allowed"))
|
|
|
|
}
|
|
|
|
|
2019-06-24 22:49:28 +00:00
|
|
|
var tlsConfig *tls.Config
|
|
|
|
var err error
|
|
|
|
if len(req.LeaderCACert) != 0 || len(req.LeaderClientCert) != 0 || len(req.LeaderClientKey) != 0 {
|
|
|
|
tlsConfig, err = tlsutil.ClientTLSConfig([]byte(req.LeaderCACert), []byte(req.LeaderClientCert), []byte(req.LeaderClientKey))
|
|
|
|
if err != nil {
|
|
|
|
respondError(w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
2019-06-21 21:41:07 +00:00
|
|
|
}
|
|
|
|
|
2019-10-11 18:56:59 +00:00
|
|
|
joined, err := core.JoinRaftCluster(context.Background(), req.LeaderAPIAddr, tlsConfig, req.Retry, req.NonVoter)
|
2019-06-20 19:14:58 +00:00
|
|
|
if err != nil {
|
|
|
|
respondError(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := JoinResponse{
|
|
|
|
Joined: joined,
|
|
|
|
}
|
|
|
|
respondOk(w, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
type JoinResponse struct {
|
|
|
|
Joined bool `json:"joined"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type JoinRequest struct {
|
2019-06-21 21:41:07 +00:00
|
|
|
LeaderAPIAddr string `json:"leader_api_addr"`
|
2019-08-29 21:37:01 +00:00
|
|
|
LeaderCACert string `json:"leader_ca_cert"`
|
2019-06-21 21:41:07 +00:00
|
|
|
LeaderClientCert string `json:"leader_client_cert"`
|
|
|
|
LeaderClientKey string `json:"leader_client_key"`
|
|
|
|
Retry bool `json:"retry"`
|
2019-10-11 18:56:59 +00:00
|
|
|
NonVoter bool `json:"non_voter"`
|
2019-06-20 19:14:58 +00:00
|
|
|
}
|