Skip to content
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

Disable network fetch of PCI database by default #29

Merged
merged 1 commit into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ PCI database could not be loaded.

> `pcidb`'s default behaviour is to first search for pci-ids DB files on the
> local host system in well-known filesystem paths. If `pcidb` cannot find a
> pci-ids DB file on the local host system, it will then fetch a current
> pci-ids DB file from the network. You can disable this network-fetching
> behaviour with the `pcidb.WithDisableNetworkFetch()` function or set the
> `PCIDB_DISABLE_NETWORK_FETCH` to a non-0 value.
> pci-ids DB file on the local host system, you can configure `pcidb` to fetch
> a current pci-ids DB file from the network. You can enable this
> network-fetching behaviour with the `pcidb.WithEnableNetworkFetch()` function
> or set the `PCIDB_ENABLE_NETWORK_FETCH` to a non-0 value.

The `pcidb.PCIDB` struct contains a number of fields that may be queried for
PCI information:
Expand Down
24 changes: 12 additions & 12 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ import (
// Concrete merged set of configuration switches that get passed to pcidb
// internal functions
type context struct {
chroot string
cacheOnly bool
cachePath string
path string
disableNetworkFetch bool
searchPaths []string
chroot string
cacheOnly bool
cachePath string
path string
enableNetworkFetch bool
searchPaths []string
}

func contextFromOptions(merged *WithOption) *context {
ctx := &context{
chroot: *merged.Chroot,
cacheOnly: *merged.CacheOnly,
cachePath: getCachePath(),
disableNetworkFetch: *merged.DisableNetworkFetch,
path: *merged.Path,
searchPaths: make([]string, 0),
chroot: *merged.Chroot,
cacheOnly: *merged.CacheOnly,
cachePath: getCachePath(),
enableNetworkFetch: *merged.EnableNetworkFetch,
path: *merged.Path,
searchPaths: make([]string, 0),
}
ctx.setSearchPaths()
return ctx
Expand Down
2 changes: 1 addition & 1 deletion discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (db *PCIDB) load(ctx *context) error {
}
}
if foundPath == "" {
if ctx.disableNetworkFetch {
if !ctx.enableNetworkFetch {
return ERR_NO_DB
}
// OK, so we didn't find any host-local copy of the pci-ids DB file. Let's
Expand Down
8 changes: 4 additions & 4 deletions internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func TestMergeOptions(t *testing.T) {
if opts.CacheOnly == nil {
t.Fatalf("Expected opts.CacheOnly to be non-nil.")
}
if opts.DisableNetworkFetch == nil {
t.Fatalf("Expected opts.DisableNetworkFetch to be non-nil.")
if opts.EnableNetworkFetch == nil {
t.Fatalf("Expected opts.EnableNetworkFetch to be non-nil.")
}
if opts.Path == nil {
t.Fatalf("Expected opts.DirectPath to be non-nil.")
Expand All @@ -46,8 +46,8 @@ func TestLoad(t *testing.T) {
// Start with a context with no search paths intentionally to test the
// disabling of the network fetch
ctx := &context{
disableNetworkFetch: true,
searchPaths: []string{},
enableNetworkFetch: false,
searchPaths: []string{},
}
db := &PCIDB{}
err := db.load(ctx)
Expand Down
28 changes: 13 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,9 @@ type WithOption struct {
// looking for any non ~/.cache/pci.ids filepaths (which is useful when we
// want to test the fetch-from-network code paths
CacheOnly *bool
// Disables the default behaviour of fetching a pci-ids from a known
// location on the network if no local pci-ids DB files can be found.
// Useful for secure environments or environments with no network
// connectivity.
DisableNetworkFetch *bool
// Enables fetching a pci-ids from a known location on the network if no
// local pci-ids DB files can be found.
EnableNetworkFetch *bool
// Path points to the absolute path of a pci.ids file in a non-standard
// location.
Path *string
Expand All @@ -110,8 +108,8 @@ func WithDirectPath(path string) *WithOption {
return &WithOption{Path: &path}
}

func WithDisableNetworkFetch() *WithOption {
return &WithOption{DisableNetworkFetch: &trueVar}
func WithEnableNetworkFetch() *WithOption {
return &WithOption{EnableNetworkFetch: &trueVar}
}

func mergeOptions(opts ...*WithOption) *WithOption {
Expand All @@ -137,17 +135,17 @@ func mergeOptions(opts ...*WithOption) *WithOption {
defaultCacheOnly = parsed
}
}
defaultDisableNetworkFetch := false
if val, exists := os.LookupEnv("PCIDB_DISABLE_NETWORK_FETCH"); exists {
defaultEnableNetworkFetch := false
if val, exists := os.LookupEnv("PCIDB_ENABLE_NETWORK_FETCH"); exists {
if parsed, err := strconv.ParseBool(val); err != nil {
fmt.Fprintf(
os.Stderr,
"Failed parsing a bool from PCIDB_DISABLE_NETWORK_FETCH "+
"Failed parsing a bool from PCIDB_ENABLE_NETWORK_FETCH "+
"environ value of %s",
val,
)
} else if parsed {
defaultDisableNetworkFetch = parsed
defaultEnableNetworkFetch = parsed
}
}

Expand All @@ -159,8 +157,8 @@ func mergeOptions(opts ...*WithOption) *WithOption {
if opt.CacheOnly != nil {
merged.CacheOnly = opt.CacheOnly
}
if opt.DisableNetworkFetch != nil {
merged.DisableNetworkFetch = opt.DisableNetworkFetch
if opt.EnableNetworkFetch != nil {
merged.EnableNetworkFetch = opt.EnableNetworkFetch
}
if opt.Path != nil {
merged.Path = opt.Path
Expand All @@ -173,8 +171,8 @@ func mergeOptions(opts ...*WithOption) *WithOption {
if merged.CacheOnly == nil {
merged.CacheOnly = &defaultCacheOnly
}
if merged.DisableNetworkFetch == nil {
merged.DisableNetworkFetch = &defaultDisableNetworkFetch
if merged.EnableNetworkFetch == nil {
merged.EnableNetworkFetch = &defaultEnableNetworkFetch
}
if merged.Path == nil {
merged.Path = &path
Expand Down