Skip to content

Commit

Permalink
alter never install behaviour to allow install if no Npcap DLL is alr…
Browse files Browse the repository at this point in the history
…eady installed
  • Loading branch information
efd6 committed Aug 1, 2023
1 parent bda92fa commit 55ad07f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packetbeat/_meta/config/windows_npcap.yml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
#packetbeat.npcap:
# # If a specific local version of Npcap is required, installation by packetbeat
# # can be blocked by setting never_install to true. No action is taken if this
# # option is set to true.
# # option is set to true unless no Npcap is already installed.
# never_install: false
{{- end -}}
29 changes: 19 additions & 10 deletions packetbeat/beater/install_npcap.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -61,13 +62,21 @@ func installNpcap(b *beat.Beat, cfg *conf.C) error {
}

log := logp.NewLogger("npcap_install")
canInstall, err := canInstallNpcap(b, cfg, log)
if err != nil {
return err
}
if !canInstall {
log.Warn("npcap installation/upgrade disabled by user")
return nil
// Only check whether we have been requested to never_install if there
// is already an Npcap installation present. This should not be necessary,
// but the start-up logic of packetbeat is tightly coupled to the presence
// of a backing sniffer. This should really not be necessary, but the changes
// to modify this behaviour are non-trivial, so just avoid the issue.
isInstalled := strings.HasPrefix(npcap.Version(), "Npcap version")
if isInstalled {
canInstall, err := canInstallNpcap(b, cfg, log)
if err != nil {
return err
}
if !canInstall {
log.Warn("npcap installation/upgrade disabled by user")
return nil
}
}

ctx, cancel := context.WithTimeout(context.Background(), installTimeout)
Expand Down Expand Up @@ -121,12 +130,12 @@ func canInstallNpcap(b *beat.Beat, rawcfg *conf.C, log *logp.Logger) (bool, erro
if len(cfg.Streams) == 0 {
// We have no stream to monitor, so we don't need to install
// anything. We may be in the middle of a config check.
log.Info("cannot install because no configured stream")
log.Debug("cannot install because no configured stream")
return false, nil
}
for _, c := range cfg.Streams {
if c.NeverInstall {
log.Infof("cannot install because %s has never_install set to true", c.Type)
log.Debugf("cannot install because %s has never_install set to true", c.Type)
return false, nil
}
}
Expand All @@ -140,7 +149,7 @@ func canInstallNpcap(b *beat.Beat, rawcfg *conf.C, log *logp.Logger) (bool, erro
return false, fmt.Errorf("failed to unpack npcap config from packetbeat configuration: %w", err)
}
if cfg.NeverInstall {
log.Infof("cannot install because %s has never_install set to true", cfg.Type)
log.Debugf("cannot install because %s has never_install set to true", cfg.Type)
}
return !cfg.NeverInstall, err
}
2 changes: 1 addition & 1 deletion packetbeat/docs/packetbeat-options.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ On Windows {beatname} requires an Npcap DLL installation. This is provided by {b
for users of the Elastic Licenced version. In some cases users may wish to use
their own installed version. In order to do this the `packetbeat.npcap.never_install`
option can be used. Setting this option to `true` will not attempt to install the
bundled Npcap library on start-up.
bundled Npcap library on start-up unless no Npcap is already installed.

[source,yaml]
------------------------------------------------------------------------------
Expand Down

0 comments on commit 55ad07f

Please sign in to comment.