open-vault/http/sys_init.go

80 lines
1.7 KiB
Go
Raw Normal View History

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", "POST":
2015-03-12 19:37:41 +00:00
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,
2015-08-25 22:33:58 +00:00
PGPKeys: req.PGPKeys,
2015-03-12 19:37:41 +00:00
})
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"`
2015-08-25 22:33:58 +00:00
PGPKeys []string `json:"pgp_keys"`
2015-03-12 19:37:41 +00:00
}
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"`
}