2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2016-03-03 17:24:28 +00:00
|
|
|
package vault
|
2016-03-05 05:03:55 +00:00
|
|
|
|
|
|
|
import (
|
2018-09-04 18:18:59 +00:00
|
|
|
"fmt"
|
2016-03-05 05:03:55 +00:00
|
|
|
"reflect"
|
2017-11-03 15:20:10 +00:00
|
|
|
"sort"
|
2016-03-05 05:03:55 +00:00
|
|
|
"testing"
|
2018-06-03 22:14:51 +00:00
|
|
|
"time"
|
2017-11-03 15:20:10 +00:00
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
"github.com/hashicorp/vault/helper/namespace"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2016-03-05 05:03:55 +00:00
|
|
|
)
|
|
|
|
|
2017-11-03 15:20:10 +00:00
|
|
|
func TestCapabilities_DerivedPolicies(t *testing.T) {
|
|
|
|
var resp *logical.Response
|
|
|
|
var err error
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
ctx := namespace.RootContext(nil)
|
|
|
|
i, _, c := testIdentityStoreWithGithubAuth(ctx, t)
|
2017-11-03 15:20:10 +00:00
|
|
|
|
|
|
|
policy1 := `
|
|
|
|
name = "policy1"
|
|
|
|
path "secret/sample" {
|
|
|
|
capabilities = ["update", "create", "sudo"]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
policy2 := `
|
|
|
|
name = "policy2"
|
|
|
|
path "secret/sample" {
|
|
|
|
capabilities = ["read", "delete"]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
policy3 := `
|
|
|
|
name = "policy3"
|
|
|
|
path "secret/sample" {
|
|
|
|
capabilities = ["list", "list"]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
// Create the above policies
|
2018-09-18 03:03:00 +00:00
|
|
|
policy, _ := ParseACLPolicy(namespace.RootNamespace, policy1)
|
|
|
|
err = c.policyStore.SetPolicy(ctx, policy)
|
2017-11-03 15:20:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
policy, _ = ParseACLPolicy(namespace.RootNamespace, policy2)
|
|
|
|
err = c.policyStore.SetPolicy(ctx, policy)
|
2017-11-03 15:20:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
policy, _ = ParseACLPolicy(namespace.RootNamespace, policy3)
|
|
|
|
err = c.policyStore.SetPolicy(ctx, policy)
|
2017-11-03 15:20:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an entity and assign policy1 to it
|
|
|
|
entityReq := &logical.Request{
|
|
|
|
Path: "entity",
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"policies": "policy1",
|
|
|
|
},
|
|
|
|
}
|
2018-09-18 03:03:00 +00:00
|
|
|
resp, err = i.HandleRequest(ctx, entityReq)
|
2017-11-03 15:20:10 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("bad: resp: %#v\nerr: %#v\n", resp, err)
|
|
|
|
}
|
|
|
|
entityID := resp.Data["id"].(string)
|
|
|
|
|
|
|
|
// Create a token for the entity and assign policy2 on the token
|
2018-06-08 21:24:27 +00:00
|
|
|
ent := &logical.TokenEntry{
|
2017-11-03 15:20:10 +00:00
|
|
|
ID: "capabilitiestoken",
|
|
|
|
Path: "secret/sample",
|
|
|
|
Policies: []string{"policy2"},
|
|
|
|
EntityID: entityID,
|
2018-06-03 22:14:51 +00:00
|
|
|
TTL: time.Hour,
|
2017-11-03 15:20:10 +00:00
|
|
|
}
|
2018-06-03 22:14:51 +00:00
|
|
|
testMakeTokenDirectly(t, c.tokenStore, ent)
|
2017-11-03 15:20:10 +00:00
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
actual, err := c.Capabilities(ctx, "capabilitiestoken", "secret/sample")
|
2017-11-03 15:20:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
expected := []string{"create", "read", "sudo", "delete", "update"}
|
|
|
|
sort.Strings(actual)
|
|
|
|
sort.Strings(expected)
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: got\n%#v\nexpected\n%#v\n", actual, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a group and add the above created entity to it
|
|
|
|
groupReq := &logical.Request{
|
|
|
|
Path: "group",
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"member_entity_ids": []string{entityID},
|
|
|
|
"policies": "policy3",
|
|
|
|
},
|
|
|
|
}
|
2018-09-18 03:03:00 +00:00
|
|
|
resp, err = i.HandleRequest(ctx, groupReq)
|
2017-11-03 15:20:10 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("bad: resp: %#v\nerr: %#v\n", resp, err)
|
|
|
|
}
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
actual, err = c.Capabilities(namespace.RootContext(nil), "capabilitiestoken", "secret/sample")
|
2017-11-03 15:20:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
expected = []string{"create", "read", "sudo", "delete", "update", "list"}
|
|
|
|
sort.Strings(actual)
|
|
|
|
sort.Strings(expected)
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: got\n%#v\nexpected\n%#v\n", actual, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-04 18:18:59 +00:00
|
|
|
func TestCapabilities_TemplatedPolicies(t *testing.T) {
|
|
|
|
var resp *logical.Response
|
|
|
|
var err error
|
2018-09-18 03:03:00 +00:00
|
|
|
i, _, c := testIdentityStoreWithGithubAuth(namespace.RootContext(nil), t)
|
2018-09-04 18:18:59 +00:00
|
|
|
// Create an entity and assign policy1 to it
|
|
|
|
entityReq := &logical.Request{
|
|
|
|
Path: "entity",
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
}
|
2018-09-18 03:03:00 +00:00
|
|
|
resp, err = i.HandleRequest(namespace.RootContext(nil), entityReq)
|
2018-09-04 18:18:59 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("bad: resp: %#v\nerr: %#v\n", resp, err)
|
|
|
|
}
|
|
|
|
entityID := resp.Data["id"].(string)
|
|
|
|
|
|
|
|
// Create a token for the entity and assign policy2 on the token
|
|
|
|
ent := &logical.TokenEntry{
|
|
|
|
ID: "capabilitiestoken",
|
|
|
|
Path: "auth/token/create",
|
|
|
|
Policies: []string{"testpolicy"},
|
|
|
|
EntityID: entityID,
|
|
|
|
TTL: time.Hour,
|
|
|
|
}
|
|
|
|
testMakeTokenDirectly(t, c.tokenStore, ent)
|
|
|
|
|
|
|
|
tCases := []struct {
|
|
|
|
policy string
|
|
|
|
path string
|
|
|
|
expected []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
`name = "testpolicy"
|
|
|
|
path "secret/{{identity.entity.id}}/sample" {
|
|
|
|
capabilities = ["update", "create"]
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
fmt.Sprintf("secret/%s/sample", entityID),
|
|
|
|
[]string{"update", "create"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`{"name": "testpolicy", "path": {"secret/{{identity.entity.id}}/sample": {"capabilities": ["read", "create"]}}}`,
|
|
|
|
fmt.Sprintf("secret/%s/sample", entityID),
|
|
|
|
[]string{"read", "create"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`{"name": "testpolicy", "path": {"secret/sample": {"capabilities": ["read"]}}}`,
|
|
|
|
"secret/sample",
|
|
|
|
[]string{"read"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tCase := range tCases {
|
|
|
|
// Create the above policies
|
2018-09-18 03:03:00 +00:00
|
|
|
policy, err := ParseACLPolicy(namespace.RootNamespace, tCase.policy)
|
2018-09-04 18:18:59 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2018-09-18 03:03:00 +00:00
|
|
|
err = c.policyStore.SetPolicy(namespace.RootContext(nil), policy)
|
2018-09-04 18:18:59 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2018-09-18 03:03:00 +00:00
|
|
|
actual, err := c.Capabilities(namespace.RootContext(nil), "capabilitiestoken", tCase.path)
|
2018-09-04 18:18:59 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
sort.Strings(actual)
|
|
|
|
sort.Strings(tCase.expected)
|
|
|
|
if !reflect.DeepEqual(actual, tCase.expected) {
|
|
|
|
t.Fatalf("bad: got\n%#v\nexpected\n%#v\n", actual, tCase.expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-09 17:50:26 +00:00
|
|
|
func TestCapabilities(t *testing.T) {
|
2016-03-05 05:03:55 +00:00
|
|
|
c, _, token := TestCoreUnsealed(t)
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
actual, err := c.Capabilities(namespace.RootContext(nil), token, "path")
|
2016-03-05 05:03:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
expected := []string{"root"}
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: got\n%#v\nexpected\n%#v\n", actual, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a policy
|
2018-09-18 03:03:00 +00:00
|
|
|
policy, _ := ParseACLPolicy(namespace.RootNamespace, aclPolicy)
|
|
|
|
err = c.policyStore.SetPolicy(namespace.RootContext(nil), policy)
|
2016-03-05 05:03:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a token for the policy
|
2018-06-08 21:24:27 +00:00
|
|
|
ent := &logical.TokenEntry{
|
2016-03-05 05:03:55 +00:00
|
|
|
ID: "capabilitiestoken",
|
|
|
|
Path: "testpath",
|
|
|
|
Policies: []string{"dev"},
|
2018-06-03 22:14:51 +00:00
|
|
|
TTL: time.Hour,
|
2016-03-05 05:03:55 +00:00
|
|
|
}
|
2018-06-03 22:14:51 +00:00
|
|
|
testMakeTokenDirectly(t, c.tokenStore, ent)
|
2016-03-05 05:03:55 +00:00
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
actual, err = c.Capabilities(namespace.RootContext(nil), "capabilitiestoken", "foo/bar")
|
2016-03-05 05:03:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2016-03-18 16:55:18 +00:00
|
|
|
expected = []string{"create", "read", "sudo"}
|
2016-03-05 05:03:55 +00:00
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: got\n%#v\nexpected\n%#v\n", actual, expected)
|
|
|
|
}
|
|
|
|
}
|