-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command to look up a lease by ID (#11129)
* snapshot * basic test * update command and add documentation * update help text * typo * add changelog for lease lookup command * run go mod vendor * remove tabs from help output
- Loading branch information
Showing
9 changed files
with
304 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:feature | ||
cli/api: Add lease lookup command | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package command | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/mitchellh/cli" | ||
"github.com/posener/complete" | ||
) | ||
|
||
var _ cli.Command = (*LeaseLookupCommand)(nil) | ||
var _ cli.CommandAutocomplete = (*LeaseLookupCommand)(nil) | ||
|
||
type LeaseLookupCommand struct { | ||
*BaseCommand | ||
} | ||
|
||
func (c *LeaseLookupCommand) Synopsis() string { | ||
return "Lookup the lease of a secret" | ||
} | ||
|
||
func (c *LeaseLookupCommand) Help() string { | ||
helpText := ` | ||
Usage: vault lease lookup ID | ||
Lookup the lease information of a secret. | ||
Every secret in Vault has a lease associated with it. Users can look up | ||
information on the lease by referencing the lease ID. | ||
Lookup lease of a secret: | ||
$ vault lease lookup database/creds/readonly/2f6a614c... | ||
` + c.Flags().Help() | ||
|
||
return strings.TrimSpace(helpText) | ||
} | ||
|
||
func (c *LeaseLookupCommand) Flags() *FlagSets { | ||
set := c.flagSet(FlagSetHTTP | FlagSetOutputFormat) | ||
|
||
return set | ||
} | ||
|
||
func (c *LeaseLookupCommand) AutocompleteArgs() complete.Predictor { | ||
return complete.PredictAnything | ||
} | ||
|
||
func (c *LeaseLookupCommand) AutocompleteFlags() complete.Flags { | ||
return c.Flags().Completions() | ||
} | ||
|
||
func (c *LeaseLookupCommand) Run(args []string) int { | ||
f := c.Flags() | ||
|
||
if err := f.Parse(args); err != nil { | ||
c.UI.Error(err.Error()) | ||
return 1 | ||
} | ||
|
||
leaseID := "" | ||
|
||
args = f.Args() | ||
switch len(args) { | ||
case 0: | ||
c.UI.Error("Missing ID!") | ||
return 1 | ||
case 1: | ||
leaseID = strings.TrimSpace(args[0]) | ||
default: | ||
c.UI.Error(fmt.Sprintf("Too many arguments (expected 1, got %d)", len(args))) | ||
return 1 | ||
} | ||
|
||
client, err := c.Client() | ||
if err != nil { | ||
c.UI.Error(err.Error()) | ||
return 2 | ||
} | ||
|
||
secret, err := client.Sys().Lookup(leaseID) | ||
if err != nil { | ||
c.UI.Error(fmt.Sprintf("error looking up lease id %s: %s", leaseID, err)) | ||
return 2 | ||
} | ||
|
||
return OutputSecret(c.UI, secret) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package command | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/hashicorp/vault/api" | ||
"github.com/mitchellh/cli" | ||
) | ||
|
||
func testLeaseLookupCommand(tb testing.TB) (*cli.MockUi, *LeaseLookupCommand) { | ||
tb.Helper() | ||
|
||
ui := cli.NewMockUi() | ||
return ui, &LeaseLookupCommand{ | ||
BaseCommand: &BaseCommand{ | ||
UI: ui, | ||
}, | ||
} | ||
} | ||
|
||
// testLeaseLookupCommandMountAndLease mounts a leased secret backend and returns | ||
// the leaseID of an item. | ||
func testLeaseLookupCommandMountAndLease(tb testing.TB, client *api.Client) string { | ||
if err := client.Sys().Mount("testing", &api.MountInput{ | ||
Type: "generic-leased", | ||
}); err != nil { | ||
tb.Fatal(err) | ||
} | ||
|
||
if _, err := client.Logical().Write("testing/foo", map[string]interface{}{ | ||
"key": "value", | ||
"lease": "5m", | ||
}); err != nil { | ||
tb.Fatal(err) | ||
} | ||
|
||
// Read the secret back to get the leaseID | ||
secret, err := client.Logical().Read("testing/foo") | ||
if err != nil { | ||
tb.Fatal(err) | ||
} | ||
if secret == nil || secret.LeaseID == "" { | ||
tb.Fatalf("missing secret or lease: %#v", secret) | ||
} | ||
|
||
return secret.LeaseID | ||
} | ||
|
||
// TestLeaseLookupCommand_Run tests basic lookup | ||
func TestLeaseLookupCommand_Run(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("empty", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
client, closer := testVaultServer(t) | ||
defer closer() | ||
|
||
_ = testLeaseLookupCommandMountAndLease(t, client) | ||
|
||
ui, cmd := testLeaseLookupCommand(t) | ||
cmd.client = client | ||
|
||
code := cmd.Run(nil) | ||
if exp := 1; code != exp { | ||
t.Errorf("expected %d to be %d", code, exp) | ||
} | ||
|
||
combined := ui.OutputWriter.String() + ui.ErrorWriter.String() | ||
expectedMsg := "Missing ID!" | ||
if !strings.Contains(combined, expectedMsg) { | ||
t.Errorf("expected %q to contain %q", combined, expectedMsg) | ||
} | ||
}) | ||
|
||
t.Run("integration", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
client, closer := testVaultServer(t) | ||
defer closer() | ||
|
||
leaseID := testLeaseLookupCommandMountAndLease(t, client) | ||
|
||
_, cmd := testLeaseLookupCommand(t) | ||
cmd.client = client | ||
|
||
code := cmd.Run([]string{leaseID}) | ||
if exp := 0; code != exp { | ||
t.Errorf("expected %d to be %d", code, exp) | ||
} | ||
}) | ||
|
||
t.Run("no_tabs", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
_, cmd := testLeaseLookupCommand(t) | ||
assertNoTabs(t, cmd) | ||
}) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
--- | ||
layout: docs | ||
page_title: lease lookup - Command | ||
sidebar_title: <code>lookup</code> | ||
description: |- | ||
The "lease lookup" command retrieves information about a lease. | ||
--- | ||
|
||
# lease lookup | ||
|
||
The `lease lookup` command retrieves information on the lease of a secret. | ||
|
||
Every secret in Vault has a lease associated with it. Users can look up | ||
information on the lease by referencing the lease ID. | ||
|
||
## Examples | ||
|
||
Lookup a lease: | ||
|
||
```shell-session | ||
$ vault lease lookup database/creds/readonly/27e1b9a1-27b8-83d9-9fe0-d99d786bdc83 | ||
Key Value | ||
--- ----- | ||
expire_time 2021-03-17T11:55:50.755313-05:00 | ||
id database/creds/readonly/27e1b9a1-27b8-83d9-9fe0-d99d786bdc83 | ||
issue_time 2021-03-17T11:45:50.755312-05:00 | ||
last_renewal <nil> | ||
renewable true | ||
ttl 9m52s | ||
``` | ||
|
||
## Usage | ||
|
||
There are no flags beyond the [standard set of flags](/docs/commands) | ||
included on all commands. |