Skip to content

Commit

Permalink
first realease
Browse files Browse the repository at this point in the history
  • Loading branch information
rucciva committed Nov 3, 2023
1 parent 77efe8a commit 0ee3e2a
Show file tree
Hide file tree
Showing 27 changed files with 1,039 additions and 213 deletions.
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
**
!*.go
!go.mod
!go.sum
!kong.conf
57 changes: 57 additions & 0 deletions .github/workflows/docker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: docker

on:
push:
tags:
- "v*"

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
release:
runs-on: ubuntu-latest

steps:
- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Log in to the Container registry
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- id: docker_meta
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}}
tags: |
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
- id: docker_build
name: Build and push Docker image
uses: docker/build-push-action@v3
with:
platforms: linux/amd64
push: true
tags: ${{ steps.docker_meta.outputs.tags }}
labels: ${{ steps.docker_meta.outputs.labels }}

- name: Install Cosign
uses: sigstore/cosign-installer@main

- name: Sign image with a key
run: |
cosign sign --key env://COSIGN_PRIVATE_KEY ${IMAGE_NAME,,}@${IMAGE_DIGEST} --yes
env:
IMAGE_NAME: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}}
IMAGE_DIGEST: ${{ steps.docker_build.outputs.digest }}
COSIGN_PRIVATE_KEY: ${{secrets.COSIGN_PRIVATE_KEY}}
COSIGN_PASSWORD: ${{secrets.COSIGN_PASSWORD}}
27 changes: 27 additions & 0 deletions .github/workflows/go.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: go

on:
push:
tags:
- "v*"

jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: "^1.20.0"
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v4
with:
distribution: goreleaser
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
5 changes: 5 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
version: 1
builds:
- goarch:
- amd64
- arm64
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM golang:1.20 AS builder
WORKDIR /src
COPY . .
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
GOOS=linux go build -o omniplug


FROM kong:3.4.2

COPY --from=builder /src/omniplug /usr/local/bin/omniplug
COPY kong.conf /tmp
RUN cat /tmp/kong.conf >> /etc/kong/kong.conf
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Kong Plugin Omniplug

Omniplug is a Kong plugin to inject proxied HTTP request with data obtained from various source.

Currently supported data source includes:

- [static json/yaml](./testdata/config.yaml#L2-L6) strings.
- [Kong context](./testdata/config.yaml#L7-L20) data.
- [HTTP response](./testdata/config.yaml#L21-L34) data.

Currently supported target of injection includes:

- Kong [context](./testdata/config.yaml#L37-L43).
- Upstream HTTP request [Headers](./testdata/config.yaml#L44-L49).
- Upstream HTTP request [Query Parameters](./testdata/config.yaml#L50-L55).
54 changes: 54 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package main

import (
"encoding/json"
"sync"

"github.com/Kong/go-pdk"
)

type Config struct {
Datasources []configDatasource `json:"datasources" yaml:"datasources"`
Injector configInjector `json:"injector" yaml:"injector"`

omniplug *Omniplug
mut sync.Mutex
}

func (conf *Config) Init() (err error) {
if conf.omniplug != nil {
return
}

conf.mut.Lock()
defer conf.mut.Unlock()
if conf.omniplug == nil {
v, err := json.Marshal(conf)
if err != nil {
return err
}

conf.omniplug = &Omniplug{}
if err = json.Unmarshal(v, conf.omniplug); err != nil {
return err
}

if err = conf.omniplug.Init(); err != nil {
return err
}
}
return
}

func (conf *Config) Access(kong *pdk.PDK) {
if err := conf.Init(); err != nil {
kong.Log.Err(err.Error())
kong.Response.ExitStatus(500)
}

conf.omniplug.Access(kong)
}

func NewConfig() interface{} {
return &Config{}
}
77 changes: 77 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package main

import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"os"
"testing"
"time"

"github.com/Kong/go-pdk/test"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
)

func TestPlugin(t *testing.T) {
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(map[string]interface{}{
"headers": r.Header,
"queryString": r.URL.Query(),
"startedDateTime": time.Now().Format(time.RFC3339Nano),
})
}))
s := os.Getenv("TEST_SERVER_URL")
os.Setenv("TEST_SERVER_URL", svr.URL)
defer os.Setenv("TEST_SERVER_URL", s)

defer svr.Close()
env, err := test.New(t, test.Request{
Method: "GET",
Url: "http://example.com?x_foo=bar&expire_at=" + url.QueryEscape(time.Now().Add(time.Hour).Format(time.RFC3339Nano)),
Headers: http.Header{
"x-foo": []string{"bar"},
},
})
assert.NoError(t, err)

f, err := os.Open("testdata/config.yaml")
assert.NoError(t, err)

config := &Config{}
err = yaml.NewDecoder(f).Decode(config)
assert.NoError(t, err)

var mockbinDate string
for i := 1; i <= 10; i++ {
t.Run(fmt.Sprintf("loop-%d", i), func(t *testing.T) {
env.DoHttps(config)
t.Log(env.ServiceReq.Url)
t.Log(string(env.ClientRes.Body))

assert.Equal(t, 200, env.ClientRes.Status)
assert.Equal(t, 200, env.ClientRes.Status)
assert.Equal(t, "Jon Doe", env.ServiceReq.Headers.Get("username"))
assert.Equal(t, "world", env.ServiceReq.Headers.Get("hello"))
assert.Equal(t, "bar", env.ServiceReq.Headers.Get("foo"))
assert.Equal(t, "bar", env.ServiceReq.Headers.Get("foo-ex"))
date := env.ServiceReq.Headers.Get("date")
if mockbinDate == "" {
mockbinDate = date
} else {
assert.Equal(t, mockbinDate, date)
}
t.Log(date)

u, err := url.Parse(env.ServiceReq.Url)
assert.NoError(t, err)
assert.Equal(t, "Jon Doe", u.Query().Get("username"))
assert.Equal(t, "world", u.Query().Get("hello"))
assert.Equal(t, "bar", u.Query().Get("foo"))
assert.Equal(t, "bar", u.Query().Get("foo-ex"))
assert.Equal(t, date, u.Query().Get("date"))
})
}
}
4 changes: 4 additions & 0 deletions cosign.pub
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE8YShccTf0mlSyO1uGlU/YygGnS+L
O98pv5hEmkw//fL6Q1OgfYpmb0CCY36Bfe936IXMzCCi31YPynQggU48sw==
-----END PUBLIC KEY-----
Loading

0 comments on commit 0ee3e2a

Please sign in to comment.