2023-04-20 12:40:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-04-20 12:40:22 +00:00
|
|
|
|
2023-04-06 09:40:04 +00:00
|
|
|
// Package demo includes fake resource types for working on Consul's generic
|
|
|
|
// state storage without having to refer to specific features.
|
|
|
|
package demo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-04-11 11:55:32 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
2023-04-06 09:40:04 +00:00
|
|
|
"google.golang.org/protobuf/types/known/anypb"
|
|
|
|
|
2023-04-11 11:10:14 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2023-04-06 09:40:04 +00:00
|
|
|
"github.com/hashicorp/consul/internal/resource"
|
|
|
|
"github.com/hashicorp/consul/proto-public/pbresource"
|
|
|
|
pbdemov1 "github.com/hashicorp/consul/proto/private/pbdemo/v1"
|
|
|
|
pbdemov2 "github.com/hashicorp/consul/proto/private/pbdemo/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-08-07 21:37:03 +00:00
|
|
|
// TypeV1RecordLabel represents a record label which artists are signed to.
|
|
|
|
// Used specifically as a resource to test partition only scoped resources.
|
|
|
|
TypeV1RecordLabel = &pbresource.Type{
|
|
|
|
Group: "demo",
|
|
|
|
GroupVersion: "v1",
|
|
|
|
Kind: "RecordLabel",
|
2023-04-06 09:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TypeV1Artist represents a musician or group of musicians.
|
|
|
|
TypeV1Artist = &pbresource.Type{
|
|
|
|
Group: "demo",
|
|
|
|
GroupVersion: "v1",
|
2023-06-26 12:25:14 +00:00
|
|
|
Kind: "Artist",
|
2023-04-06 09:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TypeV1Album represents a collection of an artist's songs.
|
|
|
|
TypeV1Album = &pbresource.Type{
|
|
|
|
Group: "demo",
|
|
|
|
GroupVersion: "v1",
|
2023-06-26 12:25:14 +00:00
|
|
|
Kind: "Album",
|
2023-04-06 09:40:04 +00:00
|
|
|
}
|
|
|
|
|
2023-09-15 19:00:23 +00:00
|
|
|
// TypeV1Concept represents an abstract concept that can be associated with any other resource.
|
|
|
|
TypeV1Concept = &pbresource.Type{
|
|
|
|
Group: "demo",
|
|
|
|
GroupVersion: "v1",
|
|
|
|
Kind: "Concept",
|
|
|
|
}
|
|
|
|
|
2023-04-06 09:40:04 +00:00
|
|
|
// TypeV2Artist represents a musician or group of musicians.
|
|
|
|
TypeV2Artist = &pbresource.Type{
|
|
|
|
Group: "demo",
|
|
|
|
GroupVersion: "v2",
|
2023-06-26 12:25:14 +00:00
|
|
|
Kind: "Artist",
|
2023-04-06 09:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TypeV2Album represents a collection of an artist's songs.
|
|
|
|
TypeV2Album = &pbresource.Type{
|
|
|
|
Group: "demo",
|
|
|
|
GroupVersion: "v2",
|
2023-06-26 12:25:14 +00:00
|
|
|
Kind: "Album",
|
2023-04-06 09:40:04 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2023-04-11 11:10:14 +00:00
|
|
|
const (
|
2023-06-26 12:25:14 +00:00
|
|
|
ArtistV1ReadPolicy = `key_prefix "resource/demo.v1.Artist/" { policy = "read" }`
|
|
|
|
ArtistV1WritePolicy = `key_prefix "resource/demo.v1.Artist/" { policy = "write" }`
|
|
|
|
ArtistV2ReadPolicy = `key_prefix "resource/demo.v2.Artist/" { policy = "read" }`
|
|
|
|
ArtistV2WritePolicy = `key_prefix "resource/demo.v2.Artist/" { policy = "write" }`
|
2023-04-11 11:10:14 +00:00
|
|
|
ArtistV2ListPolicy = `key_prefix "resource/" { policy = "list" }`
|
2023-09-22 14:53:55 +00:00
|
|
|
LabelV1ReadPolicy = `key_prefix "resource/demo.v1.Label/" { policy = "read" }`
|
|
|
|
LabelV1WritePolicy = `key_prefix "resource/demo.v1.Label/" { policy = "write" }`
|
2023-04-11 11:10:14 +00:00
|
|
|
)
|
|
|
|
|
2023-04-25 11:52:35 +00:00
|
|
|
// RegisterTypes registers the demo types. Should only be called in tests and
|
|
|
|
// dev mode.
|
2023-04-11 11:10:14 +00:00
|
|
|
//
|
|
|
|
// TODO(spatel): We're standing-in key ACLs for demo resources until our ACL
|
|
|
|
// system can be more modularly extended (or support generic resource permissions).
|
2023-04-25 11:52:35 +00:00
|
|
|
func RegisterTypes(r resource.Registry) {
|
2023-09-22 14:53:55 +00:00
|
|
|
readACL := func(authz acl.Authorizer, authzContext *acl.AuthorizerContext, id *pbresource.ID, res *pbresource.Resource) error {
|
|
|
|
if resource.EqualType(TypeV1RecordLabel, id.Type) {
|
|
|
|
if res == nil {
|
|
|
|
return resource.ErrNeedData
|
|
|
|
}
|
|
|
|
}
|
2023-04-11 11:10:14 +00:00
|
|
|
key := fmt.Sprintf("resource/%s/%s", resource.ToGVK(id.Type), id.Name)
|
2023-08-07 21:37:03 +00:00
|
|
|
return authz.ToAllowAuthorizer().KeyReadAllowed(key, authzContext)
|
2023-04-11 11:10:14 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 14:53:38 +00:00
|
|
|
writeACL := func(authz acl.Authorizer, authzContext *acl.AuthorizerContext, res *pbresource.Resource) error {
|
2023-07-20 17:06:29 +00:00
|
|
|
key := fmt.Sprintf("resource/%s/%s", resource.ToGVK(res.Id.Type), res.Id.Name)
|
2023-08-15 21:57:59 +00:00
|
|
|
return authz.ToAllowAuthorizer().KeyWriteAllowed(key, authzContext)
|
2023-04-11 11:10:14 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 21:57:59 +00:00
|
|
|
makeListACL := func(typ *pbresource.Type) func(acl.Authorizer, *acl.AuthorizerContext) error {
|
|
|
|
return func(authz acl.Authorizer, authzContext *acl.AuthorizerContext) error {
|
2023-04-11 11:10:14 +00:00
|
|
|
key := fmt.Sprintf("resource/%s", resource.ToGVK(typ))
|
2023-08-15 21:57:59 +00:00
|
|
|
return authz.ToAllowAuthorizer().KeyListAllowed(key, authzContext)
|
2023-04-11 11:10:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-11 11:55:32 +00:00
|
|
|
validateV1ArtistFn := func(res *pbresource.Resource) error {
|
|
|
|
artist := &pbdemov1.Artist{}
|
|
|
|
if err := anypb.UnmarshalTo(res.Data, artist, proto.UnmarshalOptions{}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if artist.Name == "" {
|
|
|
|
return fmt.Errorf("artist.name required")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
validateV2ArtistFn := func(res *pbresource.Resource) error {
|
|
|
|
artist := &pbdemov2.Artist{}
|
|
|
|
if err := anypb.UnmarshalTo(res.Data, artist, proto.UnmarshalOptions{}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if artist.Name == "" {
|
|
|
|
return fmt.Errorf("artist.name required")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-12 21:50:07 +00:00
|
|
|
mutateV2ArtistFn := func(res *pbresource.Resource) error {
|
|
|
|
// Not a realistic use for this hook, but set genre if not specified
|
|
|
|
artist := &pbdemov2.Artist{}
|
|
|
|
if err := anypb.UnmarshalTo(res.Data, artist, proto.UnmarshalOptions{}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if artist.Genre == pbdemov2.Genre_GENRE_UNSPECIFIED {
|
|
|
|
artist.Genre = pbdemov2.Genre_GENRE_DISCO
|
|
|
|
return res.Data.MarshalFrom(artist)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-07 21:37:03 +00:00
|
|
|
r.Register(resource.Registration{
|
|
|
|
Type: TypeV1RecordLabel,
|
|
|
|
Proto: &pbdemov1.RecordLabel{},
|
2023-09-01 14:44:53 +00:00
|
|
|
Scope: resource.ScopePartition,
|
2023-08-07 21:37:03 +00:00
|
|
|
ACLs: &resource.ACLHooks{
|
|
|
|
Read: readACL,
|
|
|
|
Write: writeACL,
|
|
|
|
List: makeListACL(TypeV1RecordLabel),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2023-04-06 09:40:04 +00:00
|
|
|
r.Register(resource.Registration{
|
|
|
|
Type: TypeV1Artist,
|
|
|
|
Proto: &pbdemov1.Artist{},
|
2023-09-01 14:44:53 +00:00
|
|
|
Scope: resource.ScopeNamespace,
|
2023-04-11 11:10:14 +00:00
|
|
|
ACLs: &resource.ACLHooks{
|
|
|
|
Read: readACL,
|
|
|
|
Write: writeACL,
|
|
|
|
List: makeListACL(TypeV1Artist),
|
|
|
|
},
|
2023-04-11 11:55:32 +00:00
|
|
|
Validate: validateV1ArtistFn,
|
2023-04-06 09:40:04 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
r.Register(resource.Registration{
|
|
|
|
Type: TypeV1Album,
|
|
|
|
Proto: &pbdemov1.Album{},
|
2023-09-01 14:44:53 +00:00
|
|
|
Scope: resource.ScopeNamespace,
|
2023-04-11 11:10:14 +00:00
|
|
|
ACLs: &resource.ACLHooks{
|
|
|
|
Read: readACL,
|
|
|
|
Write: writeACL,
|
|
|
|
List: makeListACL(TypeV1Album),
|
|
|
|
},
|
2023-04-06 09:40:04 +00:00
|
|
|
})
|
|
|
|
|
2023-09-15 19:00:23 +00:00
|
|
|
r.Register(resource.Registration{
|
|
|
|
Type: TypeV1Concept,
|
|
|
|
Proto: &pbdemov1.Concept{},
|
|
|
|
Scope: resource.ScopeNamespace,
|
|
|
|
ACLs: &resource.ACLHooks{
|
|
|
|
Read: readACL,
|
|
|
|
Write: writeACL,
|
|
|
|
List: makeListACL(TypeV1Concept),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2023-04-06 09:40:04 +00:00
|
|
|
r.Register(resource.Registration{
|
|
|
|
Type: TypeV2Artist,
|
|
|
|
Proto: &pbdemov2.Artist{},
|
2023-09-01 14:44:53 +00:00
|
|
|
Scope: resource.ScopeNamespace,
|
2023-04-11 11:10:14 +00:00
|
|
|
ACLs: &resource.ACLHooks{
|
|
|
|
Read: readACL,
|
|
|
|
Write: writeACL,
|
|
|
|
List: makeListACL(TypeV2Artist),
|
|
|
|
},
|
2023-04-11 11:55:32 +00:00
|
|
|
Validate: validateV2ArtistFn,
|
2023-04-12 21:50:07 +00:00
|
|
|
Mutate: mutateV2ArtistFn,
|
2023-04-06 09:40:04 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
r.Register(resource.Registration{
|
|
|
|
Type: TypeV2Album,
|
|
|
|
Proto: &pbdemov2.Album{},
|
2023-09-01 14:44:53 +00:00
|
|
|
Scope: resource.ScopeNamespace,
|
2023-04-11 11:10:14 +00:00
|
|
|
ACLs: &resource.ACLHooks{
|
|
|
|
Read: readACL,
|
|
|
|
Write: writeACL,
|
|
|
|
List: makeListACL(TypeV2Album),
|
|
|
|
},
|
2023-04-06 09:40:04 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-08-07 21:37:03 +00:00
|
|
|
// GenerateV1RecordLabel generates a named RecordLabel resource.
|
|
|
|
func GenerateV1RecordLabel(name string) (*pbresource.Resource, error) {
|
|
|
|
data, err := anypb.New(&pbdemov1.RecordLabel{Name: name})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pbresource.Resource{
|
|
|
|
Id: &pbresource.ID{
|
2023-08-10 14:53:38 +00:00
|
|
|
Type: TypeV1RecordLabel,
|
|
|
|
Tenancy: resource.DefaultPartitionedTenancy(),
|
|
|
|
Name: name,
|
2023-08-07 21:37:03 +00:00
|
|
|
},
|
|
|
|
Data: data,
|
|
|
|
Metadata: map[string]string{
|
2023-09-15 19:00:23 +00:00
|
|
|
"generated_at": time.Now().Format(time.RFC3339),
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateV1Concept generates a named concept resource.
|
|
|
|
func GenerateV1Concept(name string) (*pbresource.Resource, error) {
|
|
|
|
return &pbresource.Resource{
|
|
|
|
Id: &pbresource.ID{
|
|
|
|
Type: TypeV1Concept,
|
|
|
|
Tenancy: resource.DefaultPartitionedTenancy(),
|
|
|
|
Name: name,
|
|
|
|
},
|
|
|
|
Data: nil,
|
|
|
|
Metadata: map[string]string{
|
2023-08-07 21:37:03 +00:00
|
|
|
"generated_at": time.Now().Format(time.RFC3339),
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-04-06 09:40:04 +00:00
|
|
|
// GenerateV2Artist generates a random Artist resource.
|
|
|
|
func GenerateV2Artist() (*pbresource.Resource, error) {
|
|
|
|
adjective := adjectives[rand.Intn(len(adjectives))]
|
|
|
|
noun := nouns[rand.Intn(len(nouns))]
|
|
|
|
|
|
|
|
numMembers := rand.Intn(5) + 1
|
|
|
|
groupMembers := make(map[string]string, numMembers)
|
|
|
|
for i := 0; i < numMembers; i++ {
|
|
|
|
groupMembers[members[rand.Intn(len(members))]] = instruments[rand.Intn(len(instruments))]
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := anypb.New(&pbdemov2.Artist{
|
|
|
|
Name: fmt.Sprintf("%s %s", adjective, noun),
|
|
|
|
Genre: randomGenre(),
|
|
|
|
GroupMembers: groupMembers,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pbresource.Resource{
|
|
|
|
Id: &pbresource.ID{
|
|
|
|
Type: TypeV2Artist,
|
2023-08-10 14:53:38 +00:00
|
|
|
Tenancy: resource.DefaultNamespacedTenancy(),
|
2023-04-06 09:40:04 +00:00
|
|
|
Name: fmt.Sprintf("%s-%s", strings.ToLower(adjective), strings.ToLower(noun)),
|
|
|
|
},
|
|
|
|
Data: data,
|
|
|
|
Metadata: map[string]string{
|
|
|
|
"generated_at": time.Now().Format(time.RFC3339),
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateV2Album generates a random Album resource, owned by the Artist with
|
|
|
|
// the given ID.
|
|
|
|
func GenerateV2Album(artistID *pbresource.ID) (*pbresource.Resource, error) {
|
2023-05-09 14:25:55 +00:00
|
|
|
return generateV2Album(artistID, rand.New(rand.NewSource(time.Now().UnixNano())))
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateV2Album(artistID *pbresource.ID, rand *rand.Rand) (*pbresource.Resource, error) {
|
2023-04-06 09:40:04 +00:00
|
|
|
adjective := adjectives[rand.Intn(len(adjectives))]
|
|
|
|
noun := nouns[rand.Intn(len(nouns))]
|
|
|
|
|
|
|
|
numTracks := 3 + rand.Intn(3)
|
|
|
|
tracks := make([]string, numTracks)
|
|
|
|
for i := 0; i < numTracks; i++ {
|
|
|
|
words := nouns
|
|
|
|
if i%3 == 0 {
|
|
|
|
words = adjectives
|
|
|
|
}
|
|
|
|
tracks[i] = words[rand.Intn(len(words))]
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := anypb.New(&pbdemov2.Album{
|
|
|
|
Title: fmt.Sprintf("%s %s", adjective, noun),
|
|
|
|
YearOfRelease: int32(1990 + rand.Intn(time.Now().Year()-1990)),
|
|
|
|
CriticallyAclaimed: rand.Int()%2 == 0,
|
|
|
|
Tracks: tracks,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pbresource.Resource{
|
|
|
|
Id: &pbresource.ID{
|
|
|
|
Type: TypeV2Album,
|
2023-08-10 14:53:38 +00:00
|
|
|
Tenancy: clone(artistID.Tenancy),
|
2023-04-06 09:40:04 +00:00
|
|
|
Name: fmt.Sprintf("%s/%s-%s", artistID.Name, strings.ToLower(adjective), strings.ToLower(noun)),
|
|
|
|
},
|
|
|
|
Owner: artistID,
|
|
|
|
Data: data,
|
|
|
|
Metadata: map[string]string{
|
|
|
|
"generated_at": time.Now().Format(time.RFC3339),
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func randomGenre() pbdemov2.Genre {
|
|
|
|
return pbdemov2.Genre(rand.Intn(len(pbdemov1.Genre_name)-1) + 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
adjectives = []string{
|
|
|
|
"Purple",
|
|
|
|
"Angry",
|
|
|
|
"Euphoric",
|
|
|
|
"Unexpected",
|
|
|
|
"Cheesy",
|
|
|
|
"Rancid",
|
|
|
|
"Pleasant",
|
|
|
|
"Mumbling",
|
|
|
|
"Enlightened",
|
|
|
|
}
|
|
|
|
|
|
|
|
nouns = []string{
|
|
|
|
"Speakerphone",
|
|
|
|
"Fox",
|
|
|
|
"Guppy",
|
|
|
|
"Smile",
|
|
|
|
"Emacs",
|
|
|
|
"Grapefruit",
|
|
|
|
"Engineer",
|
|
|
|
"Basketball",
|
|
|
|
}
|
|
|
|
|
|
|
|
members = []string{
|
|
|
|
"Owl",
|
|
|
|
"Tiger",
|
|
|
|
"Beetle",
|
|
|
|
"Lion",
|
|
|
|
"Chicken",
|
|
|
|
"Snake",
|
|
|
|
"Monkey",
|
|
|
|
"Kitten",
|
|
|
|
"Hound",
|
|
|
|
}
|
|
|
|
|
|
|
|
instruments = []string{
|
|
|
|
"Guitar",
|
|
|
|
"Bass",
|
|
|
|
"Lead Vocals",
|
|
|
|
"Backing Vocals",
|
|
|
|
"Drums",
|
|
|
|
"Synthesizer",
|
|
|
|
"Triangle",
|
|
|
|
"Standing by the stage looking cool",
|
|
|
|
}
|
|
|
|
)
|
2023-04-17 21:33:20 +00:00
|
|
|
|
|
|
|
func clone[T proto.Message](v T) T { return proto.Clone(v).(T) }
|