From f0c804e42a7574babcfadc2c794101f4328dfda7 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 11 Mar 2015 19:33:20 +0100 Subject: [PATCH] api: sys methods --- api/SPEC.md | 3 ++- api/client.go | 4 ++-- api/response.go | 19 +++++++++++++++++++ api/sys.go | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 api/response.go diff --git a/api/SPEC.md b/api/SPEC.md index cba43c2e6a..8a49e07215 100644 --- a/api/SPEC.md +++ b/api/SPEC.md @@ -91,7 +91,7 @@ The following HTTP status codes are used throughout the API. # Group Seal/Unseal -## Seal [/sys/seal] +## Seal Status [/sys/seal-status] ### Seal Status [GET] Returns the status of whether the vault is currently sealed or not, as well as the progress of unsealing. @@ -116,6 +116,7 @@ The response has the following attributes: "progress": 1 } +## Seal [/sys/seal] ### Seal [PUT] Seal the vault. diff --git a/api/client.go b/api/client.go index 56e6d19e30..7115499f4e 100644 --- a/api/client.go +++ b/api/client.go @@ -67,7 +67,7 @@ func (c *Client) NewRequest(method, path string) *Request { // RawRequest performs the raw request given. This request may be against // a Vault server not configured with this client. This is an advanced operation // that generally won't need to be called externally. -func (c *Client) RawRequest(r *Request) (*http.Response, error) { +func (c *Client) RawRequest(r *Request) (*Response, error) { req, err := r.ToHTTP() if err != nil { return nil, err @@ -78,5 +78,5 @@ func (c *Client) RawRequest(r *Request) (*http.Response, error) { return nil, err } - return resp, nil + return &Response{Response: resp}, nil } diff --git a/api/response.go b/api/response.go new file mode 100644 index 0000000000..d624d5c59e --- /dev/null +++ b/api/response.go @@ -0,0 +1,19 @@ +package api + +import ( + "encoding/json" + "net/http" +) + +// Response is a raw response that wraps an HTTP response. +type Response struct { + *http.Response +} + +// DecodeJSON will decode the response body to a JSON structure. This +// will consume the response body, but will not close it. Close must +// still be called. +func (r *Response) DecodeJSON(out interface{}) error { + dec := json.NewDecoder(r.Body) + return dec.Decode(out) +} diff --git a/api/sys.go b/api/sys.go index 5fb111887c..fc0a74defa 100644 --- a/api/sys.go +++ b/api/sys.go @@ -9,3 +9,53 @@ type Sys struct { func (c *Client) Sys() *Sys { return &Sys{c: c} } + +func (c *Sys) SealStatus() (*SealStatusResponse, error) { + r := c.c.NewRequest("GET", "/sys/seal-status") + resp, err := c.c.RawRequest(r) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + var result SealStatusResponse + err = resp.DecodeJSON(&result) + return &result, err +} + +func (c *Sys) Seal() error { + r := c.c.NewRequest("PUT", "/sys/seal") + resp, err := c.c.RawRequest(r) + defer resp.Body.Close() + return err +} + +func (c *Sys) Unseal(shard string) (*SealStatusResponse, error) { + body := map[string]interface{}{"key": shard} + + r := c.c.NewRequest("PUT", "/sys/unseal") + if err := r.SetJSONBody(body); err != nil { + return nil, err + } + + resp, err := c.c.RawRequest(r) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + var result SealStatusResponse + err = resp.DecodeJSON(&result) + return &result, err +} + +// Structures for the requests/resposne are all down here. They aren't +// individually documentd because the map almost directly to the raw HTTP API +// documentation. Please refer to that documentation for more details. + +type SealStatusResponse struct { + Sealed bool + T int + N int + Progress int +}