2023-03-28 20:12:41 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2022-03-11 02:48:27 +00:00
|
|
|
package acl
|
|
|
|
|
|
|
|
import (
|
2022-03-25 19:34:59 +00:00
|
|
|
"fmt"
|
2022-03-11 02:48:27 +00:00
|
|
|
"regexp"
|
|
|
|
"testing"
|
2023-01-09 18:28:53 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-03-11 02:48:27 +00:00
|
|
|
)
|
|
|
|
|
2022-03-18 17:32:25 +00:00
|
|
|
func RequirePermissionDeniedError(t testing.TB, err error, authz Authorizer, _ *AuthorizerContext, resource Resource, accessLevel AccessLevel, resourceID string) {
|
2022-03-11 02:48:27 +00:00
|
|
|
t.Helper()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("An error is expected but got nil.")
|
|
|
|
}
|
|
|
|
if v, ok := err.(PermissionDeniedError); ok {
|
|
|
|
require.Equal(t, v.Resource, resource)
|
|
|
|
require.Equal(t, v.AccessLevel, accessLevel)
|
|
|
|
require.Equal(t, v.ResourceID.Name, resourceID)
|
|
|
|
} else {
|
|
|
|
t.Fatalf("Expected a permission denied error got %T %vp", err, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-18 17:32:25 +00:00
|
|
|
func RequirePermissionDeniedMessage(t testing.TB, msg string, authz interface{}, _ *AuthorizerContext, resource Resource, accessLevel AccessLevel, resourceID string) {
|
2022-03-11 02:48:27 +00:00
|
|
|
require.NotEmpty(t, msg, "expected non-empty error message")
|
|
|
|
|
2022-03-25 19:34:59 +00:00
|
|
|
baseRegex := ` lacks permission '(\S*):(\S*)' on \"([^\"]*)\"(?: in partition \"([^\"]*)\" in namespace \"([^\"]*)\")?\s*$`
|
|
|
|
|
2022-03-11 02:48:27 +00:00
|
|
|
var resourceIDFound string
|
2022-03-18 17:32:25 +00:00
|
|
|
if authz == nil {
|
2022-03-25 19:34:59 +00:00
|
|
|
expr := "^Permission denied" + `: provided token` + baseRegex
|
2022-03-11 02:48:27 +00:00
|
|
|
re, _ := regexp.Compile(expr)
|
|
|
|
matched := re.FindStringSubmatch(msg)
|
|
|
|
|
2022-03-25 19:34:59 +00:00
|
|
|
require.NotNil(t, matched, fmt.Sprintf("RE %q didn't match %q", expr, msg))
|
2022-03-11 02:48:27 +00:00
|
|
|
require.Equal(t, string(resource), matched[1], "resource")
|
|
|
|
require.Equal(t, accessLevel.String(), matched[2], "access level")
|
|
|
|
resourceIDFound = matched[3]
|
|
|
|
} else {
|
2022-03-25 19:34:59 +00:00
|
|
|
expr := "^Permission denied" + `: token with AccessorID '(\S*)'` + baseRegex
|
2022-03-11 02:48:27 +00:00
|
|
|
re, _ := regexp.Compile(expr)
|
|
|
|
matched := re.FindStringSubmatch(msg)
|
|
|
|
|
2022-03-25 19:34:59 +00:00
|
|
|
require.NotNil(t, matched, fmt.Sprintf("RE %q didn't match %q", expr, msg))
|
2022-03-18 17:32:25 +00:00
|
|
|
require.Equal(t, extractAccessorID(authz), matched[1], "auth")
|
2022-03-11 02:48:27 +00:00
|
|
|
require.Equal(t, string(resource), matched[2], "resource")
|
|
|
|
require.Equal(t, accessLevel.String(), matched[3], "access level")
|
|
|
|
resourceIDFound = matched[4]
|
|
|
|
}
|
|
|
|
// AuthorizerContext information should be checked here
|
|
|
|
require.Contains(t, resourceIDFound, resourceID, "resource id")
|
|
|
|
}
|