Skip to content

Commit

Permalink
Add support for reading raw API endpoints
Browse files Browse the repository at this point in the history
Some endpoints, such as `pki/ca` and `pki/ca/pem` return non-JSON
objects. When calling `vault read` on these endpoints, an error
is returned because they cannot be parsed as api.Secret instances:

> Error reading pki/ca/pem: invalid character '-' in numeric literal

Indeed, we go to all the trouble of (successfully) fetching this value,
only to be unable to Unmarshal into a Secrets value. Instead, add
support for a new -format=raw option, allowing these endpoints to be
consumed by callers of `vault read` directly.

Signed-off-by: Alexander Scheel <[email protected]>
  • Loading branch information
cipherboy committed Oct 26, 2022
1 parent a02a290 commit cd638de
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions command/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,40 @@ func (c *ReadCommand) Run(args []string) int {
return 1
}

secret, err := client.Logical().ReadWithData(path, data)
if Format(c.UI) != "raw" {
secret, err := client.Logical().ReadWithData(path, data)
if err != nil {
c.UI.Error(fmt.Sprintf("Error reading %s: %s", path, err))
return 2
}
if secret == nil {
c.UI.Error(fmt.Sprintf("No value found at %s", path))
return 2
}

if c.flagField != "" {
return PrintRawField(c.UI, secret, c.flagField)
}

return OutputSecret(c.UI, secret)
}

resp, err := client.Logical().ReadRawWithData(path, data)
if err != nil {
c.UI.Error(fmt.Sprintf("Error reading %s: %s", path, err))
c.UI.Error(fmt.Sprintf("Error reading: %s: %s", path, err))
return 2
}
if secret == nil {
if resp == nil || resp.Body == nil {
c.UI.Error(fmt.Sprintf("No value found at %s", path))
return 2
}
defer resp.Body.Close()

if c.flagField != "" {
return PrintRawField(c.UI, secret, c.flagField)
contents, err := io.ReadAll(resp.Body)
if err != nil {
c.UI.Error(fmt.Sprintf("Error reading: %s: %s", path, err))
return 2
}

return OutputSecret(c.UI, secret)
return OutputData(c.UI, contents)
}

0 comments on commit cd638de

Please sign in to comment.