2015-03-12 19:37:41 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/vault"
|
|
|
|
)
|
|
|
|
|
|
|
|
func handleSysInit(core *vault.Core) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
switch r.Method {
|
|
|
|
case "GET":
|
|
|
|
handleSysInitGet(core, w, r)
|
|
|
|
case "PUT":
|
|
|
|
handleSysInitPut(core, w, r)
|
|
|
|
default:
|
|
|
|
respondError(w, http.StatusMethodNotAllowed, nil)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleSysInitGet(core *vault.Core, w http.ResponseWriter, r *http.Request) {
|
|
|
|
init, err := core.Initialized()
|
|
|
|
if err != nil {
|
|
|
|
respondError(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
respondOk(w, &InitStatusResponse{
|
|
|
|
Initialized: init,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleSysInitPut(core *vault.Core, w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Parse the request
|
|
|
|
var req InitRequest
|
|
|
|
if err := parseRequest(r, &req); err != nil {
|
|
|
|
respondError(w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize
|
|
|
|
result, err := core.Initialize(&vault.SealConfig{
|
|
|
|
SecretShares: req.SecretShares,
|
|
|
|
SecretThreshold: req.SecretThreshold,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
respondError(w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode the keys
|
|
|
|
keys := make([]string, 0, len(result.SecretShares))
|
|
|
|
for _, k := range result.SecretShares {
|
|
|
|
keys = append(keys, hex.EncodeToString(k))
|
|
|
|
}
|
|
|
|
|
|
|
|
respondOk(w, &InitResponse{
|
2015-03-29 23:22:09 +00:00
|
|
|
Keys: keys,
|
|
|
|
RootToken: result.RootToken,
|
2015-03-12 19:37:41 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type InitRequest struct {
|
|
|
|
SecretShares int `json:"secret_shares"`
|
|
|
|
SecretThreshold int `json:"secret_threshold"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type InitResponse struct {
|
2015-03-29 23:22:09 +00:00
|
|
|
Keys []string `json:"keys"`
|
|
|
|
RootToken string `json:"root_token"`
|
2015-03-12 19:37:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type InitStatusResponse struct {
|
|
|
|
Initialized bool `json:"initialized"`
|
|
|
|
}
|