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

TT-1961: add support for multiple plugin bundles #126

Merged
merged 8 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:

- name: Build and bundle plugins
run: |
make plugin
make plugins
make bundles

- name: Install Venom command line tool
Expand Down
20 changes: 11 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,21 @@ mservctl:
> CGO_ENABLED=0 go build -o ../bin/mservctl
.PHONY: mservctl

start: ## Start runs development environment with mserv and mongo in docker-compose.
> docker-compose up -d
start: ## Start runs development environment with mserv and mongo in docker compose.
> docker compose up -d

stop: ## Stop runs development environment with mserv and mongo in docker-compose.
> docker-compose stop
stop: ## Stop runs development environment with mserv and mongo in docker compose.
> docker compose stop

# Builds Go plugin and moves it into local Tyk instance.
plugin:
> docker-compose run --rm tyk-plugin-compiler plugin.go _$$(date +%s)
.PHONY: plugin
# Builds multiple Go plugins and moves them into local Tyk instance.
plugins:
> @for plugin in plugin.go plugin-2.go; do \
> docker compose run --rm tyk-plugin-compiler $$plugin _$$(date +%s); \
> done
.PHONY: plugins

bundles:
> docker-compose run --rm --workdir /plugin-source --entrypoint "/opt/tyk-gateway/tyk bundle build -y -o bundle.zip" tyk-gateway
> docker compose run --rm --workdir /plugin-source --entrypoint "/opt/tyk-gateway/tyk bundle build -y -o bundle.zip" tyk-gateway
.PHONY: bundles

integration: ## Runs integration test for mserv and mservctl it needs services running.
Expand Down
141 changes: 69 additions & 72 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,40 +168,87 @@ func (a *API) HandleNewBundle(ctx context.Context, filePath, apiID, bundleName s
Added: time.Now(),
}

if len(bdl.Manifest.FileList) != 1 {
return nil, errors.New("only one plugin file file allowed per bundle")
}

pluginContainerID := fmt.Sprintf(FmtPluginContainer, bundleName)

fCont, err := getContainer(pluginContainerID)
if err != nil {
return nil, fmt.Errorf("get container error: %w", err)
}

// Parse name and path.
fName := bdl.Manifest.FileList[0]
pluginPath := path.Join(bdl.Path, fName)
// Iterate over plugin files.
for _, fName := range bdl.Manifest.FileList {
pluginPath := path.Join(bdl.Path, fName)

f, err := os.Open(pluginPath)
if err != nil {
return nil, fmt.Errorf("error opening file: %w", err)
}
f, err := os.Open(pluginPath)
if err != nil {
return nil, fmt.Errorf("error opening file: %w", err)
}

fInfo, err := f.Stat()
if err != nil {
return nil, fmt.Errorf("error stat file: %w", err)
}
fInfo, err := f.Stat()
if err != nil {
return nil, fmt.Errorf("error stat file: %w", err)
}

r := bufio.NewReader(f)
r := bufio.NewReader(f)

item, err := fCont.Put(fInfo.Name(), r, fInfo.Size(), nil)
if err != nil {
return nil, fmt.Errorf("error uploading file: %w", err)
}
item, err := fCont.Put(fInfo.Name(), r, fInfo.Size(), nil)
if err != nil {
return nil, fmt.Errorf("error uploading file: %w", err)
}

// This is an internal URL, must be interpreted by Stow
ref := item.URL().String()
// This is an internal URL, must be interpreted by Stow
ref := item.URL().String()

log.Info("completed storage")

for _, f := range bdl.Manifest.CustomMiddleware.Pre {
p := &storage.Plugin{
UID: uuid.NewString(),
FileName: fName,
FileRef: ref,
Name: f.Name,
Type: coprocess.HookType_Pre,
}

mw.Plugins = append(mw.Plugins, p)
}

for _, f := range bdl.Manifest.CustomMiddleware.Post {
p := &storage.Plugin{
UID: uuid.NewString(),
FileName: fName,
FileRef: ref,
Name: f.Name,
Type: coprocess.HookType_Post,
}

mw.Plugins = append(mw.Plugins, p)
}

for _, f := range bdl.Manifest.CustomMiddleware.PostKeyAuth {
p := &storage.Plugin{
UID: uuid.NewString(),
FileName: fName,
FileRef: ref,
Name: f.Name,
Type: coprocess.HookType_PostKeyAuth,
}

mw.Plugins = append(mw.Plugins, p)
}

if bdl.Manifest.CustomMiddleware.AuthCheck.Name != "" {
p := &storage.Plugin{
UID: uuid.NewString(),
FileName: fName,
FileRef: ref,
Name: bdl.Manifest.CustomMiddleware.AuthCheck.Name,
Type: coprocess.HookType_CustomKeyCheck,
}

mw.Plugins = append(mw.Plugins, p)
}
}

// Store the bundle zip file too, because we can use it again
bF, err := os.Open(filepath.Clean(filePath))
Expand All @@ -222,56 +269,6 @@ func (a *API) HandleNewBundle(ctx context.Context, filePath, apiID, bundleName s
// This is an internal URL, must be interpreted by Stow
mw.BundleRef = bundleData.URL().String()

log.Info("completed storage")

for _, f := range bdl.Manifest.CustomMiddleware.Pre {
p := &storage.Plugin{
UID: uuid.NewString(),
FileName: fName,
FileRef: ref,
Name: f.Name,
Type: coprocess.HookType_Pre,
}

mw.Plugins = append(mw.Plugins, p)
}

for _, f := range bdl.Manifest.CustomMiddleware.Post {
p := &storage.Plugin{
UID: uuid.NewString(),
FileName: fName,
FileRef: ref,
Name: f.Name,
Type: coprocess.HookType_Post,
}

mw.Plugins = append(mw.Plugins, p)
}

for _, f := range bdl.Manifest.CustomMiddleware.PostKeyAuth {
p := &storage.Plugin{
UID: uuid.NewString(),
FileName: fName,
FileRef: ref,
Name: f.Name,
Type: coprocess.HookType_PostKeyAuth,
}

mw.Plugins = append(mw.Plugins, p)
}

if bdl.Manifest.CustomMiddleware.AuthCheck.Name != "" {
p := &storage.Plugin{
UID: uuid.NewString(),
FileName: fName,
FileRef: ref,
Name: bdl.Manifest.CustomMiddleware.AuthCheck.Name,
Type: coprocess.HookType_CustomKeyCheck,
}

mw.Plugins = append(mw.Plugins, p)
}

log.Warning("not loading into dispatcher")
// a.LoadMWIntoDispatcher(mw, bdl.Path)

Expand Down
9 changes: 8 additions & 1 deletion bundles/simple/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"file_list": [
"plugin_v5.2.2_linux_amd64.so"
"plugin_v5.2.2_linux_amd64.so",
"plugin-2_v5.2.2_linux_amd64.so"
],
"custom_middleware": {
"pre": [
Expand All @@ -9,6 +10,12 @@
"path": "plugin_v5.2.2_linux_amd64.so",
"require_session": false,
"raw_body_only": false
},
{
"name": "AddFooBarHeader2",
"path": "plugin-2_v5.2.2_linux_amd64.so",
"require_session": false,
"raw_body_only": false
}
],
"driver": "goplugin"
Expand Down
10 changes: 10 additions & 0 deletions bundles/simple/plugin-2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main
imogenkraak marked this conversation as resolved.
Show resolved Hide resolved

import (
"net/http"
)

// AddFooBarHeader2 adds custom "Foo: Bar" header to the request
func AddFooBarHeader2(_ http.ResponseWriter, r *http.Request) {
r.Header.Add("Foo", "Bar")
}
imogenkraak marked this conversation as resolved.
Show resolved Hide resolved
Loading