generated from honeycombio/.github
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
maint: Move config to it's own package (#139)
## Which problem is this PR solving? Config is currently part of the assemblers sub-package. As more of the agent will utilise configuration, it makes sense to move it to it's own package for easier use. ## Short description of the changes - Move assemblers/config.go to config.config.go - Replace usage of assemblers.config with config.config - Replace direct use of flags with config properties - Update PacketSource and BpFilter names so they are exported - Replace all pointer references of config with actual values ## How to verify that this has the expected result Agent continues to work as it has with no external changes.
- Loading branch information
1 parent
a719936
commit 7571de8
Showing
6 changed files
with
94 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package source | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/google/gopacket" | ||
"github.com/google/gopacket/pcap" | ||
"github.com/honeycombio/ebpf-agent/config" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
func NewPacketSource(config config.Config) (*gopacket.PacketSource, error) { | ||
var packetSource *gopacket.PacketSource | ||
var err error | ||
|
||
switch config.PacketSource { | ||
case "pcap": | ||
packetSource, err = newPcapPacketSource(config) | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("Failed to setup pcap handle") | ||
} | ||
// TODO: other data sources (eg afpacket, pfring, etc) | ||
default: | ||
log.Fatal().Str("packet_source", config.PacketSource).Msg("Unknown packet source") | ||
} | ||
|
||
packetSource.Lazy = config.Lazy | ||
packetSource.NoCopy = true | ||
|
||
return packetSource, nil | ||
} | ||
|
||
func newPcapPacketSource(config config.Config) (*gopacket.PacketSource, error) { | ||
log.Info(). | ||
Str("interface", config.Interface). | ||
Int("snaplen", config.Snaplen). | ||
Bool("promiscuous", config.Promiscuous). | ||
Str("bpf_filter", config.BpfFilter). | ||
Msg("Configuring pcap packet source") | ||
handle, err := pcap.OpenLive(config.Interface, int32(config.Snaplen), config.Promiscuous, time.Second) | ||
if err != nil { | ||
log.Fatal(). | ||
Err(err). | ||
Msg("Failed to open a pcap handle") | ||
return nil, err | ||
} | ||
if config.BpfFilter != "" { | ||
if err = handle.SetBPFFilter(config.BpfFilter); err != nil { | ||
log.Fatal(). | ||
Err(err). | ||
Msg("Error setting BPF filter") | ||
return nil, err | ||
} | ||
} | ||
|
||
return gopacket.NewPacketSource( | ||
handle, | ||
handle.LinkType(), | ||
), nil | ||
} |