2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-10-04 22:08:10 +00:00
|
|
|
package mock
|
2017-09-11 23:31:37 +00:00
|
|
|
|
|
|
|
import (
|
2023-03-16 13:50:20 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
2017-09-11 23:31:37 +00:00
|
|
|
"fmt"
|
2018-01-31 20:13:57 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2022-09-22 18:49:58 +00:00
|
|
|
"time"
|
2017-09-11 23:31:37 +00:00
|
|
|
|
2023-03-16 13:50:20 +00:00
|
|
|
"github.com/golang-jwt/jwt/v4"
|
2019-01-15 19:46:12 +00:00
|
|
|
testing "github.com/mitchellh/go-testing-interface"
|
2023-03-16 13:50:20 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-01-15 19:46:12 +00:00
|
|
|
|
2023-03-16 13:50:20 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
2017-09-11 23:31:37 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
2017-10-04 22:08:10 +00:00
|
|
|
// StateStore defines the methods required from state.StateStore but avoids a
|
|
|
|
// circular dependency.
|
|
|
|
type StateStore interface {
|
2020-12-01 16:11:34 +00:00
|
|
|
UpsertACLPolicies(msgType structs.MessageType, index uint64, policies []*structs.ACLPolicy) error
|
|
|
|
UpsertACLTokens(msgType structs.MessageType, index uint64, tokens []*structs.ACLToken) error
|
2017-10-04 22:08:10 +00:00
|
|
|
}
|
|
|
|
|
2017-09-11 23:31:37 +00:00
|
|
|
// NamespacePolicy is a helper for generating the policy hcl for a given
|
2018-03-11 18:29:24 +00:00
|
|
|
// namespace. Either policy or capabilities may be nil but not both.
|
2017-09-11 23:31:37 +00:00
|
|
|
func NamespacePolicy(namespace string, policy string, capabilities []string) string {
|
|
|
|
policyHCL := fmt.Sprintf("namespace %q {", namespace)
|
|
|
|
if policy != "" {
|
|
|
|
policyHCL += fmt.Sprintf("\n\tpolicy = %q", policy)
|
|
|
|
}
|
|
|
|
if len(capabilities) != 0 {
|
2018-01-31 20:13:57 +00:00
|
|
|
for i, s := range capabilities {
|
|
|
|
if !strings.HasPrefix(s, "\"") {
|
|
|
|
capabilities[i] = strconv.Quote(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
policyHCL += fmt.Sprintf("\n\tcapabilities = [%v]", strings.Join(capabilities, ","))
|
2017-09-11 23:31:37 +00:00
|
|
|
}
|
|
|
|
policyHCL += "\n}"
|
|
|
|
return policyHCL
|
|
|
|
}
|
|
|
|
|
2022-09-22 18:49:58 +00:00
|
|
|
// NamespacePolicyWithVariables is a helper for generating the policy hcl for a given
|
2022-06-27 19:51:01 +00:00
|
|
|
// namespace. Either policy or capabilities may be nil but not both.
|
2022-08-26 18:03:56 +00:00
|
|
|
func NamespacePolicyWithVariables(namespace string, policy string, capabilities []string, svars map[string][]string) string {
|
2022-06-27 19:51:01 +00:00
|
|
|
policyHCL := fmt.Sprintf("namespace %q {", namespace)
|
|
|
|
if policy != "" {
|
|
|
|
policyHCL += fmt.Sprintf("\n\tpolicy = %q", policy)
|
|
|
|
}
|
|
|
|
if len(capabilities) != 0 {
|
|
|
|
for i, s := range capabilities {
|
|
|
|
if !strings.HasPrefix(s, "\"") {
|
|
|
|
capabilities[i] = strconv.Quote(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
policyHCL += fmt.Sprintf("\n\tcapabilities = [%v]", strings.Join(capabilities, ","))
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
policyHCL += VariablePolicy(svars)
|
2022-06-27 19:51:01 +00:00
|
|
|
policyHCL += "\n}"
|
|
|
|
return policyHCL
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// VariablePolicy is a helper for generating the policy hcl for a given
|
|
|
|
// variable block inside of a namespace.
|
|
|
|
func VariablePolicy(svars map[string][]string) string {
|
2022-06-27 19:51:01 +00:00
|
|
|
policyHCL := ""
|
|
|
|
if len(svars) > 0 {
|
2022-08-26 18:03:56 +00:00
|
|
|
policyHCL = "\n\n\tvariables {"
|
2022-06-27 19:51:01 +00:00
|
|
|
for p, c := range svars {
|
|
|
|
for i, s := range c {
|
|
|
|
if !strings.HasPrefix(s, "\"") {
|
|
|
|
c[i] = strconv.Quote(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
policyHCL += fmt.Sprintf("\n\t\tpath %q { capabilities = [%v]}", p, strings.Join(c, ","))
|
|
|
|
}
|
|
|
|
policyHCL += "\n\t}"
|
|
|
|
}
|
|
|
|
return policyHCL
|
|
|
|
}
|
|
|
|
|
2019-07-25 14:32:19 +00:00
|
|
|
// HostVolumePolicy is a helper for generating the policy hcl for a given
|
|
|
|
// host-volume. Either policy or capabilities may be nil but not both.
|
|
|
|
func HostVolumePolicy(vol string, policy string, capabilities []string) string {
|
|
|
|
policyHCL := fmt.Sprintf("host_volume %q {", vol)
|
|
|
|
if policy != "" {
|
|
|
|
policyHCL += fmt.Sprintf("\n\tpolicy = %q", policy)
|
|
|
|
}
|
|
|
|
if len(capabilities) != 0 {
|
|
|
|
for i, s := range capabilities {
|
|
|
|
if !strings.HasPrefix(s, "\"") {
|
|
|
|
capabilities[i] = strconv.Quote(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
policyHCL += fmt.Sprintf("\n\tcapabilities = [%v]", strings.Join(capabilities, ","))
|
|
|
|
}
|
|
|
|
policyHCL += "\n}"
|
|
|
|
return policyHCL
|
|
|
|
}
|
|
|
|
|
2017-10-06 19:02:53 +00:00
|
|
|
// AgentPolicy is a helper for generating the hcl for a given agent policy.
|
|
|
|
func AgentPolicy(policy string) string {
|
|
|
|
return fmt.Sprintf("agent {\n\tpolicy = %q\n}\n", policy)
|
|
|
|
}
|
|
|
|
|
2017-09-15 03:33:12 +00:00
|
|
|
// NodePolicy is a helper for generating the hcl for a given node policy.
|
|
|
|
func NodePolicy(policy string) string {
|
|
|
|
return fmt.Sprintf("node {\n\tpolicy = %q\n}\n", policy)
|
|
|
|
}
|
|
|
|
|
2017-10-13 21:36:02 +00:00
|
|
|
// QuotaPolicy is a helper for generating the hcl for a given quota policy.
|
|
|
|
func QuotaPolicy(policy string) string {
|
|
|
|
return fmt.Sprintf("quota {\n\tpolicy = %q\n}\n", policy)
|
|
|
|
}
|
|
|
|
|
2020-03-17 21:32:39 +00:00
|
|
|
// PluginPolicy is a helper for generating the hcl for a given plugin policy.
|
|
|
|
func PluginPolicy(policy string) string {
|
|
|
|
return fmt.Sprintf("plugin {\n\tpolicy = %q\n}\n", policy)
|
|
|
|
}
|
|
|
|
|
2017-09-11 23:31:37 +00:00
|
|
|
// CreatePolicy creates a policy with the given name and rule.
|
2017-10-04 22:08:10 +00:00
|
|
|
func CreatePolicy(t testing.T, state StateStore, index uint64, name, rule string) {
|
2017-09-11 23:31:37 +00:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
// Create the ACLPolicy
|
|
|
|
policy := &structs.ACLPolicy{
|
|
|
|
Name: name,
|
|
|
|
Rules: rule,
|
|
|
|
}
|
|
|
|
policy.SetHash()
|
2020-12-01 16:11:34 +00:00
|
|
|
assert.Nil(t, state.UpsertACLPolicies(structs.MsgTypeTestSetup, index, []*structs.ACLPolicy{policy}))
|
2017-09-11 23:31:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateToken creates a local, client token for the given policies
|
2017-10-04 22:08:10 +00:00
|
|
|
func CreateToken(t testing.T, state StateStore, index uint64, policies []string) *structs.ACLToken {
|
2017-09-11 23:31:37 +00:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
// Create the ACLToken
|
2017-10-04 22:08:10 +00:00
|
|
|
token := ACLToken()
|
2017-09-11 23:31:37 +00:00
|
|
|
token.Policies = policies
|
|
|
|
token.SetHash()
|
2020-12-01 16:11:34 +00:00
|
|
|
assert.Nil(t, state.UpsertACLTokens(structs.MsgTypeTestSetup, index, []*structs.ACLToken{token}))
|
2017-09-11 23:31:37 +00:00
|
|
|
return token
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreatePolicyAndToken creates a policy and then returns a token configured for
|
|
|
|
// just that policy. CreatePolicyAndToken uses the given index and index+1.
|
2017-10-04 22:08:10 +00:00
|
|
|
func CreatePolicyAndToken(t testing.T, state StateStore, index uint64, name, rule string) *structs.ACLToken {
|
2017-09-11 23:31:37 +00:00
|
|
|
CreatePolicy(t, state, index, name, rule)
|
|
|
|
return CreateToken(t, state, index+1, []string{name})
|
|
|
|
}
|
2022-09-22 18:49:58 +00:00
|
|
|
|
|
|
|
func ACLRole() *structs.ACLRole {
|
|
|
|
role := structs.ACLRole{
|
|
|
|
ID: uuid.Generate(),
|
|
|
|
Name: fmt.Sprintf("acl-role-%s", uuid.Short()),
|
|
|
|
Description: "mocked-test-acl-role",
|
|
|
|
Policies: []*structs.ACLRolePolicyLink{
|
|
|
|
{Name: "mocked-test-policy-1"},
|
|
|
|
{Name: "mocked-test-policy-2"},
|
|
|
|
},
|
|
|
|
CreateIndex: 10,
|
|
|
|
ModifyIndex: 10,
|
|
|
|
}
|
|
|
|
role.SetHash()
|
|
|
|
return &role
|
|
|
|
}
|
|
|
|
|
|
|
|
func ACLPolicy() *structs.ACLPolicy {
|
|
|
|
ap := &structs.ACLPolicy{
|
|
|
|
Name: fmt.Sprintf("policy-%s", uuid.Generate()),
|
|
|
|
Description: "Super cool policy!",
|
|
|
|
Rules: `
|
|
|
|
namespace "default" {
|
|
|
|
policy = "write"
|
|
|
|
}
|
|
|
|
node {
|
|
|
|
policy = "read"
|
|
|
|
}
|
|
|
|
agent {
|
|
|
|
policy = "read"
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
CreateIndex: 10,
|
|
|
|
ModifyIndex: 20,
|
|
|
|
}
|
|
|
|
ap.SetHash()
|
|
|
|
return ap
|
|
|
|
}
|
|
|
|
|
|
|
|
func ACLToken() *structs.ACLToken {
|
|
|
|
tk := &structs.ACLToken{
|
|
|
|
AccessorID: uuid.Generate(),
|
|
|
|
SecretID: uuid.Generate(),
|
|
|
|
Name: "my cool token " + uuid.Generate(),
|
|
|
|
Type: "client",
|
|
|
|
Policies: []string{"foo", "bar"},
|
|
|
|
Global: false,
|
|
|
|
CreateTime: time.Now().UTC(),
|
|
|
|
CreateIndex: 10,
|
|
|
|
ModifyIndex: 20,
|
|
|
|
}
|
|
|
|
tk.SetHash()
|
|
|
|
return tk
|
|
|
|
}
|
|
|
|
|
|
|
|
func ACLManagementToken() *structs.ACLToken {
|
|
|
|
return &structs.ACLToken{
|
|
|
|
AccessorID: uuid.Generate(),
|
|
|
|
SecretID: uuid.Generate(),
|
|
|
|
Name: "management " + uuid.Generate(),
|
|
|
|
Type: "management",
|
|
|
|
Global: true,
|
|
|
|
CreateTime: time.Now().UTC(),
|
|
|
|
CreateIndex: 10,
|
|
|
|
ModifyIndex: 20,
|
|
|
|
}
|
|
|
|
}
|
2022-11-10 18:42:41 +00:00
|
|
|
|
2023-03-16 13:50:20 +00:00
|
|
|
func ACLOIDCAuthMethod() *structs.ACLAuthMethod {
|
2022-11-21 09:15:39 +00:00
|
|
|
maxTokenTTL, _ := time.ParseDuration("3600s")
|
2022-11-10 18:42:41 +00:00
|
|
|
method := structs.ACLAuthMethod{
|
|
|
|
Name: fmt.Sprintf("acl-auth-method-%s", uuid.Short()),
|
2022-11-21 09:15:39 +00:00
|
|
|
Type: "OIDC",
|
|
|
|
TokenLocality: "local",
|
|
|
|
MaxTokenTTL: maxTokenTTL,
|
2022-12-09 13:46:54 +00:00
|
|
|
Default: false,
|
2022-11-10 18:42:41 +00:00
|
|
|
Config: &structs.ACLAuthMethodConfig{
|
|
|
|
OIDCDiscoveryURL: "http://example.com",
|
|
|
|
OIDCClientID: "mock",
|
|
|
|
OIDCClientSecret: "very secret secret",
|
2023-01-13 13:14:29 +00:00
|
|
|
OIDCScopes: []string{"groups"},
|
2023-03-16 13:50:20 +00:00
|
|
|
BoundAudiences: []string{"sales", "engineering"},
|
2022-11-10 18:42:41 +00:00
|
|
|
AllowedRedirectURIs: []string{"foo", "bar"},
|
|
|
|
DiscoveryCaPem: []string{"foo"},
|
2023-03-16 13:50:20 +00:00
|
|
|
SigningAlgs: []string{"RS256"},
|
2022-11-10 18:42:41 +00:00
|
|
|
ClaimMappings: map[string]string{"foo": "bar"},
|
|
|
|
ListClaimMappings: map[string]string{"foo": "bar"},
|
|
|
|
},
|
|
|
|
CreateTime: time.Now().UTC(),
|
|
|
|
CreateIndex: 10,
|
|
|
|
ModifyIndex: 10,
|
|
|
|
}
|
2022-12-08 13:05:46 +00:00
|
|
|
method.Canonicalize()
|
2023-03-17 14:37:54 +00:00
|
|
|
method.SetHash()
|
2022-11-10 18:42:41 +00:00
|
|
|
return &method
|
|
|
|
}
|
2022-12-14 07:48:18 +00:00
|
|
|
|
2023-03-16 13:50:20 +00:00
|
|
|
func ACLJWTAuthMethod() *structs.ACLAuthMethod {
|
|
|
|
maxTokenTTL, _ := time.ParseDuration("3600s")
|
|
|
|
method := structs.ACLAuthMethod{
|
|
|
|
Name: fmt.Sprintf("acl-auth-method-%s", uuid.Short()),
|
|
|
|
Type: "JWT",
|
|
|
|
TokenLocality: "local",
|
|
|
|
MaxTokenTTL: maxTokenTTL,
|
|
|
|
Default: false,
|
|
|
|
Config: &structs.ACLAuthMethodConfig{
|
|
|
|
JWTValidationPubKeys: []string{},
|
|
|
|
OIDCDiscoveryURL: "http://example.com",
|
|
|
|
BoundAudiences: []string{"sales", "engineering"},
|
|
|
|
DiscoveryCaPem: []string{"foo"},
|
|
|
|
SigningAlgs: []string{"RS256"},
|
|
|
|
ClaimMappings: map[string]string{"foo": "bar"},
|
|
|
|
ListClaimMappings: map[string]string{"foo": "bar"},
|
|
|
|
},
|
|
|
|
CreateTime: time.Now().UTC(),
|
|
|
|
CreateIndex: 10,
|
|
|
|
ModifyIndex: 10,
|
|
|
|
}
|
|
|
|
method.Canonicalize()
|
2023-03-17 14:14:51 +00:00
|
|
|
method.SetHash()
|
2023-03-16 13:50:20 +00:00
|
|
|
return &method
|
|
|
|
}
|
|
|
|
|
|
|
|
// SampleJWTokenWithKeys takes a set of claims (can be nil) and optionally
|
|
|
|
// a private RSA key that should be used for signing the JWT, and returns:
|
|
|
|
// - a JWT signed with a randomly generated RSA key
|
|
|
|
// - PEM string of the public part of that key that can be used for validation.
|
|
|
|
func SampleJWTokenWithKeys(claims jwt.Claims, rsaKey *rsa.PrivateKey) (string, string, error) {
|
|
|
|
var token, pubkeyPem string
|
|
|
|
|
|
|
|
if rsaKey == nil {
|
|
|
|
var err error
|
|
|
|
rsaKey, err = rsa.GenerateKey(rand.Reader, 4096)
|
|
|
|
if err != nil {
|
|
|
|
return token, pubkeyPem, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pubkeyBytes, err := x509.MarshalPKIXPublicKey(rsaKey.Public())
|
|
|
|
if err != nil {
|
|
|
|
return token, pubkeyPem, err
|
|
|
|
}
|
|
|
|
pubkeyPem = string(pem.EncodeToMemory(
|
|
|
|
&pem.Block{
|
|
|
|
Type: "RSA PUBLIC KEY",
|
|
|
|
Bytes: pubkeyBytes,
|
|
|
|
},
|
|
|
|
))
|
|
|
|
|
|
|
|
var rawToken *jwt.Token
|
|
|
|
if claims != nil {
|
|
|
|
rawToken = jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
|
|
|
|
} else {
|
|
|
|
rawToken = jwt.New(jwt.SigningMethodRS256)
|
|
|
|
}
|
|
|
|
|
|
|
|
token, err = rawToken.SignedString(rsaKey)
|
|
|
|
if err != nil {
|
|
|
|
return token, pubkeyPem, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return token, pubkeyPem, nil
|
|
|
|
}
|
|
|
|
|
2022-12-14 07:48:18 +00:00
|
|
|
func ACLBindingRule() *structs.ACLBindingRule {
|
|
|
|
return &structs.ACLBindingRule{
|
|
|
|
ID: uuid.Short(),
|
|
|
|
Description: "mocked-acl-binding-rule",
|
|
|
|
AuthMethod: "auth0",
|
|
|
|
Selector: "engineering in list.roles",
|
|
|
|
BindType: "role",
|
|
|
|
BindName: "eng-ro",
|
|
|
|
CreateTime: time.Now().UTC(),
|
|
|
|
ModifyTime: time.Now().UTC(),
|
|
|
|
CreateIndex: 10,
|
|
|
|
ModifyIndex: 10,
|
|
|
|
}
|
|
|
|
}
|