Skip to content

Commit

Permalink
Merge pull request #24 from seantcanavan/shore_up_examples
Browse files Browse the repository at this point in the history
shore up the list of examples
  • Loading branch information
seantcanavan authored Jan 4, 2024
2 parents ab0d1a6 + d43f105 commit 577175a
Show file tree
Hide file tree
Showing 18 changed files with 840 additions and 52 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export CONNECTION_STRING="mongodb+srv://USER:[email protected]/?retryWrites=true&w=majority"
export LAMBDA_JWT_ROUTER_HMAC_SECRET="6F697765757279746F776965757279746977756F65727974696F75776572797475696F776579727475696F776579727475696F776579727475696F7779657274"
export STAGE="development"
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
all: format tidy build test

build:
build: tidy
env GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" ./...

format:
Expand Down
120 changes: 111 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Supports automatically replying to HTTP OPTIONS requests so calls from browsers
Go HTTP router library for AWS API Gateway-invoked Lambda Functions
Forked from [aquasecurity/lmdrouter](https://github.com/aquasecurity/lmdrouter)

## Installation
1. `go get github.com/seantcanavan/lambda_jwt_router@latest`


## How to Build locally
1. `make build`
Expand All @@ -16,8 +19,8 @@ Forked from [aquasecurity/lmdrouter](https://github.com/aquasecurity/lmdrouter)
1. `make test`

## How to Use
1. set the environment variable `LAMBDA_JWT_ROUTER_NO_CORS=true` to disable adding a CORS OPTIONS handler to every route automatically
1. If you do not set it manually - the default value will be `*`
1. set the environment variable `LAMBDA_JWT_ROUTER_NO_CORS` to `true` to disable adding a CORS OPTIONS handler to every route automatically
1. If you do not set it manually - the default value will be `false` (all endpoints have CORS added by default)
2. set the environment variable `LAMBDA_JWT_ROUTER_CORS_METHODS` to configure which CORS methods you would like to support
1. If you do not set it manually - the default value will be `*`
3. set the environment variable `LAMBDA_JWT_ROUTER_CORS_ORIGIN` to configure which CORS origins you would like to support
Expand All @@ -26,17 +29,112 @@ Forked from [aquasecurity/lmdrouter](https://github.com/aquasecurity/lmdrouter)
1. If you do not set it manually - the default value will be `*`
5. set the environment variable `LAMBDA_JWT_ROUTER_HMAC_SECRET` to configure the HMAC secret used to encode/decode JWTs
6. See https://github.com/aquasecurity/lmdrouter for the original README and details
7. More TBD coming on how to better utilize the changes I have made

## Sample routing example
## Sample routing example - see `routing_example.go` for more detail
```
var router *lambda_router.Router
func init() {
router = lambda_router.NewRouter("/api")
router.Route("DELETE", "/books/:id", books.DeleteLambda)
router.Route("GET", "/books/:id", books.GetLambda)
router.Route("POST", "/books", books.CreateLambda)
router.Route("PUT", "/books/:id", books.UpdateLambda)
}
func main() {
// if we're running this in staging or production, we want to use the lambda handler on startup
environment := os.Getenv("STAGE")
if environment == "staging" || environment == "production" {
lambda.Start(router.Handler)
} else { // else, we want to start an HTTP server to listen for local development
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Printf("Ready to listen and serve on port %s", port)
err := http.ListenAndServe(":"+port, http.HandlerFunc(router.ServeHTTP))
if err != nil {
panic(fmt.Sprintf("http.ListAndServe error %s", err))
}
}
}
```

## Sample JWT example - see `jwt_example.go` for more detail
```
var router *lambda_router.Router
func init() {
// implement your own base middleware functions and add to the NewRouter declaration to apply to every route
router = lambda_router.NewRouter("/api", lambda_jwt.InjectLambdaContextMW)
// to configure middleware at the route level, add them singularly to each route
// DecodeStandard will automagically check events.Headers["Authorization"] for a valid JWT.
// It will look for the LAMBDA_JWT_ROUTER_HMAC_SECRET environment variable and use that to decode
// the JWT. If decoding succeeds, it will inject all the standard claims into the context object
// before returning so other callers can access those fields at run time.
router.Route("DELETE", "/books/:id", books.DeleteLambda, lambda_jwt.DecodeStandard)
router.Route("GET", "/books/:id", books.GetLambda, lambda_jwt.DecodeStandard)
router.Route("POST", "/books", books.CreateLambda, lambda_jwt.DecodeStandard)
router.Route("PUT", "/books/:id", books.UpdateLambda, lambda_jwt.DecodeStandard)
}
## Sample JWT example
func main() {
// if we're running this in staging or production, we want to use the lambda handler on startup
environment := os.Getenv("STAGE")
if environment == "staging" || environment == "production" {
lambda.Start(router.Handler)
} else { // else, we want to start an HTTP server to listen for local development
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Printf("Ready to listen and serve on port %s", port)
err := http.ListenAndServe(":"+port, http.HandlerFunc(router.ServeHTTP))
if err != nil {
panic(fmt.Sprintf("http.ListAndServe error %s", err))
}
}
}
```

## Sample middleware example - see `middleware_example.go` for more detail
```
var router *lambda_router.Router
## Sample middleware example
func init() {
// implement your own base middleware functions and add to the NewRouter declaration to apply to every route
router = lambda_router.NewRouter("/api", lambda_jwt.InjectLambdaContextMW)
// to configure middleware at the route level, add them singularly to each route
router.Route("DELETE", "/books/:id", books.DeleteLambda, lambda_jwt.LogRequestMW)
router.Route("GET", "/books/:id", books.GetLambda, lambda_jwt.LogRequestMW)
router.Route("POST", "/books", books.CreateLambda, lambda_jwt.LogRequestMW)
router.Route("PUT", "/books/:id", books.UpdateLambda, lambda_jwt.LogRequestMW)
}
func main() {
// if we're running this in staging or production, we want to use the lambda handler on startup
environment := os.Getenv("STAGE")
if environment == "staging" || environment == "production" {
lambda.Start(router.Handler)
} else { // else, we want to start an HTTP server to listen for local development
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Printf("Ready to listen and serve on port %s", port)
err := http.ListenAndServe(":"+port, http.HandlerFunc(router.ServeHTTP))
if err != nil {
panic(fmt.Sprintf("http.ListAndServe error %s", err))
}
}
}
## All tests are passing
```
go test -v ./...
=== RUN TestAllowOptionsMW
=== RUN TestAllowOptionsMW/verify_empty_OPTIONS_req_succeeds
=== RUN TestAllowOptionsMW/verify_OPTIONS_req_succeeds_with_invalid_JWT_for_AllowOptions
Expand Down Expand Up @@ -114,20 +212,25 @@ go test -v ./...
--- PASS: TestVerifyJWT/verify_err_when_parsing_invalid_jwt (0.00s)
--- PASS: TestVerifyJWT/verify_err_when_parsing_expired_token_with_valid_jwt (0.00s)
PASS
ok github.com/seantcanavan/lambda_jwt_router/lambda_jwt 0.005s
ok github.com/seantcanavan/lambda_jwt_router/lambda_jwt 0.004s
? github.com/seantcanavan/lambda_jwt_router/lambda_util [no test files]
=== RUN TestMarshalLambdaRequest
=== RUN TestMarshalLambdaRequest/verify_MarshalReq_correctly_adds_the_JSON_string_to_the_request_body
--- PASS: TestMarshalLambdaRequest (0.00s)
--- PASS: TestMarshalLambdaRequest/verify_MarshalReq_correctly_adds_the_JSON_string_to_the_request_body (0.00s)
=== RUN Test_UnmarshalReq
=== RUN Test_UnmarshalReq/valid_path&query_input
=== RUN Test_UnmarshalReq/valid_empty_input
=== RUN Test_UnmarshalReq/valid_input_unset_values
=== RUN Test_UnmarshalReq/invalid_path&query_input
=== RUN Test_UnmarshalReq/valid_body_input,_not_base64
=== RUN Test_UnmarshalReq/invalid_body_input,_not_base64
=== RUN Test_UnmarshalReq/valid_body_input,_base64
=== RUN Test_UnmarshalReq/invalid_body_input,_base64
--- PASS: Test_UnmarshalReq (0.00s)
--- PASS: Test_UnmarshalReq/valid_path&query_input (0.00s)
--- PASS: Test_UnmarshalReq/valid_empty_input (0.00s)
--- PASS: Test_UnmarshalReq/valid_input_unset_values (0.00s)
--- PASS: Test_UnmarshalReq/invalid_path&query_input (0.00s)
--- PASS: Test_UnmarshalReq/valid_body_input,_not_base64 (0.00s)
--- PASS: Test_UnmarshalReq/invalid_body_input,_not_base64 (0.00s)
Expand Down Expand Up @@ -247,5 +350,4 @@ ok github.com/seantcanavan/lambda_jwt_router/lambda_jwt 0.005s
--- PASS: TestRouter/Overlapping_routes (0.00s)
PASS
ok github.com/seantcanavan/lambda_jwt_router/lambda_router 0.004s
? github.com/seantcanavan/lambda_jwt_router/lambda_util [no test files]
```
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ require (

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/sync v0.4.0 // indirect
golang.org/x/text v0.13.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,46 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc=
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe h1:iruDEfMl2E6fbMZ9s0scYfZQ84/6SPL6zC8ACM2oIL0=
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=
github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4=
github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8=
github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM=
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d h1:splanxYIlg+5LfHAM6xpdFEAYOk8iySO56hMFq6uLyA=
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.mongodb.org/mongo-driver v1.13.1 h1:YIc7HTYsKndGK4RFzJ3covLz1byri52x0IoMB0Pt/vk=
go.mongodb.org/mongo-driver v1.13.1/go.mod h1:wcDf1JBCXy2mOW0bWHwO/IOYqdca1MPCwDtFu/Z9+eo=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ=
golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -49,6 +61,8 @@ golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
Expand Down
Loading

0 comments on commit 577175a

Please sign in to comment.