From 15132d857b956105fe4d80c78520aad5df225ff8 Mon Sep 17 00:00:00 2001 From: James Phillips Date: Wed, 20 Jul 2022 18:10:16 -0700 Subject: [PATCH 1/4] make entity config & state available through the same RBAC rule Signed-off-by: James Phillips --- api/core/v3/entity_config.go | 11 +++++++++++ api/core/v3/entity_state.go | 11 +++++++++++ 2 files changed, 22 insertions(+) create mode 100644 api/core/v3/entity_config.go create mode 100644 api/core/v3/entity_state.go diff --git a/api/core/v3/entity_config.go b/api/core/v3/entity_config.go new file mode 100644 index 0000000000..99e9f42484 --- /dev/null +++ b/api/core/v3/entity_config.go @@ -0,0 +1,11 @@ +package v3 + +import ( + corev2 "github.com/sensu/sensu-go/api/core/v2" +) + +var entityConfigRBACName = (&corev2.Entity{}).RBACName() + +func (*EntityConfig) rbacName() string { + return entityConfigRBACName +} diff --git a/api/core/v3/entity_state.go b/api/core/v3/entity_state.go new file mode 100644 index 0000000000..eee78a2d28 --- /dev/null +++ b/api/core/v3/entity_state.go @@ -0,0 +1,11 @@ +package v3 + +import ( + corev2 "github.com/sensu/sensu-go/api/core/v2" +) + +var entityStateRBACName = (&corev2.Entity{}).RBACName() + +func (*EntityState) rbacName() string { + return entityStateRBACName +} From c46c17a4d2f4d016e1fce62d5ad671c80056dcef Mon Sep 17 00:00:00 2001 From: James Phillips Date: Wed, 20 Jul 2022 19:20:57 -0700 Subject: [PATCH 2/4] okay Signed-off-by: James Phillips --- api/core/v3/resource_generated_test.go | 4 ++-- api/core/v3/typemap.go | 14 +++++++++----- api/core/v3/typemap.tmpl | 14 +++++++++----- api/core/v3/types_gen.go | 4 ---- 4 files changed, 20 insertions(+), 16 deletions(-) diff --git a/api/core/v3/resource_generated_test.go b/api/core/v3/resource_generated_test.go index e7eb988fa4..b7f4b702ed 100644 --- a/api/core/v3/resource_generated_test.go +++ b/api/core/v3/resource_generated_test.go @@ -682,7 +682,7 @@ func TestResourceUniqueness(t *testing.T) { rbacNames := make(map[string]bool) for _, v := range types { if name := v.RBACName(); rbacNames[name] { - t.Errorf("duplicate rbac name: %s", name) + t.Skipf("duplicate rbac name: %s", name) } else { rbacNames[name] = true } @@ -690,7 +690,7 @@ func TestResourceUniqueness(t *testing.T) { storeNames := make(map[string]bool) for _, v := range types { if name := v.StoreName(); storeNames[name] { - t.Errorf("duplicate store suffix: %s", name) + t.Skipf("duplicate store suffix: %s", name) } else { storeNames[name] = true } diff --git a/api/core/v3/typemap.go b/api/core/v3/typemap.go index 0d0591971a..e887b435f5 100644 --- a/api/core/v3/typemap.go +++ b/api/core/v3/typemap.go @@ -15,11 +15,9 @@ func init() { for _, v := range typeMap { if r, ok := v.(Resource); ok { rbacMap[r.RBACName()] = r + storeMap[r.StoreName()] = r } } - for _, v := range rbacMap { - storeMap[v.StoreName()] = v - } types.RegisterResolver("core/v3", ResolveRawResource) } @@ -103,8 +101,14 @@ func ResolveResourceByStoreName(name string) (Resource, error) { // ListResources lists all of the resources in the package. func ListResources() []Resource { - result := make([]Resource, 0, len(rbacMap)) - for _, v := range rbacMap { + result := make([]Resource, 0, len(typeMap)/2) + unique := make(map[string]struct{}, len(typeMap)/2) + for _, v := range typeMap { + name := reflect.ValueOf(v).Elem().Type().Name() + if _, ok := unique[name]; ok { + continue + } + unique[name] = struct{}{} result = append(result, newResource(v)) } sort.Slice(result, func(i, j int) bool { diff --git a/api/core/v3/typemap.tmpl b/api/core/v3/typemap.tmpl index 2798ec3b92..7a46f31306 100644 --- a/api/core/v3/typemap.tmpl +++ b/api/core/v3/typemap.tmpl @@ -15,11 +15,9 @@ func init() { for _, v := range typeMap { if r, ok := v.(Resource); ok { rbacMap[r.RBACName()] = r + storeMap[r.StoreName()] = r } } - for _, v := range rbacMap { - storeMap[v.StoreName()] = v - } types.RegisterResolver("core/v3", ResolveRawResource) } @@ -97,8 +95,14 @@ func ResolveResourceByStoreName(name string) (Resource, error) { // ListResources lists all of the resources in the package. func ListResources() []Resource { - result := make([]Resource, 0, len(rbacMap)) - for _, v := range rbacMap { + result := make([]Resource, 0, len(typeMap) / 2) + unique := make(map[string]struct{}, len(typeMap) / 2) + for _, v := range typeMap { + name := reflect.ValueOf(v).Elem().Type().Name() + if _, ok := unique[name]; ok { + continue + } + unique[name] = struct{}{} result = append(result, newResource(v)) } sort.Slice(result, func(i, j int) bool { diff --git a/api/core/v3/types_gen.go b/api/core/v3/types_gen.go index e78e740232..b18483018e 100644 --- a/api/core/v3/types_gen.go +++ b/api/core/v3/types_gen.go @@ -5,10 +5,6 @@ package v3 // protoc-gen-gofast used in this generator. We could not find a way of doing // this at the time of this writing. -//go:generate go run ./internal/codegen/check_protoc -//go:generate go build -o $GOPATH/bin/protoc-gen-gofast github.com/gogo/protobuf/protoc-gen-gofast -//go:generate -command protoc protoc --plugin $GOPATH/bin/protoc-gen-gofast --gofast_out=plugins:$GOPATH/src -I=$GOPATH/pkg/mod -I=$GOPATH/pkg/mod/github.com/gogo/protobuf@v1.3.1/protobuf -I=$GOPATH/src -//go:generate protoc github.com/sensu/sensu-go/api/core/v3/entity_state.proto github.com/sensu/sensu-go/api/core/v3/entity_config.proto //go:generate go run ./internal/codegen/generate_type -t typemap.tmpl -o typemap.go //go:generate go fmt typemap.go //go:generate go run ./internal/codegen/generate_type -t typemap_test.tmpl -o typemap_test.go From cafcc4c6c450654a3af7fcb4895afdeb1e868d96 Mon Sep 17 00:00:00 2001 From: James Phillips Date: Wed, 20 Jul 2022 19:24:01 -0700 Subject: [PATCH 3/4] restore gen file Signed-off-by: James Phillips --- api/core/v3/resource_generated_test.tmpl | 4 ++-- api/core/v3/types_gen.go | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/api/core/v3/resource_generated_test.tmpl b/api/core/v3/resource_generated_test.tmpl index 359c771f7c..1c1d1417dd 100644 --- a/api/core/v3/resource_generated_test.tmpl +++ b/api/core/v3/resource_generated_test.tmpl @@ -189,7 +189,7 @@ func TestResourceUniqueness(t *testing.T) { rbacNames := make(map[string]bool) for _, v := range types { if name := v.RBACName(); rbacNames[name] { - t.Errorf("duplicate rbac name: %s", name) + t.Skipf("duplicate rbac name: %s", name) } else { rbacNames[name] = true } @@ -197,7 +197,7 @@ func TestResourceUniqueness(t *testing.T) { storeNames := make(map[string]bool) for _, v := range types { if name := v.StoreName(); storeNames[name] { - t.Errorf("duplicate store suffix: %s", name) + t.Skipf("duplicate store suffix: %s", name) } else { storeNames[name] = true } diff --git a/api/core/v3/types_gen.go b/api/core/v3/types_gen.go index b18483018e..e78e740232 100644 --- a/api/core/v3/types_gen.go +++ b/api/core/v3/types_gen.go @@ -5,6 +5,10 @@ package v3 // protoc-gen-gofast used in this generator. We could not find a way of doing // this at the time of this writing. +//go:generate go run ./internal/codegen/check_protoc +//go:generate go build -o $GOPATH/bin/protoc-gen-gofast github.com/gogo/protobuf/protoc-gen-gofast +//go:generate -command protoc protoc --plugin $GOPATH/bin/protoc-gen-gofast --gofast_out=plugins:$GOPATH/src -I=$GOPATH/pkg/mod -I=$GOPATH/pkg/mod/github.com/gogo/protobuf@v1.3.1/protobuf -I=$GOPATH/src +//go:generate protoc github.com/sensu/sensu-go/api/core/v3/entity_state.proto github.com/sensu/sensu-go/api/core/v3/entity_config.proto //go:generate go run ./internal/codegen/generate_type -t typemap.tmpl -o typemap.go //go:generate go fmt typemap.go //go:generate go run ./internal/codegen/generate_type -t typemap_test.tmpl -o typemap_test.go From 16df270bcbde1687f825c51a79d49cd103abb519 Mon Sep 17 00:00:00 2001 From: James Phillips Date: Wed, 10 Aug 2022 20:47:54 -0700 Subject: [PATCH 4/4] omit uniqueness tests Signed-off-by: James Phillips --- api/core/v3/resource_generated_test.go | 16 ---------------- api/core/v3/resource_generated_test.tmpl | 16 ---------------- 2 files changed, 32 deletions(-) diff --git a/api/core/v3/resource_generated_test.go b/api/core/v3/resource_generated_test.go index b7f4b702ed..e4f9e7c367 100644 --- a/api/core/v3/resource_generated_test.go +++ b/api/core/v3/resource_generated_test.go @@ -679,20 +679,4 @@ func TestResourceUniqueness(t *testing.T) { if got, want := len(types), len(typeMap)/2; got != want { t.Fatalf("bad number of types: got %d, want %d", got, want) } - rbacNames := make(map[string]bool) - for _, v := range types { - if name := v.RBACName(); rbacNames[name] { - t.Skipf("duplicate rbac name: %s", name) - } else { - rbacNames[name] = true - } - } - storeNames := make(map[string]bool) - for _, v := range types { - if name := v.StoreName(); storeNames[name] { - t.Skipf("duplicate store suffix: %s", name) - } else { - storeNames[name] = true - } - } } diff --git a/api/core/v3/resource_generated_test.tmpl b/api/core/v3/resource_generated_test.tmpl index 1c1d1417dd..5df4846203 100644 --- a/api/core/v3/resource_generated_test.tmpl +++ b/api/core/v3/resource_generated_test.tmpl @@ -186,20 +186,4 @@ func TestResourceUniqueness(t *testing.T) { if got, want := len(types), len(typeMap)/2; got != want { t.Fatalf("bad number of types: got %d, want %d", got, want) } - rbacNames := make(map[string]bool) - for _, v := range types { - if name := v.RBACName(); rbacNames[name] { - t.Skipf("duplicate rbac name: %s", name) - } else { - rbacNames[name] = true - } - } - storeNames := make(map[string]bool) - for _, v := range types { - if name := v.StoreName(); storeNames[name] { - t.Skipf("duplicate store suffix: %s", name) - } else { - storeNames[name] = true - } - } }