Skip to content

Commit

Permalink
add grpc_go-auth-mongo-accesstoken example
Browse files Browse the repository at this point in the history
Source: https://github.com/armujahid/tyk-grpc-go-mongo-accesstoken

This example is containerized and can easily deployed on k8s after tweaking business logic
  • Loading branch information
armujahid committed Oct 19, 2022
1 parent 48c7efc commit 3e533c4
Show file tree
Hide file tree
Showing 12 changed files with 670 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ GoLang | Post-Auth | OAuth2 Introspection
GoLang | Post-Auth | Authorizes request against OPA | [Link](plugins/go-postauth-opa_integration)
Golang | Post + Auth | Dummy one to test the 2 hooks in go | [Link](plugins/go-auth-multiple_hook_example)
Golang | Pre | Checks Basic Auth creds against an AWS DynamoDB instance | [Link](plugins/go-auth-basicauth_dynamodb)
gRPC (GoLang) | Auth | Performs auth check(accesstoken verification) against MongoDB | [Link](plugins/grpc_go-auth-mongo-accesstoken)
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 (Java) | Auth | Decodes JWT, inserts a claim and resigns it | [Link](plugins/grpc_java-auth-jwt_decoder_repackager)
Expand Down
3 changes: 3 additions & 0 deletions plugins/grpc_go-auth-mongo-accesstoken/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
MONGODB_URL="mongoconnectionstring"
DATABASE="something"
COLLECTION="AccessToken"
1 change: 1 addition & 0 deletions plugins/grpc_go-auth-mongo-accesstoken/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
24 changes: 24 additions & 0 deletions plugins/grpc_go-auth-mongo-accesstoken/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM golang:1.15-alpine as builder

WORKDIR /go/src/app

RUN GRPC_HEALTH_PROBE_VERSION=v0.4.12 && \
wget -qO/bin/grpc_health_probe https://github.com/grpc-ecosystem/grpc-health-probe/releases/download/${GRPC_HEALTH_PROBE_VERSION}/grpc_health_probe-linux-amd64 && \
chmod +x /bin/grpc_health_probe

COPY go.mod ./
COPY go.sum ./
RUN go mod download

COPY *.go ./

RUN CGO_ENABLED=0 go build -o /go/bin/app

FROM gcr.io/distroless/static-debian11

COPY --from=builder /bin/grpc_health_probe ./grpc_health_probe
COPY --from=builder /go/bin/app /

EXPOSE 9111

CMD [ "/app" ]
20 changes: 20 additions & 0 deletions plugins/grpc_go-auth-mongo-accesstoken/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)
Copyright © 2022 Abdul Rauf, https://armujahid.me <abdulraufmujahid@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
68 changes: 68 additions & 0 deletions plugins/grpc_go-auth-mongo-accesstoken/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# How to start gRPC server:


```bash
cp .env.example .env
```


modify .env



```bash
docker-compose up --build
```

Tyk configurations:

1) tyk.conf:

```
"coprocess_options": {
"enable_coprocess": true,
"coprocess_grpc_server": "tcp://host.docker.internal:9111"
},
```

ensure that tyk can reach the gRPC server

2) Use apidef.json in tyk

# Example DB config
Create a collection in mongo with this object (I have tested this with Loopback 3 `AccessToken` model which already has compatible schema)
```json
{
"_id":"asdfasdfasdf32dsafdasdfas",
"ttl":1209600,
"created":"2022-08-30T09:20:20.910+00:00"
}
```

# Testing:
```bash
curl -s 'http://localhost:8082/mongo-auth/get' -H "Authorization: asdfasdfasdf32dsafdasdfas"
```
Output if token is valid:
```
{
"args": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip",
"Authorization": "asdfasdfasdf32dsafdasdfas",
"Host": "httpbin.org",
"User-Agent": "curl/7.81.0",
"X-Amzn-Trace-Id": "Root=1-6315b555-4a26f3f923cde5aa3f596b6f"
},
"origin": "172.28.0.1, <external ip>",
"url": "http://httpbin.org/get"
}
```

Output if token is invalid:
```
{
"error": "Access forbidden"
}
```
43 changes: 43 additions & 0 deletions plugins/grpc_go-auth-mongo-accesstoken/apidef.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "Tyk Mongo Auth API",
"api_id": "mongoauth",
"org_id": "default",
"definition": {
"location": "header",
"key": "version"
},
"use_keyless": false,
"enable_coprocess_auth": true,
"version_data": {
"not_versioned": true,
"versions": {
"Default": {
"name": "Default"
}
}
},
"custom_middleware": {
"pre": [],
"post": [],
"post_key_auth": [],
"auth_check": {
"name": "MongoAuth",
"path": "",
"require_session": false
},
"response": [],
"driver": "grpc",
"id_extractor": {
"extract_from": "header",
"extract_with": "value",
"extractor_config": {
"header_name": "Authorization"
}
}
},
"proxy": {
"listen_path": "/mongo-auth/",
"target_url": "http://httpbin.org",
"strip_listen_path": true
}
}
15 changes: 15 additions & 0 deletions plugins/grpc_go-auth-mongo-accesstoken/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: "3"
services:
tyk:
build:
context: .
env_file:
- .env
ports:
- "9111:9111"
healthcheck:
test: ["CMD", "/grpc_health_probe", "-addr=:9111"]
interval: 30s
timeout: 10s
retries: 3
start_period: 5s
36 changes: 36 additions & 0 deletions plugins/grpc_go-auth-mongo-accesstoken/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module github.com/TykTechnologies/tyk-grpc-go-basicauth-jwt

go 1.13

require (
github.com/Sirupsen/logrus v1.9.0 // indirect
github.com/TykTechnologies/gojsonschema v0.0.0-20170222154038-dcb3e4bb7990 // indirect
github.com/TykTechnologies/tyk v2.6.1-0.20190726143653-b94b86c275c9+incompatible // indirect
github.com/TykTechnologies/tyk-protobuf v0.0.0-20190418154138-1df3dfe2f1d4
github.com/clbanning/mxj v1.8.4 // indirect
github.com/dgrijalva/jwt-go v3.2.1-0.20180921172315-3af4c746e1c2+incompatible // indirect
github.com/franela/goblin v0.0.0-20211003143422-0a4f594942bf // indirect
github.com/franela/goreq v0.0.0-20171204163338-bcd34c9993f8 // indirect
github.com/lonelycode/go-uuid v0.0.0-20141202165402-ed3ca8a15a93 // indirect
github.com/lonelycode/osin v0.0.0-20160423095202-da239c9dacb6 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/onsi/gomega v1.20.2 // indirect
github.com/pmylund/go-cache v2.1.0+incompatible // indirect
github.com/sirupsen/logrus v1.4.3-0.20190701143506-07a84ee7412e
github.com/x-cray/logrus-prefixed-formatter v0.5.2 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
go.mongodb.org/mongo-driver v1.10.1
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3 // indirect
google.golang.org/genproto v0.0.0-20190425155659-357c62f0e4bb // indirect
google.golang.org/grpc v1.19.0
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 // indirect
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc // indirect
)

replace (
github.com/Sirupsen/logrus v1.9.0 => github.com/sirupsen/logrus v1.9.0
github.com/sirupsen/logrus v1.9.0 => github.com/Sirupsen/logrus v1.9.0
)
Loading

0 comments on commit 3e533c4

Please sign in to comment.