Skip to content

Commit

Permalink
fix: wfx does not start if built without plugin tag
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Adler <[email protected]>
  • Loading branch information
michaeladler committed Jul 26, 2024
1 parent 036d0ad commit c91bd19
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 76 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- `wfx` would not start if it was built without plugins support

### Changed

- Use zstd instead of xz to compress release tarballs
Expand Down
70 changes: 0 additions & 70 deletions cmd/wfx/cmd/root/plugins.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build plugin

package root

/*
Expand All @@ -11,17 +9,7 @@ package root
*/

import (
"os"
"path"
"path/filepath"
"sort"

"github.com/Southclaws/fault"
"github.com/knadh/koanf/v2"
"github.com/rs/zerolog/log"
"github.com/siemens/wfx/internal/errutil"
"github.com/siemens/wfx/middleware"
"github.com/siemens/wfx/middleware/plugin"
)

func LoadNorthboundPlugins(chQuit chan error) ([]middleware.IntermediateMW, error) {
Expand All @@ -31,61 +19,3 @@ func LoadNorthboundPlugins(chQuit chan error) ([]middleware.IntermediateMW, erro
func LoadSouthboundPlugins(chQuit chan error) ([]middleware.IntermediateMW, error) {
return loadPluginSet(clientPluginsDirFlag, chQuit)
}

func loadPluginSet(flag string, chQuit chan error) ([]middleware.IntermediateMW, error) {
var pluginsDir string
k.Read(func(k *koanf.Koanf) {
pluginsDir = k.String(flag)
})
if pluginsDir == "" {
return []middleware.IntermediateMW{}, nil
}
return errutil.Wrap2(createPluginMiddlewares(pluginsDir, chQuit))
}

func createPluginMiddlewares(pluginsDir string, chQuit chan error) ([]middleware.IntermediateMW, error) {
pluginMws, err := loadPlugins(pluginsDir)
if err != nil {
return nil, fault.Wrap(err)
}
result := make([]middleware.IntermediateMW, 0, len(pluginMws))
for _, p := range pluginMws {
mw, err := plugin.NewMiddleware(p, chQuit)
if err != nil {
return nil, fault.Wrap(err)
}
result = append(result, mw)
}
return result, nil
}

func loadPlugins(dir string) ([]plugin.Plugin, error) {
log.Debug().Msg("Loading plugins")
entries, err := os.ReadDir(dir)
if err != nil {
return nil, fault.Wrap(err)
}

result := make([]plugin.Plugin, 0, len(entries))
for _, entry := range entries {
if !entry.IsDir() {
dest, err := filepath.EvalSymlinks(path.Join(dir, entry.Name()))
if err != nil {
return nil, fault.Wrap(err)
}
info, err := os.Stat(dest)
if err != nil {
return nil, fault.Wrap(err)
}
// check if file is executable
if (info.Mode() & 0o111) != 0 {
result = append(result, plugin.NewFBPlugin(dest))
} else {
log.Warn().Str("dest", dest).Msg("Ignoring non-executable file")
}
}
}
sort.Slice(result, func(i int, j int) bool { return result[i].Name() < result[j].Name() })
log.Debug().Int("count", len(result)).Msg("Loaded plugins")
return result, nil
}
16 changes: 10 additions & 6 deletions cmd/wfx/cmd/root/plugins_disabled.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ package root
import (
"errors"

"github.com/knadh/koanf/v2"
"github.com/siemens/wfx/middleware"
)

func LoadNorthboundPlugins(chan error) ([]middleware.IntermediateMW, error) {
return nil, errors.New("this binary was built without plugin support")
}

func LoadSouthboundPlugins(chan error) ([]middleware.IntermediateMW, error) {
return nil, errors.New("this binary was built without plugin support")
func loadPluginSet(flag string, _chQuit chan error) ([]middleware.IntermediateMW, error) {
var pluginsDir string
k.Read(func(k *koanf.Koanf) {
pluginsDir = k.String(flag)
})
if pluginsDir != "" {
return nil, errors.New("this binary was built without plugin support")
}
return []middleware.IntermediateMW{}, nil
}
19 changes: 19 additions & 0 deletions cmd/wfx/cmd/root/plugins_disabled_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,36 @@ package root
import (
"testing"

"github.com/knadh/koanf/v2"
"github.com/stretchr/testify/assert"
)

func TestLoadNorthboundPlugins(t *testing.T) {
k.Write(func(k *koanf.Koanf) {
_ = k.Set(mgmtPluginsDirFlag, "/plugins")
})
mw, err := LoadNorthboundPlugins(nil)
assert.Nil(t, mw)
assert.ErrorContains(t, err, "this binary was built without plugin support")
}

func TestLoadNorthboundPlugins_None(t *testing.T) {
mw, err := LoadNorthboundPlugins(nil)
assert.NoError(t, err)
assert.Empty(t, mw)
}

func TestLoadSouthboundPlugins(t *testing.T) {
k.Write(func(k *koanf.Koanf) {
_ = k.Set(clientPluginsDirFlag, "/plugins")
})
mw, err := LoadSouthboundPlugins(nil)
assert.Nil(t, mw)
assert.ErrorContains(t, err, "this binary was built without plugin support")
}

func TestLoadSouthboundPlugins_None(t *testing.T) {
mw, err := LoadSouthboundPlugins(nil)
assert.NoError(t, err)
assert.Empty(t, mw)
}
83 changes: 83 additions & 0 deletions cmd/wfx/cmd/root/plugins_enabled.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//go:build plugin

package root

/*
* SPDX-FileCopyrightText: 2023 Siemens AG
*
* SPDX-License-Identifier: Apache-2.0
*
* Author: Michael Adler <[email protected]>
*/

import (
"os"
"path"
"path/filepath"
"sort"

"github.com/Southclaws/fault"
"github.com/knadh/koanf/v2"
"github.com/rs/zerolog/log"
"github.com/siemens/wfx/internal/errutil"
"github.com/siemens/wfx/middleware"
"github.com/siemens/wfx/middleware/plugin"
)

func loadPluginSet(flag string, chQuit chan error) ([]middleware.IntermediateMW, error) {
var pluginsDir string
k.Read(func(k *koanf.Koanf) {
pluginsDir = k.String(flag)
})
if pluginsDir == "" {
return []middleware.IntermediateMW{}, nil
}
return errutil.Wrap2(createPluginMiddlewares(pluginsDir, chQuit))
}

func createPluginMiddlewares(pluginsDir string, chQuit chan error) ([]middleware.IntermediateMW, error) {
pluginMws, err := loadPlugins(pluginsDir)
if err != nil {
return nil, fault.Wrap(err)
}
result := make([]middleware.IntermediateMW, 0, len(pluginMws))
for _, p := range pluginMws {
mw, err := plugin.NewMiddleware(p, chQuit)
if err != nil {
return nil, fault.Wrap(err)
}
result = append(result, mw)
}
return result, nil
}

func loadPlugins(dir string) ([]plugin.Plugin, error) {
log.Debug().Msg("Loading plugins")
entries, err := os.ReadDir(dir)
if err != nil {
return nil, fault.Wrap(err)
}

result := make([]plugin.Plugin, 0, len(entries))
for _, entry := range entries {
if !entry.IsDir() {
dest, err := filepath.EvalSymlinks(path.Join(dir, entry.Name()))
if err != nil {
return nil, fault.Wrap(err)

Check warning on line 66 in cmd/wfx/cmd/root/plugins_enabled.go

View check run for this annotation

Codecov / codecov/patch

cmd/wfx/cmd/root/plugins_enabled.go#L66

Added line #L66 was not covered by tests
}
info, err := os.Stat(dest)
if err != nil {
return nil, fault.Wrap(err)

Check warning on line 70 in cmd/wfx/cmd/root/plugins_enabled.go

View check run for this annotation

Codecov / codecov/patch

cmd/wfx/cmd/root/plugins_enabled.go#L70

Added line #L70 was not covered by tests
}
// check if file is executable
if (info.Mode() & 0o111) != 0 {
result = append(result, plugin.NewFBPlugin(dest))
} else {
log.Warn().Str("dest", dest).Msg("Ignoring non-executable file")
}
}
}
sort.Slice(result, func(i int, j int) bool { return result[i].Name() < result[j].Name() })
log.Debug().Int("count", len(result)).Msg("Loaded plugins")
return result, nil
}

0 comments on commit c91bd19

Please sign in to comment.