2015-03-11 19:03:28 +00:00
|
|
|
package api
|
|
|
|
|
2022-03-24 17:58:03 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
)
|
2018-07-24 22:49:55 +00:00
|
|
|
|
2015-03-11 19:03:28 +00:00
|
|
|
func (c *Sys) SealStatus() (*SealStatusResponse, error) {
|
2022-03-23 21:47:43 +00:00
|
|
|
return c.SealStatusWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Sys) SealStatusWithContext(ctx context.Context) (*SealStatusResponse, error) {
|
2022-03-24 17:58:03 +00:00
|
|
|
r := c.c.NewRequest(http.MethodGet, "/v1/sys/seal-status")
|
2022-03-23 21:47:43 +00:00
|
|
|
return sealStatusRequestWithContext(ctx, c, r)
|
2015-03-11 19:03:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Sys) Seal() error {
|
2022-03-23 21:47:43 +00:00
|
|
|
return c.SealWithContext(context.Background())
|
|
|
|
}
|
2018-07-24 22:49:55 +00:00
|
|
|
|
2022-03-23 21:47:43 +00:00
|
|
|
func (c *Sys) SealWithContext(ctx context.Context) error {
|
|
|
|
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
|
2018-07-24 22:49:55 +00:00
|
|
|
defer cancelFunc()
|
2022-03-23 21:47:43 +00:00
|
|
|
|
2022-03-24 17:58:03 +00:00
|
|
|
r := c.c.NewRequest(http.MethodPut, "/v1/sys/seal")
|
2022-03-23 21:47:43 +00:00
|
|
|
|
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-03-31 18:46:55 +00:00
|
|
|
}
|
2022-03-23 21:47:43 +00:00
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return nil
|
2015-03-11 19:03:28 +00:00
|
|
|
}
|
|
|
|
|
2015-10-28 19:59:39 +00:00
|
|
|
func (c *Sys) ResetUnsealProcess() (*SealStatusResponse, error) {
|
2022-03-23 21:47:43 +00:00
|
|
|
return c.ResetUnsealProcessWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Sys) ResetUnsealProcessWithContext(ctx context.Context) (*SealStatusResponse, error) {
|
2015-10-28 19:59:39 +00:00
|
|
|
body := map[string]interface{}{"reset": true}
|
|
|
|
|
2022-03-24 17:58:03 +00:00
|
|
|
r := c.c.NewRequest(http.MethodPut, "/v1/sys/unseal")
|
2015-10-28 19:59:39 +00:00
|
|
|
if err := r.SetJSONBody(body); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-23 21:47:43 +00:00
|
|
|
return sealStatusRequestWithContext(ctx, c, r)
|
2015-10-28 19:59:39 +00:00
|
|
|
}
|
|
|
|
|
2015-03-11 19:03:28 +00:00
|
|
|
func (c *Sys) Unseal(shard string) (*SealStatusResponse, error) {
|
2022-03-23 21:47:43 +00:00
|
|
|
return c.UnsealWithContext(context.Background(), shard)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Sys) UnsealWithContext(ctx context.Context, shard string) (*SealStatusResponse, error) {
|
2015-03-11 19:03:28 +00:00
|
|
|
body := map[string]interface{}{"key": shard}
|
|
|
|
|
2022-03-24 17:58:03 +00:00
|
|
|
r := c.c.NewRequest(http.MethodPut, "/v1/sys/unseal")
|
2015-03-11 19:03:28 +00:00
|
|
|
if err := r.SetJSONBody(body); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-23 21:47:43 +00:00
|
|
|
return sealStatusRequestWithContext(ctx, c, r)
|
2015-10-28 19:59:39 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 06:34:02 +00:00
|
|
|
func (c *Sys) UnsealWithOptions(opts *UnsealOpts) (*SealStatusResponse, error) {
|
2022-03-23 21:47:43 +00:00
|
|
|
return c.UnsealWithOptionsWithContext(context.Background(), opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Sys) UnsealWithOptionsWithContext(ctx context.Context, opts *UnsealOpts) (*SealStatusResponse, error) {
|
2022-03-24 17:58:03 +00:00
|
|
|
r := c.c.NewRequest(http.MethodPut, "/v1/sys/unseal")
|
2022-03-23 21:47:43 +00:00
|
|
|
|
2018-10-23 06:34:02 +00:00
|
|
|
if err := r.SetJSONBody(opts); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-23 21:47:43 +00:00
|
|
|
return sealStatusRequestWithContext(ctx, c, r)
|
2018-10-23 06:34:02 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 21:47:43 +00:00
|
|
|
func sealStatusRequestWithContext(ctx context.Context, c *Sys, r *Request) (*SealStatusResponse, error) {
|
|
|
|
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
|
2018-07-24 22:49:55 +00:00
|
|
|
defer cancelFunc()
|
2022-03-23 21:47:43 +00:00
|
|
|
|
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2015-03-11 19:03:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
var result SealStatusResponse
|
|
|
|
err = resp.DecodeJSON(&result)
|
|
|
|
return &result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
type SealStatusResponse struct {
|
2022-09-06 18:11:04 +00:00
|
|
|
Type string `json:"type"`
|
|
|
|
Initialized bool `json:"initialized"`
|
|
|
|
Sealed bool `json:"sealed"`
|
|
|
|
T int `json:"t"`
|
|
|
|
N int `json:"n"`
|
|
|
|
Progress int `json:"progress"`
|
|
|
|
Nonce string `json:"nonce"`
|
|
|
|
Version string `json:"version"`
|
|
|
|
BuildDate string `json:"build_date"`
|
|
|
|
Migration bool `json:"migration"`
|
|
|
|
ClusterName string `json:"cluster_name,omitempty"`
|
|
|
|
ClusterID string `json:"cluster_id,omitempty"`
|
|
|
|
RecoverySeal bool `json:"recovery_seal"`
|
|
|
|
StorageType string `json:"storage_type,omitempty"`
|
|
|
|
HCPLinkStatus string `json:"hcp_link_status,omitempty"`
|
|
|
|
HCPLinkResourceID string `json:"hcp_link_resource_ID,omitempty"`
|
2015-03-11 19:03:28 +00:00
|
|
|
}
|
2018-10-23 06:34:02 +00:00
|
|
|
|
|
|
|
type UnsealOpts struct {
|
|
|
|
Key string `json:"key"`
|
|
|
|
Reset bool `json:"reset"`
|
|
|
|
Migrate bool `json:"migrate"`
|
|
|
|
}
|