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

Adding standalone gRPC Go server example for gRPC Tyk plugins. #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@ Golang | Analytics | Manipulate Tyk analytics records

### gRPC Plugin Languages

Language | Phase | Description | Link
-------- | ----- |----------------------------------------------------------------------------------------------------------| ---
gRPC (GoLang) | Pre | Header Injection & Auth example | [Link](plugins/grpc_go-auth-pre_headerinject_authhook)
gRPC (GoLang) | Pre | Invokes an AWS Lambda | [Link](plugins/grpc_go-pre-aws_lambda)
gRPC (Node) | Pre / Autg | Simple NODE example with access to config data Lambda | [Link](plugins/grpc_node-auth-simple)
gRPC (Java) | Auth | Decodes JWT, inserts a claim and resigns it | [Link](plugins/grpc_java-auth-jwt_decoder_repackager)
gRPC (Java) | Post | Inserts Metadata from the portal requested key as an HTTP header | [Link](plugins/grpc_java-post-insert_metadata_as_header)
gRPC (.NET) | Auth | Performs auth check against a SQL server | [Link](plugins/grpc_dotnet-auth_sql_basicauth )
gRPC (Ruby) | Pre | Modifies HTTP header | [Link](plugins/grpc_ruby-pre-header_modify)
gRPC (Python) | Pre | Inserts a HTTP header | [Link](plugins/grpc_python-pre-insert_header)
Language | Phase | Description | Link
-------- | ----- |--------------------------------------------------------------------| ---
gRPC (GoLang) | Pre | Header Injection Example (standalone gRPC Server) | [Link](plugins/grpc-go-pre-headerinject)
gRPC (GoLang) | Pre | Header Injection & Auth example (bundle) | [Link](plugins/grpc_go-auth-pre_headerinject_authhook)
gRPC (GoLang) | Pre | Invokes an AWS Lambda | [Link](plugins/grpc_go-pre-aws_lambda)
gRPC (Node) | Pre / Autg | Simple NODE example with access to config data Lambda | [Link](plugins/grpc_node-auth-simple)
gRPC (Java) | Auth | Decodes JWT, inserts a claim and resigns it | [Link](plugins/grpc_java-auth-jwt_decoder_repackager)
gRPC (Java) | Post | Inserts Metadata from the portal requested key as an HTTP header | [Link](plugins/grpc_java-post-insert_metadata_as_header)
gRPC (.NET) | Auth | Performs auth check against a SQL server | [Link](plugins/grpc_dotnet-auth_sql_basicauth )
gRPC (Ruby) | Pre | Modifies HTTP header | [Link](plugins/grpc_ruby-pre-header_modify)
gRPC (Python) | Pre | Inserts a HTTP header | [Link](plugins/grpc_python-pre-insert_header)

### Javascript Plugin Languages

Expand Down
113 changes: 113 additions & 0 deletions plugins/grpc-go-pre-headerinject/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
module github.com/TykTechnologies/tyk-grpc-go-basicauth-jwt

go 1.22

replace github.com/jeffail/tunny v0.1.4 => github.com/Jeffail/tunny v0.1.4

replace github.com/hashicorp/terraform v1.0.1 => github.com/hashicorp/terraform v0.14.11

require (
github.com/TykTechnologies/again v0.0.0-20190805133618-6ad301e7eaed // indirect
github.com/TykTechnologies/circuitbreaker v2.2.2+incompatible // indirect
github.com/TykTechnologies/drl v0.0.0-20231218155806-88e4363884a2 // indirect
github.com/TykTechnologies/goautosocket v0.0.0-20190430121222-97bfa5e7e481 // indirect
github.com/TykTechnologies/gojsonschema v0.0.0-20170222154038-dcb3e4bb7990 // indirect
github.com/TykTechnologies/gorpc v0.0.0-20241016124253-606484472fbb // indirect
github.com/TykTechnologies/goverify v0.0.0-20220808203004-1486f89e7708 // indirect
github.com/TykTechnologies/leakybucket v0.0.0-20170301023702-71692c943e3c // indirect
github.com/TykTechnologies/murmur3 v0.0.0-20230310161213-aad17efd5632 // indirect
github.com/TykTechnologies/openid2go v0.1.2 // indirect
github.com/bshuster-repo/logrus-logstash-hook v1.1.0 // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/cenk/backoff v2.2.1+incompatible // indirect
github.com/clbanning/mxj v1.8.4 // indirect
github.com/evalphobia/logrus_sentry v0.8.2 // indirect
github.com/gemnasium/logrus-graylog-hook v2.0.7+incompatible // indirect
github.com/go-jose/go-jose/v3 v3.0.3 // indirect
github.com/gocraft/health v0.0.0-20170925182251-8675af27fef0 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/justinas/alice v1.2.0 // indirect
github.com/kelseyhightower/envconfig v1.4.0 // indirect
github.com/lonelycode/osin v0.0.0-20160423095202-da239c9dacb6 // indirect
github.com/mavricknz/ldap v0.0.0-20160227184754-f5a958005e43 // indirect
github.com/miekg/dns v1.1.62 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/openzipkin/zipkin-go v0.4.3 // indirect
github.com/oschwald/maxminddb-golang v1.13.1 // indirect
github.com/paulbellamy/ratecounter v0.2.0 // indirect
github.com/pires/go-proxyproto v0.7.0 // indirect
github.com/pmylund/go-cache v2.1.0+incompatible // indirect
github.com/robertkrimen/otto v0.4.0 // indirect
github.com/rs/cors v1.11.1 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/stretchr/testify v1.9.0 // indirect; test
github.com/uber/jaeger-client-go v2.30.1-0.20220110192849-8d8e8fcfd04d+incompatible // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/net v0.29.0
google.golang.org/grpc v1.66.2
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/vmihailenco/msgpack.v2 v2.9.2 // indirect
gopkg.in/xmlpath.v2 v2.0.0-20150820204837-860cbeca3ebc // indirect
)

require github.com/TykTechnologies/tyk v2.9.5+incompatible

require (
github.com/Jeffail/gabs v1.4.0 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver v1.5.0 // indirect
github.com/agext/levenshtein v1.2.2 // indirect
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a // indirect
github.com/franela/goblin v0.0.0-20211003143422-0a4f594942bf // indirect
github.com/franela/goreq v0.0.0-20171204163338-bcd34c9993f8 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/getsentry/raven-go v0.2.0 // indirect
github.com/go-redis/redis v6.15.9+incompatible // indirect
github.com/go-test/deep v1.0.8 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/hcl/v2 v2.9.1 // indirect
github.com/hashicorp/terraform v1.0.1 // indirect
github.com/huandu/xstrings v1.5.0 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/jeffail/tunny v0.1.4 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/lonelycode/go-uuid v0.0.0-20141202165402-ed3ca8a15a93 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mavricknz/asn1-ber v0.0.0-20151103223136-b9df1c2f4213 // indirect
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/newrelic/go-agent v2.13.0+incompatible // indirect
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/onsi/gomega v1.33.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rogpeppe/go-internal v1.11.1-0.20231026093722-fa6a31e0812c // indirect
github.com/uber/jaeger-lib v2.4.2-0.20210604143007-135cf5605a6d+incompatible // indirect
github.com/x-cray/logrus-prefixed-formatter v0.5.2 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/zclconf/go-cty v1.8.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/term v0.24.0 // indirect
golang.org/x/text v0.18.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240604185151-ef581f913117 // indirect
gopkg.in/Masterminds/sprig.v2 v2.22.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
rsc.io/letsencrypt v0.0.3 // indirect
)
58 changes: 58 additions & 0 deletions plugins/grpc-go-pre-headerinject/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"github.com/TykTechnologies/tyk/coprocess"
"golang.org/x/net/context"
"google.golang.org/grpc"
"log"
"net"
)

// Dispatcher implementation
type Dispatcher struct{}

// Dispatch will be called on every request:
func (d *Dispatcher) Dispatch(ctx context.Context, object *coprocess.Object) (*coprocess.Object, error) {
log.Println("Receiving object:", object)

// We dispatch the object based on the hook name (as specified in the manifest file), these functions are in hooks.go:
switch object.HookName {
case "MyPreHook":
log.Println("MyPreHook is called!")
return MyPreHook(object)
}

log.Println("Unknown hook: ", object.HookName)

return object, nil
}

// DispatchEvent will be called when a Tyk event is triggered:
func (d *Dispatcher) DispatchEvent(ctx context.Context, event *coprocess.Event) (*coprocess.EventReply, error) {
return &coprocess.EventReply{}, nil
}

const (
ListenAddress = ":5555"
)

// MyPreHook performs a header injection:
func MyPreHook(object *coprocess.Object) (*coprocess.Object, error) {
object.Request.SetHeaders = map[string]string{
"Myheader": "Myvalue",
}

return object, nil
}

func main() {
lis, err := net.Listen("tcp", ListenAddress)
if err != nil {
log.Fatalf("Failed to listen: %v", err)
}

log.Printf("starting grpc server on %v", ListenAddress)
s := grpc.NewServer()
coprocess.RegisterDispatcherServer(s, &Dispatcher{})
s.Serve(lis)
}
23 changes: 23 additions & 0 deletions plugins/grpc-go-pre-headerinject/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# grpc-go-pre-headerinject

Allows a user to spin up a gRPC server in Go to service requests from Tyk.
Leverages the updated Protobuf definitions from Tyk. The included file `tykapidef_classic_grpc_go_header_injection.json` can be imported into Tyk and leverages the plugin.

## Quickstart
```zsh
# Navigate into the repo
cd grpc-go-pre-headerinject

go mod tidy

go run main.go
```

The gRPC server will now be running on localhost:5555. Import the file `tykapidef_classic_grpc_go_header_injection.json` into your Tyk installation and then invoke the resultant API to see the header injected.

## Note
The go.mod is sourced from [here](https://github.com/TykTechnologies/tyk/blob/master/go.mod).

The flatmap error can be overcome with the following replace statement in the go.mod file:

`replace github.com/hashicorp/terraform v1.0.1 => github.com/hashicorp/terraform v0.14.11`
Loading