Improve grant validation (#3081)

This adds several relationships into the resource package that are then
made use of to improve the grant validation that runs during Parse. By
understanding mappings between ID and resource type the validation
checks can far better align with actual allowed grant formats so we can
find several common misunderstandings before they simply result in an
authorization failure.

In the future some of these functions could be used in the logic in the
actual ACL checks but it's not currently necessary and I don't want to
lump that change in with this.

Note that the tests have not meaningfully changed, but they _have_ had
to be updated because the stricter validation is actually catching
issues.
This commit is contained in:
Jeff Mitchell authored and GitHub committed 2023-03-16 10:11:55 -04:00
1 parent ef37369c2b
commit fc664eb35f
12 files changed
+380 -222

No files matched your search

+45 -26
View File
@@ -3,7 +3,11 @@
package globals
import "strings"
import (
"strings"
"github.com/hashicorp/boundary/internal/types/resource"
)
// This set of consts is intended to be a place to collect commonly-used
// prefixes. This can avoid some cross-package dependency issues. Unlike the
@@ -109,33 +113,48 @@ const (
WorkerPrefix = "w"
)
var topLevelPrefixes = map[string]bool{
AuthTokenPrefix: true,
PasswordAuthMethodPrefix: true,
OidcAuthMethodPrefix: true,
ProjectPrefix: true,
OrgPrefix: true,
UserPrefix: true,
GroupPrefix: true,
RolePrefix: true,
StaticCredentialStorePrefix: true,
StaticCredentialStorePreviousPrefix: true,
VaultCredentialStorePrefix: true,
StaticHostCatalogPrefix: true,
PluginHostCatalogPrefix: true,
PluginHostCatalogPreviousPrefix: true,
SessionPrefix: true,
TcpTargetPrefix: true,
SshTargetPrefix: true,
WorkerPrefix: true,
var prefixToResourceType = map[string]resource.Type{
AuthTokenPrefix: resource.AuthToken,
PasswordAuthMethodPrefix: resource.AuthMethod,
PasswordAccountPrefix: resource.Account,
PasswordAccountPreviousPrefix: resource.Account,
OidcAuthMethodPrefix: resource.AuthMethod,
OidcAccountPrefix: resource.Account,
OidcManagedGroupPrefix: resource.ManagedGroup,
GlobalPrefix: resource.Scope,
ProjectPrefix: resource.Scope,
OrgPrefix: resource.Scope,
UserPrefix: resource.User,
GroupPrefix: resource.Group,
RolePrefix: resource.Role,
StaticCredentialStorePrefix: resource.CredentialStore,
StaticCredentialStorePreviousPrefix: resource.CredentialStore,
VaultCredentialStorePrefix: resource.CredentialStore,
VaultCredentialLibraryPrefix: resource.CredentialLibrary,
VaultSshCertificateCredentialLibraryPrefix: resource.CredentialLibrary,
UsernamePasswordCredentialPrefix: resource.Credential,
UsernamePasswordCredentialPreviousPrefix: resource.Credential,
SshPrivateKeyCredentialPrefix: resource.Credential,
JsonCredentialPrefix: resource.Credential,
StaticHostCatalogPrefix: resource.HostCatalog,
StaticHostSetPrefix: resource.HostSet,
StaticHostPrefix: resource.Host,
PluginHostCatalogPrefix: resource.HostCatalog,
PluginHostCatalogPreviousPrefix: resource.HostCatalog,
PluginHostSetPrefix: resource.HostSet,
PluginHostSetPreviousPrefix: resource.HostSet,
PluginHostPrefix: resource.Host,
PluginHostPreviousPrefix: resource.Host,
SessionPrefix: resource.Session,
TcpTargetPrefix: resource.Target,
SshTargetPrefix: resource.Target,
WorkerPrefix: resource.Worker,
}
// IsTopLevelResourcePrefix takes in a resource ID (or a prefix) and indicates
// via the result whether the ID/prefix corresponds to a top-level resource;
// that is, one (like auth method) that does not have a parent (like account or
// managed group)
func IsTopLevelResourcePrefix(in string) bool {
// ResourceTypeFromPrefix takes in a resource ID (or a prefix) and returns the
// corresponding resource typ
func ResourceTypeFromPrefix(in string) resource.Type {
// If full ID, trim to just prefix
in, _, _ = strings.Cut(in, "_")
return topLevelPrefixes[in]
return prefixToResourceType[in]
}
+12 -12
View File
@@ -7,22 +7,22 @@ import (
"fmt"
"testing"
"github.com/hashicorp/boundary/internal/types/resource"
"github.com/stretchr/testify/assert"
)
func TestIsTopLevelResourcePrefix(t *testing.T) {
func TestResourceTypeFromPrefix(t *testing.T) {
// Test a random sampling
topLevel := []string{VaultCredentialStorePrefix, StaticCredentialStorePrefix, GroupPrefix, TcpTargetPrefix}
child := []string{VaultCredentialLibraryPrefix, OidcManagedGroupPrefix, StaticHostSetPrefix, JsonCredentialPrefix}
for _, prefix := range topLevel {
assert.True(t, IsTopLevelResourcePrefix(prefix))
assert.True(t, IsTopLevelResourcePrefix(fmt.Sprintf("%s_foobar", prefix)))
assert.False(t, IsTopLevelResourcePrefix(fmt.Sprintf("%sfoobar", prefix)))
vals := map[string]resource.Type{
VaultCredentialLibraryPrefix: resource.CredentialLibrary,
OidcManagedGroupPrefix: resource.ManagedGroup,
StaticHostSetPrefix: resource.HostSet,
JsonCredentialPrefix: resource.Credential,
}
for _, prefix := range child {
assert.False(t, IsTopLevelResourcePrefix(prefix))
assert.False(t, IsTopLevelResourcePrefix(fmt.Sprintf("%s_foobar", prefix)))
assert.False(t, IsTopLevelResourcePrefix(fmt.Sprintf("%sfoobar", prefix)))
for prefix, typ := range vals {
assert.Equal(t, typ, ResourceTypeFromPrefix(prefix))
assert.Equal(t, typ, ResourceTypeFromPrefix(fmt.Sprintf("%s_foobar", prefix)))
assert.Equal(t, resource.Unknown, ResourceTypeFromPrefix(fmt.Sprintf("%sfoobar", prefix)))
}
}