-
Notifications
You must be signed in to change notification settings - Fork 758
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
fix: refactoring to remove pubsub flags to improve experience #3339
base: master
Are you sure you want to change the base?
Changes from 3 commits
63af521
d7733dd
85ccad7
f84bc2b
2d27928
aecd00d
d5cfb9b
acc6280
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,9 @@ import ( | |
type ClientConfig struct { | ||
// Name of the component to be used for pub sub messaging | ||
Component string `json:"component"` | ||
|
||
// Topic where the messages would be published for the connection | ||
Topic string `json:"topic"` | ||
} | ||
|
||
// Dapr represents driver for interacting with pub sub using dapr. | ||
|
@@ -21,19 +24,22 @@ type Dapr struct { | |
|
||
// Name of the pubsub component | ||
pubSubComponent string | ||
|
||
// Topic where the messages would be published for the connection | ||
topic string | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What part of the connection/channel config should be specify-able by downstream users (e.g. constraint authors) and what should be owned by the author of the constraint objects? We should think about the personas who would be interacting with these knobs. Are the people setting up the infra always the same people writing the policy? |
||
} | ||
|
||
const ( | ||
Name = "dapr" | ||
) | ||
|
||
func (r *Dapr) Publish(_ context.Context, data interface{}, topic string) error { | ||
func (r *Dapr) Publish(_ context.Context, data interface{}) error { | ||
jsonData, err := json.Marshal(data) | ||
if err != nil { | ||
return fmt.Errorf("error marshaling data: %w", err) | ||
} | ||
|
||
err = r.client.PublishEvent(context.Background(), r.pubSubComponent, topic, jsonData) | ||
err = r.client.PublishEvent(context.Background(), r.pubSubComponent, r.topic, jsonData) | ||
if err != nil { | ||
return fmt.Errorf("error publishing message to dapr: %w", err) | ||
} | ||
|
@@ -56,6 +62,11 @@ func (r *Dapr) UpdateConnection(_ context.Context, config interface{}) error { | |
return fmt.Errorf("failed to get value of component") | ||
} | ||
r.pubSubComponent = cfg.Component | ||
cfg.Topic, ok = m["topic"].(string) | ||
if !ok { | ||
return fmt.Errorf("failed to get value of topic") | ||
} | ||
r.topic = cfg.Topic | ||
return nil | ||
} | ||
|
||
|
@@ -70,6 +81,10 @@ func NewConnection(_ context.Context, config interface{}) (connection.Connection | |
if !ok { | ||
return nil, fmt.Errorf("failed to get value of component") | ||
} | ||
cfg.Topic, ok = m["topic"].(string) | ||
if !ok { | ||
return nil, fmt.Errorf("failed to get value of topic") | ||
} | ||
|
||
tmp, err := daprClient.NewClient() | ||
if err != nil { | ||
|
@@ -79,5 +94,6 @@ func NewConnection(_ context.Context, config interface{}) (connection.Connection | |
return &Dapr{ | ||
client: tmp, | ||
pubSubComponent: cfg.Component, | ||
topic: cfg.Topic, | ||
}, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package pubsub | |
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"sync" | ||
|
||
|
@@ -19,16 +20,19 @@ func NewSystem() *System { | |
return &System{} | ||
} | ||
|
||
func (s *System) Publish(_ context.Context, connection string, topic string, msg interface{}) error { | ||
func (s *System) Publish(ctx context.Context, msg interface{}) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we still looking at supporting multiple queues simultaneously? Possibly specifying which connection/topic gets pushed to as part of a constraint's enforcement action? Example: send violation X to security queue, violation Y to ops queue? If so, |
||
s.mux.RLock() | ||
defer s.mux.RUnlock() | ||
if len(s.connections) > 0 { | ||
if c, ok := s.connections[connection]; ok { | ||
return c.Publish(context.Background(), msg, topic) | ||
} | ||
return fmt.Errorf("connection is not initialized, name: %s ", connection) | ||
var errs error | ||
|
||
if len(s.connections) == 0 { | ||
return fmt.Errorf("no connections are established") | ||
} | ||
|
||
for _, c := range s.connections { | ||
errs = errors.Join(errs, c.Publish(ctx, msg)) | ||
} | ||
return fmt.Errorf("No connections are established") | ||
return errs | ||
} | ||
|
||
func (s *System) UpsertConnection(ctx context.Context, config interface{}, name string, provider string) error { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,5 +24,6 @@ data: | |
provider: "dapr" | ||
config: | | ||
{ | ||
"component": "pubsub" | ||
"component": "pubsub", | ||
"topic": "audit-channel" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ Install prerequisites such as a pubsub tool, a message broker etc. | |
|
||
### Setting up audit with pubsub enabled | ||
|
||
In the audit deployment, set the `--enable-pub-sub` flag to `true` to publish audit violations. Additionally, use `--audit-connection` (defaults to `audit-connection`) and `--audit-channel`(defaults to `audit-channel`) flags to allow audit to publish violations using desired connection onto desired channel. `--audit-connection` must be set to the name of the connection config, and `--audit-channel` must be set to name of the channel where violations should get published. | ||
In the audit deployment, set the `--enable-pub-sub` flag to `true` to publish audit violations. | ||
|
||
A ConfigMap that contains `provider` and `config` fields in `data` is required to establish connection for sending violations over the channel. Following is an example ConfigMap to establish a connection that uses Dapr to publish messages: | ||
|
||
|
@@ -33,7 +33,8 @@ data: | |
provider: "dapr" | ||
config: | | ||
{ | ||
"component": "pubsub" | ||
"component": "pubsub", | ||
"topic": "audit-channel" | ||
} | ||
``` | ||
|
||
|
@@ -125,6 +126,9 @@ Dapr: https://dapr.io/ | |
- name: go-sub | ||
image: fake-subscriber:latest | ||
imagePullPolicy: Never | ||
env: | ||
- name: AUDIT_CHANNEL | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is consuming this env var? it looks like it is no longer being used and got removed here: https://github.com/open-policy-agent/gatekeeper/pull/3339/files#diff-76ed074a9305c04054cdebb9e9aad2d818052b07091de1f20cad0bbac34ffb52L228 |
||
value: "audit-channel" | ||
``` | ||
|
||
> [!IMPORTANT] | ||
|
@@ -156,15 +160,13 @@ Dapr: https://dapr.io/ | |
EOF | ||
``` | ||
|
||
2. To upgrade or install Gatekeeper with `--enable-pub-sub` set to `true`, `--audit-connection` set to `audit-connection`, `--audit-channel` set to `audit-channel` on audit pod. | ||
2. To upgrade or install Gatekeeper with `--enable-pub-sub` set to `true` on audit pod. | ||
|
||
```shell | ||
# auditPodAnnotations is used to add annotations required by Dapr to inject sidecar to audit pod | ||
echo 'auditPodAnnotations: {dapr.io/enabled: "true", dapr.io/app-id: "audit", dapr.io/metrics-port: "9999", dapr.io/sidecar-seccomp-profile-type: "RuntimeDefault"}' > /tmp/annotations.yaml | ||
helm upgrade --install gatekeeper gatekeeper/gatekeeper --namespace gatekeeper-system \ | ||
--set audit.enablePubsub=true \ | ||
--set audit.connection=audit-connection \ | ||
--set audit.channel=audit-channel \ | ||
--values /tmp/annotations.yaml | ||
``` | ||
|
||
|
@@ -183,13 +185,12 @@ Dapr: https://dapr.io/ | |
provider: "dapr" | ||
config: | | ||
{ | ||
"component": "pubsub" | ||
"component": "pubsub", | ||
"topic": "audit-channel" | ||
} | ||
EOF | ||
``` | ||
|
||
**Note:** Name of the connection configMap must match the value of `--audit-connection` for it to be used by audit to publish violation. At the moment, only one connection config can exists for audit. | ||
|
||
4. Create the constraint templates and constraints, and make sure audit ran by checking constraints. If constraint status is updated with information such as `auditTimeStamp` or `totalViolations`, then audit has ran at least once. Additionally, populated `TOTAL-VIOLATIONS` field for all constraints while listing constraints also indicates that audit has ran at least once. | ||
|
||
```log | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you removed the wrong value here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yip! I will update that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yea