Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[No click le green button] Attempt to cherry pick changes into main #4879

Merged
merged 3 commits into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions api/core/v3/entity_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package v3

import (
"strconv"
"strings"

corev2 "github.com/sensu/sensu-go/api/core/v2"
stringutil "github.com/sensu/sensu-go/api/core/v3/internal/strings"
)

var entityConfigRBACName = (&corev2.Entity{}).RBACName()

func (e *EntityConfig) rbacName() string {
return entityConfigRBACName
}

func (e *EntityConfig) Fields() map[string]string {
fields := map[string]string{
"entity_config.name": e.Metadata.Name,
"entity_config.namespace": e.Metadata.Namespace,
"entity_config.deregister": strconv.FormatBool(e.Deregister),
"entity_config.entity_class": e.EntityClass,
"entity_config.subscriptions": strings.Join(e.Subscriptions, ","),
}
MergeMapWithPrefix(fields, e.Metadata.Labels, "entity_config.labels.")
return fields
}

// MergeMapWithPrefix merges contents of one map into another using a prefix.
func MergeMapWithPrefix(a map[string]string, b map[string]string, prefix string) {
for k, v := range b {
a[prefix+k] = v
}
}

func redactMap(m map[string]string, redact []string) map[string]string {
if len(redact) == 0 {
redact = corev2.DefaultRedactFields
}
result := make(map[string]string, len(m))
for k, v := range m {
if stringutil.FoundInArray(k, redact) {
result[k] = corev2.Redacted
} else {
result[k] = v
}
}
return result
}

// ProduceRedacted redacts the entity according to the entity's Redact fields.
// A redacted copy is returned. The copy contains pointers to the original's
// memory, with different Labels and Annotations.
func (e *EntityConfig) ProduceRedacted() Resource {
if e == nil {
return nil
}
if e.Metadata == nil || (e.Metadata.Labels == nil && e.Metadata.Annotations == nil) {
return e
}
copy := &EntityConfig{}
*copy = *e
copy.Metadata.Annotations = redactMap(e.Metadata.Annotations, e.Redact)
copy.Metadata.Labels = redactMap(e.Metadata.Labels, e.Redact)
return copy
}
135 changes: 135 additions & 0 deletions api/core/v3/entity_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package v3

import (
"reflect"
"testing"

corev2 "github.com/sensu/sensu-go/api/core/v2"
)

func TestEntityConfigFields(t *testing.T) {
tests := []struct {
name string
args Fielder
wantKey string
want string
}{
{
name: "exposes name",
args: FixtureEntityConfig("my-agent"),
wantKey: "entity_config.name",
want: "my-agent",
},
{
name: "exposes deregister",
args: &EntityConfig{Metadata: &corev2.ObjectMeta{}, Deregister: true},
wantKey: "entity_config.deregister",
want: "true",
},
{
name: "exposes class",
args: &EntityConfig{Metadata: &corev2.ObjectMeta{}, EntityClass: "agent"},
wantKey: "entity_config.entity_class",
want: "agent",
},
{
name: "exposes subscriptions",
args: &EntityConfig{Metadata: &corev2.ObjectMeta{}, Subscriptions: []string{"www", "unix"}},
wantKey: "entity_config.subscriptions",
want: "www,unix",
},
{
name: "exposes labels",
args: &EntityConfig{
Metadata: &corev2.ObjectMeta{
Labels: map[string]string{"region": "philadelphia"},
},
},
wantKey: "entity_config.labels.region",
want: "philadelphia",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.args.Fields()
if !reflect.DeepEqual(got[tt.wantKey], tt.want) {
t.Errorf("EntityConfig.Fields() = got[%s] %v, want[%s] %v", tt.wantKey, got[tt.wantKey], tt.wantKey, tt.want)
}
})
}
}

func TestEntityConfig_ProduceRedacted(t *testing.T) {
tests := []struct {
name string
in *EntityConfig
want *EntityConfig
}{
{
name: "nil metadata",
in: func() *EntityConfig {
cfg := FixtureEntityConfig("test")
cfg.Metadata = nil
return cfg
}(),
want: func() *EntityConfig {
cfg := FixtureEntityConfig("test")
cfg.Metadata = nil
return cfg
}(),
},
{
name: "nothing to redact",
in: func() *EntityConfig {
cfg := FixtureEntityConfig("test")
cfg.Metadata.Labels["my_field"] = "test123"
return cfg
}(),
want: func() *EntityConfig {
cfg := FixtureEntityConfig("test")
cfg.Metadata.Labels["my_field"] = "test123"
return cfg
}(),
},
{
name: "redact default fields",
in: func() *EntityConfig {
cfg := FixtureEntityConfig("test")
cfg.Metadata.Labels["my_field"] = "test123"
cfg.Metadata.Labels["password"] = "test123"
return cfg
}(),
want: func() *EntityConfig {
cfg := FixtureEntityConfig("test")
cfg.Metadata.Labels["my_field"] = "test123"
cfg.Metadata.Labels["password"] = corev2.Redacted
return cfg
}(),
},
{
name: "redact custom fields",
in: func() *EntityConfig {
cfg := FixtureEntityConfig("test")
cfg.Redact = []string{"my_field"}
cfg.Metadata.Labels["my_field"] = "test123"
cfg.Metadata.Labels["password"] = "test123"
return cfg
}(),
want: func() *EntityConfig {
cfg := FixtureEntityConfig("test")
cfg.Redact = []string{"my_field"}
cfg.Metadata.Labels["my_field"] = corev2.Redacted
cfg.Metadata.Labels["password"] = "test123"
return cfg
}(),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := tt.in
if got := e.ProduceRedacted(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("EntityConfig.ProduceRedacted() = %v, want %v", got, tt.want)
}
})
}
}
19 changes: 19 additions & 0 deletions api/core/v3/entity_state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package v3

import (
corev2 "github.com/sensu/sensu-go/api/core/v2"
)

var entityStateRBACName = (&corev2.Entity{}).RBACName()

func (*EntityState) rbacName() string {
return entityStateRBACName
}

func (e *EntityState) Fields() map[string]string {
fields := map[string]string{
"entity_state.name": e.Metadata.Name,
"entity_state.namespace": e.Metadata.Namespace,
}
return fields
}
36 changes: 36 additions & 0 deletions api/core/v3/entity_state_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package v3

import (
"reflect"
"testing"
)

func TestEntityStateFields(t *testing.T) {
tests := []struct {
name string
args Fielder
wantKey string
want string
}{
{
name: "exposes name",
args: FixtureEntityState("my-agent"),
wantKey: "entity_state.name",
want: "my-agent",
},
{
name: "exposes deregister",
args: FixtureEntityState("my-agent"),
wantKey: "entity_state.namespace",
want: "default",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.args.Fields()
if !reflect.DeepEqual(got[tt.wantKey], tt.want) {
t.Errorf("EntityState.Fields() = got[%s] %v, want[%s] %v", tt.wantKey, got[tt.wantKey], tt.wantKey, tt.want)
}
})
}
}
7 changes: 7 additions & 0 deletions api/core/v3/fielder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package v3

// Fielder includes a set of fields that represent a resource.
type Fielder interface {
// Fields returns a set of fields that represent the resource.
Fields() map[string]string
}
4 changes: 4 additions & 0 deletions api/core/v3/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ require (
github.com/golang/protobuf v1.5.2
github.com/sensu/sensu-go/api/core/v2 v2.15.0
github.com/sensu/sensu-go/types v0.11.0
github.com/stretchr/testify v1.6.0
)

require (
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/echlebek/timeproxy v1.0.0 // indirect
github.com/golang-jwt/jwt/v4 v4.0.0 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.3 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/robertkrimen/otto v0.0.0-20191219234010-c382bd3c16ff // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/sirupsen/logrus v1.6.0 // indirect
Expand All @@ -27,4 +30,5 @@ require (
google.golang.org/grpc v1.38.0 // indirect
google.golang.org/protobuf v1.26.0 // indirect
gopkg.in/sourcemap.v1 v1.0.5 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
1 change: 1 addition & 0 deletions api/core/v3/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlba
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/sourcemap.v1 v1.0.5 h1:inv58fC9f9J3TK2Y2R1NPntXEn3/wjWHkonhIUODNTI=
gopkg.in/sourcemap.v1 v1.0.5/go.mod h1:2RlvNNSMglmRrcvhfuzp4hQHwOtjxlbjX7UPY/GXb78=
Expand Down
60 changes: 60 additions & 0 deletions api/core/v3/internal/strings/strings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package strings

import (
"strings"
"unicode"
)

const (
lowerAlphaStart = 97
lowerAlphaStop = 122
)

func isAlpha(r rune) bool {
return r >= lowerAlphaStart && r <= lowerAlphaStop
}

func alphaNumeric(s string) bool {
for _, r := range s {
if !(unicode.IsDigit(r) || isAlpha(r)) {
return false
}
}
return true
}

func normalize(s string) string {
if alphaNumeric(s) {
return s
}
lowered := strings.ToLower(s)
if alphaNumeric(lowered) {
return lowered
}
trimmed := make([]rune, 0, len(lowered))
for _, r := range lowered {
if isAlpha(r) {
trimmed = append(trimmed, r)
}
}
return string(trimmed)
}

// FoundInArray searches array for item without distinguishing between uppercase
// and lowercase and non-alphanumeric characters. Returns true if item is a
// value of array
func FoundInArray(item string, array []string) bool {
if item == "" || len(array) == 0 {
return false
}

item = normalize(item)

for i := range array {
if normalize(array[i]) == item {
return true
}
}

return false
}
34 changes: 34 additions & 0 deletions api/core/v3/internal/strings/strings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package strings

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestFoundInArray(t *testing.T) {
var array []string

found := FoundInArray("Foo", []string{})
assert.False(t, found)

array = []string{"foo", "bar"}
found = FoundInArray("Foo", array)
assert.True(t, found)

array = []string{"foo", "bar"}
found = FoundInArray("FooBar", array)
assert.False(t, found)

array = []string{"foo", "bar"}
found = FoundInArray("Foo ", array)
assert.True(t, found)

array = []string{"foo_bar"}
found = FoundInArray("Foo_Bar", array)
assert.True(t, found)

array = []string{"foobar"}
found = FoundInArray("Foo_Qux", array)
assert.False(t, found)
}
Loading