Skip to content

Commit

Permalink
Optional NATS config / event publishing (#31)
Browse files Browse the repository at this point in the history
* Not all projects will need event publishing, so it should be configurable such that ones that don't intend to use it don't need to have NATS credentials configured for them.

Signed-off-by: Jacob See <[email protected]>

* Switch from nil pointer to an enabled field on the struct

Signed-off-by: Jacob See <[email protected]>

---------

Signed-off-by: Jacob See <[email protected]>
  • Loading branch information
jacobsee authored Mar 14, 2024
1 parent d6a01fe commit 84f45b7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ data:
jwksuri: https://iam.example.com/jwks.json
issuer: https://iam.example.com/
events:
enabled: true
nats:
url: nats://nats:4222
credsFile: /etc/nats/nats.creds
Expand Down Expand Up @@ -71,7 +72,7 @@ spec:
- name: iam-runtime-socket
mountPath: /var/iam-runtime/
- name: iam-runtime
image: ghcr.io/infratographer/iam-runtime-infratographer:v0.3.0
image: ghcr.io/infratographer/iam-runtime-infratographer:v0.3.1
volumeMounts:
- name: iam-runtime-config
mountPath: /etc/iam-runtime-infratographer/
Expand Down
4 changes: 3 additions & 1 deletion internal/eventsx/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import "github.com/spf13/pflag"

// Config represents a configuration for events.
type Config struct {
NATS NATSConfig `mapstructure:"nats"`
Enabled bool `mapstructure:"enabled"`
NATS NATSConfig `mapstructure:"nats"`
}

// NATSConfig represents NATS-specific configuration for events.
Expand All @@ -18,6 +19,7 @@ type NATSConfig struct {

// AddFlags sets the command line flags for publishing events.
func AddFlags(flags *pflag.FlagSet) {
flags.Bool("events.enabled", false, "enable NATS event-based functions")
flags.String("events.nats.url", "", "NATS server URL to use")
flags.String("events.nats.publishprefix", "", "NATS publish prefix to use")
flags.String("events.nats.publishtopic", "", "NATS publish topic to use")
Expand Down
6 changes: 6 additions & 0 deletions internal/eventsx/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package eventsx

import "errors"

// ErrPublishNotEnabled represents an error state where an event publish was attempted despite not being enabled
var ErrPublishNotEnabled = errors.New("event publishing is not enabled")
13 changes: 13 additions & 0 deletions internal/eventsx/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,28 @@ type Publisher interface {
}

type publisher struct {
enabled bool
topic string
innerPub events.AuthRelationshipPublisher
}

func (p publisher) PublishAuthRelationshipRequest(ctx context.Context, message events.AuthRelationshipRequest) (events.Message[events.AuthRelationshipResponse], error) {
if !p.enabled {
return nil, ErrPublishNotEnabled
}
return p.innerPub.PublishAuthRelationshipRequest(ctx, p.topic, message)
}

// NewPublisher creates a new events publisher from the given config.
func NewPublisher(cfg Config) (Publisher, error) {
if !cfg.Enabled {
return publisher{
enabled: false,
topic: "",
innerPub: nil,
}, nil
}

natsCfg := events.NATSConfig{
URL: cfg.NATS.URL,
PublishPrefix: cfg.NATS.PublishPrefix,
Expand All @@ -36,6 +48,7 @@ func NewPublisher(cfg Config) (Publisher, error) {
}

out := publisher{
enabled: true,
topic: cfg.NATS.PublishTopic,
innerPub: conn,
}
Expand Down

0 comments on commit 84f45b7

Please sign in to comment.