Skip to content

Commit

Permalink
Fix overwriting custom RBAC config and Kubernetes plugin JSON schema (#…
Browse files Browse the repository at this point in the history
…1401)

- Fix overwriting custom RBAC config
- Fix Kubernetes extraButtons schema
- Remove unused properties
  • Loading branch information
pkosiec authored Feb 27, 2024
1 parent a744a14 commit ced876d
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 136 deletions.
121 changes: 60 additions & 61 deletions helm/botkube/README.md

Large diffs are not rendered by default.

21 changes: 0 additions & 21 deletions helm/botkube/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -666,27 +666,6 @@ communications:
- k8s-err-events
- k8s-recommendation-events

# -- Settings for deprecated Slack integration.
# **DEPRECATED:** Legacy Slack integration has been deprecated and removed from the Slack App Directory.
# Use `socketSlack` instead. Read more here: https://docs.botkube.io/installation/slack/
#
# @default -- See the `values.yaml` file for full object.
## This object will be removed as a part of https://github.com/kubeshop/botkube/issues/865.
slack:
enabled: false
channels:
'default':
name: 'SLACK_CHANNEL'
notification:
disabled: false
bindings:
executors:
- k8s-default-tools
sources:
- k8s-err-events
- k8s-recommendation-events
token: ''

## Global Botkube configuration.
settings:
# -- Cluster name to differentiate incoming messages.
Expand Down
26 changes: 19 additions & 7 deletions internal/analytics/segment_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,14 +382,26 @@ func (r *SegmentReporter) getAnonymizedRBAC(rbac *config.PolicyRule) *config.Pol
return nil
}

rbac.Group.Prefix = r.anonymizedValue(rbac.Group.Prefix)
for key, name := range rbac.Group.Static.Values {
rbac.Group.Static.Values[key] = r.anonymizedValue(name)
var anonymizedGroupValues []string
for _, name := range rbac.Group.Static.Values {
anonymizedGroupValues = append(anonymizedGroupValues, r.anonymizedValue(name))
}
return &config.PolicyRule{
User: config.UserPolicySubject{
Type: rbac.User.Type,
Static: config.UserStaticSubject{
Value: r.anonymizedValue(rbac.User.Static.Value),
},
Prefix: r.anonymizedValue(rbac.User.Prefix),
},
Group: config.GroupPolicySubject{
Type: rbac.Group.Type,
Static: config.GroupStaticSubject{
Values: anonymizedGroupValues,
},
Prefix: r.anonymizedValue(rbac.Group.Prefix),
},
}

rbac.User.Prefix = r.anonymizedValue(rbac.User.Prefix)
rbac.User.Static.Value = r.anonymizedValue(rbac.User.Static.Value)
return rbac
}

func (r *SegmentReporter) anonymizedValue(value string) string {
Expand Down
41 changes: 34 additions & 7 deletions internal/analytics/segment_reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,7 @@ func TestSegmentReporter_ReportBotEnabled(t *testing.T) {

func TestSegmentReporter_ReportPluginsEnabled(t *testing.T) {
// given
identity := fixIdentity()
segmentReporter, segmentCli := fakeSegmentReporterWithIdentity(identity)

// when
err := segmentReporter.ReportPluginsEnabled(map[string]config.Executors{
executors := map[string]config.Executors{
"botkube/helm_11yy1": {
DisplayName: "helm",
Plugins: map[string]config.Plugin{
Expand Down Expand Up @@ -227,7 +223,8 @@ func TestSegmentReporter_ReportPluginsEnabled(t *testing.T) {
},
},
},
}, map[string]config.Sources{
}
sources := map[string]config.Sources{
"botkube/kubernetes_22yy2": {
DisplayName: "k8s",
Plugins: map[string]config.Plugin{
Expand Down Expand Up @@ -294,11 +291,27 @@ func TestSegmentReporter_ReportPluginsEnabled(t *testing.T) {
},
},
},
})
}

executors2, err := deepClone[map[string]config.Executors](executors)
require.NoError(t, err)

sources2, err := deepClone[map[string]config.Sources](sources)
require.NoError(t, err)

identity := fixIdentity()
segmentReporter, segmentCli := fakeSegmentReporterWithIdentity(identity)

// when
err = segmentReporter.ReportPluginsEnabled(executors, sources)
require.NoError(t, err)

// then
compareMessagesAgainstGoldenFile(t, segmentCli.messages)

// ensure the report doesn't modify the original maps
assert.Equal(t, executors2, executors)
assert.Equal(t, sources2, sources)
}

func TestSegmentReporter_ReportSinkEnabled(t *testing.T) {
Expand Down Expand Up @@ -458,3 +471,17 @@ func fixIdentity() *analytics.Identity {
ControlPlaneNodeCount: 0,
}
}

func deepClone[T any](in any) (any, error) {
origJSON, err := json.Marshal(in)
if err != nil {
return nil, err
}

var out T
if err = json.Unmarshal(origJSON, &out); err != nil {
return nil, err
}

return out, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"Group": {
"Type": "ChannelName",
"Static": {
"Values": []
"Values": null
},
"Prefix": "***"
}
Expand Down
2 changes: 1 addition & 1 deletion internal/executor/kubectl/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func NewExecutor(ver string, kcRunner kcRunner) *Executor {
}
}

// Metadata returns details about Helm plugin.
// Metadata returns details about Kubectl plugin.
func (e *Executor) Metadata(context.Context) (api.MetadataOutput, error) {
return api.MetadataOutput{
Version: e.pluginVersion,
Expand Down
78 changes: 40 additions & 38 deletions internal/source/kubernetes/config_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -703,46 +703,48 @@
"extraButtons": {
"title": "Extra Buttons",
"description": "Extra buttons for actionable items.",
"type": "object",
"properties": {
"enabled": {
"type": "boolean",
"default": false,
"description": "If enabled, renders extra button.",
"title": "Enable extra button"
},
"trigger": {
"title": "Trigger",
"description": "Define log level for the plugin. Ensure that Botkube has plugin logging enabled for standard output.",
"type": "object",
"additionalProperties": false,
"properties": {
"type": {
"title": "Event types",
"description": "Event types which will trigger this action",
"type": "array",
"items": {
"type": "string",
"title": "Event type"
"type": "array",
"items": {
"properties": {
"enabled": {
"type": "boolean",
"default": false,
"description": "If enabled, renders extra button.",
"title": "Enable extra button"
},
"trigger": {
"title": "Trigger",
"description": "Define log level for the plugin. Ensure that Botkube has plugin logging enabled for standard output.",
"type": "object",
"additionalProperties": false,
"properties": {
"type": {
"title": "Event types",
"description": "Event types which will trigger this action",
"type": "array",
"items": {
"type": "string",
"title": "Event type"
}
}
}
}
},
"button": {
"title": "Button",
"description": "Button settings for showing after each matched events.",
"type": "object",
"additionalProperties": false,
"properties": {
"commandTpl": {
"title": "Command template",
"description": "Command template that can be used to generate actual command.",
"type": "string"
},
"displayName": {
"title": "Display name",
"description": "Display name of this command.",
"type": "string"
},
"button": {
"title": "Button",
"description": "Button settings for showing after each matched events.",
"type": "object",
"additionalProperties": false,
"properties": {
"commandTpl": {
"title": "Command template",
"description": "Command template that can be used to generate actual command.",
"type": "string"
},
"displayName": {
"title": "Display name",
"description": "Display name of this command.",
"type": "string"
}
}
}
}
Expand Down

0 comments on commit ced876d

Please sign in to comment.