-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support for external middleware plugins
This commit introduces possibility to integrate external middleware plugins. These plugins have the possibility to modify incoming HTTP requests prior to routing. This includes modifying headers, routing and denying requests. Due to the lack of a proper plugin system in Go, external middleware plugins are implemented as standalone sub-processes. They communicate with wfx via protobuf message over stdin/stdout, hence it's crucial for the plugin to read from stdin promptly to avoid blocking wfx. An example plugin written in Go is included in the `example/plugin` subdirectory, taking special care that stdin does not block. Signed-off-by: Michael Adler <[email protected]>
- Loading branch information
1 parent
976875f
commit b642140
Showing
52 changed files
with
2,887 additions
and
112 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,7 +73,7 @@ jobs: | |
--health-retries 20 | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- run: go test -timeout 180s -race -coverprofile=coverage.out -covermode=atomic -tags testing,integration,postgres,sqlite ./... | ||
- run: go test -timeout 180s -race -coverprofile=coverage.out -covermode=atomic -tags testing,integration,postgres,sqlite,plugin ./... | ||
env: | ||
PGHOST: postgres | ||
PGPORT: 5432 | ||
|
@@ -109,7 +109,7 @@ jobs: | |
--health-retries 20 | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- run: go test -timeout 180s -race -coverprofile=coverage.out -covermode=atomic -tags testing,integration,mysql,sqlite ./... | ||
- run: go test -timeout 180s -race -coverprofile=coverage.out -covermode=atomic -tags testing,integration,mysql,sqlite,plugin ./... | ||
env: | ||
MYSQL_DATABASE: wfx | ||
MYSQL_ROOT_PASSWORD: root | ||
|
@@ -196,22 +196,23 @@ jobs: | |
- uses: dominikh/[email protected] | ||
with: | ||
install-go: false | ||
build-tags: sqlite,testing | ||
build-tags: sqlite,testing,plugin | ||
- name: golangci-lint | ||
uses: golangci/golangci-lint-action@v3 | ||
with: | ||
version: latest | ||
args: --build-tags=sqlite,testing | ||
args: --build-tags=sqlite,testing,plugin | ||
skip-cache: true | ||
|
||
generate: | ||
name: Generate Code | ||
runs-on: ubuntu-latest | ||
container: | ||
image: quay.io/goswagger/swagger | ||
image: archlinux | ||
steps: | ||
- name: Install packages | ||
run: pacman -Syu --noconfirm python-yaml git just go flatbuffers go-swagger gofumpt | ||
- uses: actions/checkout@v4 | ||
- run: apk add --no-cache py3-yaml git just bash go | ||
- name: Disable git security features | ||
run: git config --global safe.directory '*' | ||
- uses: brokeyourbike/go-mockery-action@v0 | ||
|
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 |
---|---|---|
|
@@ -18,6 +18,7 @@ builds: | |
- sqlite | ||
- postgres | ||
- mysql | ||
- plugin | ||
flags: | ||
- -trimpath | ||
- -mod=readonly | ||
|
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,68 @@ | ||
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/rs/zerolog/log" | ||
"github.com/siemens/wfx/middleware" | ||
"github.com/siemens/wfx/middleware/plugin" | ||
) | ||
|
||
func createPluginMiddlewares(pluginsDir string) ([]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) | ||
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 | ||
} |
Oops, something went wrong.