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

add grpc_go-auth-mongo-accesstoken example #13

Open
wants to merge 3 commits 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Golang | Pre | Checks Basic Auth creds against an AWS DynamoDB instance

Language | Phase | Description | Link
-------- | ----- |----------------------------------------------------------------------------------------------------------| ---
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"
2 changes: 2 additions & 0 deletions plugins/grpc_go-auth-mongo-accesstoken/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.env
vendor
10 changes: 10 additions & 0 deletions plugins/grpc_go-auth-mongo-accesstoken/Compilation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
```bash
rm -rf go.mod go.sum vendor/
go1.16.15 mod init tyk-grpc-plugin
go1.16.15 mod edit -replace github.com/jensneuse/graphql-go-tools=github.com/TykTechnologies/graphql-go-tools@v1.6.2-0.20221026084245-1fc4f5ca74bb
go1.16.15 get github.com/TykTechnologies/tyk@767c8b336fa5433174648bc2532edd7611ce9be0
go1.16.15 get go.mongodb.org/mongo-driver
<Write and finalize code>
go1.16.15 mod tidy
go1.16.15 mod vendor
```
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.16-alpine as builder

WORKDIR /go/src/app

RUN GRPC_HEALTH_PROBE_VERSION=v0.4.14 && \
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
12 changes: 12 additions & 0 deletions plugins/grpc_go-auth-mongo-accesstoken/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module tyk-grpc-plugin

go 1.16

replace github.com/jensneuse/graphql-go-tools => github.com/TykTechnologies/graphql-go-tools v1.6.2-0.20221026084245-1fc4f5ca74bb

require (
github.com/TykTechnologies/tyk v1.9.2-0.20221129200023-767c8b336fa5
github.com/sirupsen/logrus v1.9.0
go.mongodb.org/mongo-driver v1.11.1
google.golang.org/grpc v1.51.0
)
Loading