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

#27 channelpattern #28

Merged
merged 7 commits into from
Feb 20, 2019
Merged
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
11 changes: 8 additions & 3 deletions action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/project-flogo/contrib/activity/rest"
coreactivity "github.com/project-flogo/core/activity"
"github.com/project-flogo/core/api"
"github.com/project-flogo/core/engine/channels"
microapi "github.com/project-flogo/microgateway/api"
"github.com/project-flogo/microgateway/internal/testing/activity"
"github.com/project-flogo/microgateway/internal/testing/trigger"
Expand Down Expand Up @@ -294,13 +295,17 @@ func TestMicrogatewayChannelPattern(t *testing.T) {
assert.Nil(t, err)

action, err := handler.NewAction(&Action{}, map[string]interface{}{
"pattern": "DefaultChannelPattern",
"useJWT": false,
"useCircuitBreaker": false,
"pattern": "DefaultChannelPattern",
"useJWT": false,
"channel": "test",
"value": "test",
})
assert.Nil(t, err)
assert.NotNil(t, action)

_, err = channels.New("test", 5)
assert.Nil(t, err)

e, err := api.NewEngine(app)
assert.Nil(t, err)
e.Start()
Expand Down
83 changes: 83 additions & 0 deletions docs/default-channel-pattern.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Gateway using Default Channel Pattern
Link: https://github.com/project-flogo/microgateway/internal/pattern/DefaultChannelPattern.json
Examples:
https://github.com/project-flogo/microgateway/examples/api/default-channel-pattern
https://github.com/project-flogo/microgateway/examples/json/default-channel-pattern



This recipe is a gateway using the defult channel pattern which uses JWT.
This receipe uses 2 triggers.
1. REST trigger:- For calling the channel pattern
Link: https://github.com/project-flogo/contrib/trigger/rest

2. Channel Trigger:- For calling second microgateway action
Link: https://github.com/project-flogo/contrib/trigger/channel

Channel Pattern:
Channels allow one action to call another action. Channel pattern checks for JWT conditions.
If valid ... it runs corresponding channel activity and message is sent over the channel defined by its name.
Channel trigger then receives the message from channel and corresponding action (eg: log activity) is called.


#Channel Activity
| Name | Type | Description |
|:-----------|:--------|:--------------|
| channel | string | The channel to put the value on |
| value | string | The value to put on channel |

Link: https://github.com/project-flogo/contrib/activity/channel


#JWT
| Name | Type | Description |
|:-----------|:--------|:--------------|
| token | string | The raw token |
| key | string | The key used to sign the token |
| signingMethod | string | The signing method used (HMAC, ECDSA, RSA, RSAPSS) |
| issuer | string | The 'iss' standard claim to match against |
| subject | string | The 'sub' standard claim to match against |
| audience | string | The 'aud' standard claim to match against |

Link: https://github.com/project-flogo/microgateway/activity/jwt


## Installation
* Install [Go](https://golang.org/)

## Setup
```bash
git clone https://github.com/project-flogo/microgateway
cd microgateway/examples/api/default-http-pattern
```

## Testing
Start the gateway:
```bash
go run main.go
```


### Request is successful
Run the following command:
```bash
curl --request GET http://localhost:9096/endpoint -H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJNYXNobGluZyIsImlhdCI6MTU0NDEzMTYxOCwiZXhwIjoxNTc1NjY3NjE4LCJhdWQiOiJ3d3cubWFzaGxpbmcuaW8iLCJzdWIiOiJ0ZW1wdXNlckBtYWlsLmNvbSJ9.wgunWSIJqieRKsmObATT2VEHMMzkKte6amuUlhc1oKs"
```

You should see:
```json
{"response":"Success!"}
```
On the server screen, you get 200 response code and log service outputs "Output: Test log message service invoked"


### JWT token is invalid
Run the following command:
```bash
curl --request GET http://localhost:9096/endpoint -H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJNYXNobGluZyIsImlhdCI6MTU0NDEzMTYxOCwiZXhwIjoxNTc1NjY3NjE4LCJhdWQiOiJ3d3cubWFzaGxpbmcuaW8iLCJzdWIiOiJ0ZW1wdXNlckBtYWlsLmNvbSJ9.wgunWSIJqieRKsmObATT2VEHMMzkKte6amuUlhc1oK"
```

You should see:
```json
{"errorMessage":"","validationMessage":"signature is invalid"}
```
114 changes: 114 additions & 0 deletions docs/default-http-pattern.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# Gateway using Default Http Pattern
This recipe is a gateway using the defult http pattern which uses JWT, Rate Limiter, and Circuit Breaker.
Link: https://github.com/project-flogo/microgateway/internal/pattern/DefaultHttpPattern.json
Examples:
https://github.com/project-flogo/microgateway/examples/api/default-http-pattern
https://github.com/project-flogo/microgateway/examples/json/default-http-pattern


Http Pattern:
Pattern checks for JWT, Ratelimiter and CircuitBreaker conditions.
If valid ... it runs corresponding activity.
This receipe uses REST trigger.
1. REST trigger:- For calling the http pattern
Link: https://github.com/project-flogo/contrib/trigger/rest


#Circuit Breaker Service:
| Name | Type | Description |
|:-----------|:--------|:--------------|
| mode | string | The tripping mode: 'a' for contiguous errors, 'b' for errors within a time period, 'c' for contiguous errors within a time period, and 'd' for a probabilistic smart circuit breaker mode. Defaults to mode 'a' |
| threshold | number | The number of errors required for tripping|
| period | number | Number of seconds in which errors have to occur for the circuit breaker to trip. Applies to modes 'b' and 'c'|
| timeout | number | Number of seconds that the circuit breaker will remain tripped. Applies to modes 'a', 'b', 'c'|

Link: https://github.com/project-flogo/microgateway/activity/circuitbreaker


#Rate Limiter
| Name | Type | Description |
|:-----------|:--------|:--------------|
| limit | string | Limit can be specifed in the format of "limit-period". Valid periods are 'S', 'M' & 'H' to represent Second, Minute & Hour. Example: "10-S" represents 10 request/second |

Link: https://github.com/project-flogo/microgateway/activity/ratelimiter



#JWT
| Name | Type | Description |
|:-----------|:--------|:--------------|
| token | string | The raw token |
| key | string | The key used to sign the token |
| signingMethod | string | The signing method used (HMAC, ECDSA, RSA, RSAPSS) |
| issuer | string | The 'iss' standard claim to match against |
| subject | string | The 'sub' standard claim to match against |
| audience | string | The 'aud' standard claim to match against |

Link: https://github.com/project-flogo/microgateway/activity/jwt



## Installation
* Install [Go](https://golang.org/)

## Setup
```bash
git clone https://github.com/project-flogo/microgateway
cd microgateway/examples/api/default-http-pattern
```

## Testing
Start the gateway:
```bash
go run main.go
```
and test below scenario.

In another terminal start the server:
```bash
go run main.go -server
```

### Request is successful
Run the following command:
```bash
curl --request GET http://localhost:9096/endpoint -H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJNYXNobGluZyIsImlhdCI6MTU0NDEzMTYxOCwiZXhwIjoxNTc1NjY3NjE4LCJhdWQiOiJ3d3cubWFzaGxpbmcuaW8iLCJzdWIiOiJ0ZW1wdXNlckBtYWlsLmNvbSJ9.wgunWSIJqieRKsmObATT2VEHMMzkKte6amuUlhc1oKs"
```

You should see:
```json
{"category":{"id":0,"name":"string"},"id":1,"name":"sally","photoUrls":["string"],"status":"available","tags":[{"id":0,"name":"string"}]}
```

### JWT token is invalid
Run the following command:
```bash
curl --request GET http://localhost:9096/endpoint -H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJNYXNobGluZyIsImlhdCI6MTU0NDEzMTYxOCwiZXhwIjoxNTc1NjY3NjE4LCJhdWQiOiJ3d3cubWFzaGxpbmcuaW8iLCJzdWIiOiJ0ZW1wdXNlckBtYWlsLmNvbSJ9.wgunWSIJqieRKsmObATT2VEHMMzkKte6amuUlhc1oK"
```

You should see:
```json
{"errorMessage":"","validationMessage":"signature is invalid"}
```

### Rate limit is exceeded
Run the following command faster the 1 per second:
```bash
curl --request GET http://localhost:9096/endpoint -H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJNYXNobGluZyIsImlhdCI6MTU0NDEzMTYxOCwiZXhwIjoxNTc1NjY3NjE4LCJhdWQiOiJ3d3cubWFzaGxpbmcuaW8iLCJzdWIiOiJ0ZW1wdXNlckBtYWlsLmNvbSJ9.wgunWSIJqieRKsmObATT2VEHMMzkKte6amuUlhc1oKs"
```

You should see:
```json
{"status":"Rate Limit Exceeded - The service you have requested is over the allowed limit."}
```

### Circuit breaker tripped
Stop the server and run the following command 6 times:
```bash
curl --request GET http://localhost:9096/endpoint -H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJNYXNobGluZyIsImlhdCI6MTU0NDEzMTYxOCwiZXhwIjoxNTc1NjY3NjE4LCJhdWQiOiJ3d3cubWFzaGxpbmcuaW8iLCJzdWIiOiJ0ZW1wdXNlckBtYWlsLmNvbSJ9.wgunWSIJqieRKsmObATT2VEHMMzkKte6amuUlhc1oKs"
```

You should see:
```json
{"error":"circuit breaker tripped"}
```
60 changes: 60 additions & 0 deletions examples/api/default-channel-pattern/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Gateway using Default Channel Pattern
This recipe is a gateway using the defult channel pattern which uses JWT.

#Channel Activity
| Name | Type | Description |
|:-----------|:--------|:--------------|
| channel | string | The channel to put the value on |
| value | string | The value to put on channel |


#JWT
| Name | Type | Description |
|:-----------|:--------|:--------------|
| token | string | The raw token |
| key | string | The key used to sign the token |
| signingMethod | string | The signing method used (HMAC, ECDSA, RSA, RSAPSS) |
| issuer | string | The 'iss' standard claim to match against |
| subject | string | The 'sub' standard claim to match against |
| audience | string | The 'aud' standard claim to match against |


## Installation
* Install [Go](https://golang.org/)

## Setup
```bash
git clone https://github.com/project-flogo/microgateway
cd microgateway/examples/api/default-http-pattern
```

## Testing
Start the gateway:
```bash
go run main.go
```


### Request is successful
Run the following command:
```bash
curl --request GET http://localhost:9096/endpoint -H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJNYXNobGluZyIsImlhdCI6MTU0NDEzMTYxOCwiZXhwIjoxNTc1NjY3NjE4LCJhdWQiOiJ3d3cubWFzaGxpbmcuaW8iLCJzdWIiOiJ0ZW1wdXNlckBtYWlsLmNvbSJ9.wgunWSIJqieRKsmObATT2VEHMMzkKte6amuUlhc1oKs"
```

You should see:
```json
{"response":"Success!"}
```
On the server screen, you get 200 response code and log service outputs "Output: Test log message service invoked"


### JWT token is invalid
Run the following command:
```bash
curl --request GET http://localhost:9096/endpoint -H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJNYXNobGluZyIsImlhdCI6MTU0NDEzMTYxOCwiZXhwIjoxNTc1NjY3NjE4LCJhdWQiOiJ3d3cubWFzaGxpbmcuaW8iLCJzdWIiOiJ0ZW1wdXNlckBtYWlsLmNvbSJ9.wgunWSIJqieRKsmObATT2VEHMMzkKte6amuUlhc1oK"
```

You should see:
```json
{"errorMessage":"","validationMessage":"signature is invalid"}
```
20 changes: 20 additions & 0 deletions examples/api/default-channel-pattern/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (

"github.com/project-flogo/core/engine"
"github.com/project-flogo/microgateway/examples"
_ "github.com/project-flogo/microgateway/activity/circuitbreaker"
_ "github.com/project-flogo/microgateway/activity/jwt"
_ "github.com/project-flogo/contrib/activity/channel"
)


func main() {

e, err := examples.DefaultChannelPattern()
if err != nil {
panic(err)
}
engine.RunEngine(e)
}
3 changes: 2 additions & 1 deletion examples/api/default-http-pattern/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ var (

const reply = `{
"id": 1,
"category": { "id": 0,
"category": {
"id": 0,
"name": "string"
},
"name": "sally",
Expand Down
64 changes: 64 additions & 0 deletions examples/examples.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package examples

import (
"github.com/project-flogo/contrib/activity/log"
"github.com/project-flogo/contrib/activity/rest"
channeltrigger "github.com/project-flogo/contrib/trigger/channel"
trigger "github.com/project-flogo/contrib/trigger/rest"
"github.com/project-flogo/core/api"
"github.com/project-flogo/core/engine"
"github.com/project-flogo/core/engine/channels"
"github.com/project-flogo/microgateway"
microapi "github.com/project-flogo/microgateway/api"
)
Expand Down Expand Up @@ -162,3 +165,64 @@ func DefaultHTTPPattern() (engine.Engine, error) {

return api.NewEngine(app)
}

func DefaultChannelPattern() (engine.Engine, error) {
app := api.NewApp()

trg := app.NewTrigger(&trigger.Trigger{}, &trigger.Settings{Port: 9096})
handler, err := trg.NewHandler(&trigger.HandlerSettings{
Method: "GET",
Path: "/endpoint",
})
if err != nil {
panic(err)
}

_, err = handler.NewAction(&microgateway.Action{}, map[string]interface{}{
"pattern": "DefaultChannelPattern",
"useJWT": true,
"jwtSigningMethod": "HMAC",
"jwtKey": "qwertyuiopasdfghjklzxcvbnm789101",
"jwtAud": "www.mashling.io",
"jwtIss": "Mashling",
"jwtSub": "tempuser@mail.com",
"channel": "test",
"value": "test",
})
if err != nil {
panic(err)
}

// channel
_, err = channels.New("test", 5)
if err != nil {
panic(err)
}

gateway := microapi.New("Log")
service := gateway.NewService("log", &log.Activity{})
service.SetDescription("Invoking test Log service")
step := gateway.NewStep(service)
step.AddInput("message", "Output: Test log message service invoked")
response := gateway.NewResponse(false)
response.SetCode(200)
settings, err := gateway.AddResource(app)
if err != nil {
panic(err)
}

channeltrg := app.NewTrigger(&channeltrigger.Trigger{}, nil)
channelhandler, err := channeltrg.NewHandler(&channeltrigger.HandlerSettings{
Channel: "test",
})
if err != nil {
panic(err)
}

_, err = channelhandler.NewAction(&microgateway.Action{}, settings)
if err != nil {
panic(err)
}

return api.NewEngine(app)
}
Loading