vault: lookup without a token looks up self

This commit is contained in:
Mitchell Hashimoto 2015-03-31 12:50:07 -07:00
parent 6a72ea61d5
commit 63f259cc8d
2 changed files with 27 additions and 1 deletions

View File

@ -89,7 +89,7 @@ func NewTokenStore(c *Core) (*TokenStore, error) {
},
&framework.Path{
Pattern: "lookup/(?P<token>.+)",
Pattern: "lookup(/(?P<token>.+))?",
Fields: map[string]*framework.FieldSchema{
"token": &framework.FieldSchema{
@ -501,6 +501,9 @@ func (ts *TokenStore) handleRevokeOrphan(
func (ts *TokenStore) handleLookup(
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
id := data.Get("token").(string)
if id == "" {
id = req.ClientToken
}
if id == "" {
return logical.ErrorResponse("missing token ID"), logical.ErrInvalidRequest
}

View File

@ -538,6 +538,29 @@ func TestTokenStore_HandleRequest_Lookup(t *testing.T) {
}
}
func TestTokenStore_HandleRequest_LookupSelf(t *testing.T) {
_, ts, root := mockTokenStore(t)
req := logical.TestRequest(t, logical.ReadOperation, "lookup")
req.ClientToken = root
resp, err := ts.HandleRequest(req)
if err != nil {
t.Fatalf("err: %v %v", err, resp)
}
if resp == nil {
t.Fatalf("bad: %#v", resp)
}
exp := map[string]interface{}{
"id": root,
"policies": []string{"root"},
"path": "sys/root",
"meta": map[string]string(nil),
}
if !reflect.DeepEqual(resp.Data, exp) {
t.Fatalf("bad: %#v exp: %#v", resp.Data, exp)
}
}
func testMakeToken(t *testing.T, ts *TokenStore, root, client string, policy []string) {
req := logical.TestRequest(t, logical.WriteOperation, "create")
req.ClientToken = root