Skip to content

Commit

Permalink
Changes to the configuration defaults, the poll interval type and sup…
Browse files Browse the repository at this point in the history
…ported runtimes"
  • Loading branch information
rnishtala-sumo committed Oct 23, 2023
1 parent c8fa535 commit a154fe1
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 17 deletions.
7 changes: 4 additions & 3 deletions pkg/receiver/activedirectoryinvreceiver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ receivers:
base_dn: "CN=Users,DC=exampledomain,DC=com"

# User attributes
# default = [name, mail, department, manager, memberOf]
attributes: [name, mail, department, manager, memberOf]

# The polling interval.
# default = 60
poll_interval: 60
# default = 24h
poll_interval: 24h
```
The full list of settings exposed for this receiver are documented in
Expand All @@ -33,7 +34,7 @@ receivers:
activedirectoryinv:
base_dn: "CN=Users,DC=exampledomain,DC=com"
attributes: [name, mail, department, manager, memberOf]
poll_interval: 60
poll_interval: 24h

exporters:
logging:
Expand Down
18 changes: 16 additions & 2 deletions pkg/receiver/activedirectoryinvreceiver/adinvreceiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package activedirectoryinvreceiver
import (
"context"
"fmt"
"runtime"
"sync"
"time"

Expand Down Expand Up @@ -79,6 +80,9 @@ func newLogsReceiver(cfg *ADConfig, logger *zap.Logger, client Client, consumer

// Start the logs receiver
func (l *ADReceiver) Start(ctx context.Context, _ component.Host) error {
if !supportedOS() {
return errSupportedOS
}
l.logger.Debug("Starting to poll for active directory inventory records")
l.wg.Add(1)
go l.startPolling(ctx)
Expand All @@ -96,7 +100,13 @@ func (l *ADReceiver) Shutdown(_ context.Context) error {
// Start polling for Active Directory inventory records
func (l *ADReceiver) startPolling(ctx context.Context) {
defer l.wg.Done()
t := time.NewTicker(l.config.PollInterval * time.Second)
duration, err := time.ParseDuration(l.config.PollInterval)
if err != nil {
l.logger.Error("Failed to parse poll interval", zap.Error(err))
return
}
l.logger.Info("Polling interval: ", zap.Duration("interval", duration))
t := time.NewTicker(duration)
for {
select {
case <-ctx.Done():
Expand Down Expand Up @@ -145,10 +155,10 @@ func (r *ADReceiver) poll(ctx context.Context) error {
resourceLogs := &rl
_ = resourceLogs.ScopeLogs().AppendEmpty()
root, err := r.client.Open(r.config.DN, resourceLogs)
r.traverse(root, r.config.Attributes, resourceLogs)
if err != nil {
return err
}
r.traverse(root, r.config.Attributes, resourceLogs)
err = r.consumer.ConsumeLogs(ctx, logs)
if err != nil {
r.logger.Error("Error consuming log", zap.Error(err))
Expand All @@ -171,3 +181,7 @@ func setUserAttributes(user Object, attrs []string, resourceLogs *plog.ResourceL
logRecord.SetTimestamp(observedTime)
logRecord.Body().SetStr(attributes)
}

func supportedOS() bool {
return (runtime.GOOS == "windows")
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func TestStart(t *testing.T) {
func TestPoll(t *testing.T) {
cfg := CreateDefaultConfig().(*ADConfig)
cfg.DN = "CN=Guest,CN=Users,DC=exampledomain,DC=com"
cfg.PollInterval = 1
cfg.PollInterval = "1s"
cfg.Attributes = []string{"name"}

sink := &consumertest.LogsSink{}
Expand Down
16 changes: 11 additions & 5 deletions pkg/receiver/activedirectoryinvreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,22 @@ import (
// ADConfig defines configuration for Active Directory Inventory receiver.

type ADConfig struct {
DN string `mapstructure:"base_dn"` // DN is the base distinguished name to search from
Attributes []string `mapstructure:"attributes"`
PollInterval time.Duration `mapstructure:"poll_interval"`
DN string `mapstructure:"base_dn"` // DN is the base distinguished name to search from
Attributes []string `mapstructure:"attributes"`
PollInterval string `mapstructure:"poll_interval"`
}

var (
errInvalidDN = errors.New("Base DN is incorrect, it must be in the format of CN=Users,DC=exampledomain,DC=com")
errInvalidPollInterval = errors.New("poll interval is incorrect, it must be a duration greater than one second")
errInvalidPollInterval = errors.New("poll interval is incorrect, invalid duration")
errSupportedOS = errors.New(typeStr + " is only supported on Windows.")
)

func isValidDuration(durationStr string) bool {
_, err := time.ParseDuration(durationStr)
return err == nil
}

// Validate validates all portions of the relevant config
func (c *ADConfig) Validate() error {

Expand All @@ -47,7 +53,7 @@ func (c *ADConfig) Validate() error {
return errInvalidDN
}

if c.PollInterval < 0 {
if !isValidDuration(c.PollInterval) {
return errInvalidPollInterval
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/receiver/activedirectoryinvreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,23 @@ func TestValidate(t *testing.T) {
config: ADConfig{
DN: "CN=Guest,CN=Users,DC=exampledomain,DC=com",
Attributes: []string{"name"},
PollInterval: 60,
PollInterval: "60s",
},
},
{
name: "Valid Config DC",
config: ADConfig{
DN: "DC=exampledomain,DC=com",
Attributes: []string{"name"},
PollInterval: 60,
PollInterval: "60s",
},
},
{
name: "Valid Config OU",
config: ADConfig{
DN: "CN=Guest,OU=Users,DC=exampledomain,DC=com",
Attributes: []string{"name"},
PollInterval: 60,
PollInterval: "24h",
},
},
{
Expand All @@ -62,7 +62,7 @@ func TestValidate(t *testing.T) {
config: ADConfig{
DN: "CN=Users,DC=exampledomain,DC=com",
Attributes: []string{"name"},
PollInterval: -1,
PollInterval: "test",
},
expectedErr: errInvalidPollInterval,
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/receiver/activedirectoryinvreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func NewFactory() receiver.Factory {
func CreateDefaultConfig() component.Config {
return &ADConfig{
DN: "CN=Guest,CN=Users,DC=exampledomain,DC=com",
Attributes: []string{"name"},
PollInterval: 60,
Attributes: []string{"name", "mail", "department", "manager", "memberOf"},
PollInterval: "24h",
}
}

Expand Down

0 comments on commit a154fe1

Please sign in to comment.