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 echo middleware #15

Closed
wants to merge 34 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
399c0bd
Add license
Joffref Aug 24, 2022
e9c371c
Create dependency-review.yml
Joffref Aug 24, 2022
2313715
Create codeql.yml
Joffref Aug 24, 2022
a659379
Create go-test.yml
Joffref Aug 24, 2022
b2b28d4
Adding push trigger
Joffref Aug 24, 2022
6eed725
Remove unused test, add documentation
Joffref Aug 24, 2022
c805938
Fix test
Joffref Aug 24, 2022
0930f1d
Refactoring package
Joffref Aug 25, 2022
07c9242
fix go mod issue
Joffref Aug 25, 2022
3184a7f
fix inputmethod neeeded by default
Joffref Aug 25, 2022
8b6d1ca
remove unnecessary test
Joffref Aug 25, 2022
f33b061
Resolve flaky test
Joffref Aug 25, 2022
6d6245a
Fix gin handler
Joffref Aug 25, 2022
58e3f57
Enhance gin middleware
Joffref Aug 25, 2022
03be059
Issues fix
Joffref Aug 25, 2022
a907cf1
Add dependabot action (#1)
Joffref Aug 25, 2022
1ba853e
V1.0.1 (#2)
Joffref Aug 26, 2022
7cc0872
Bump github.com/open-policy-agent/opa from 0.43.0 to 0.43.1 (#3)
dependabot[bot] Sep 16, 2022
9a4d376
simplify conditions (#6)
marat-42 Feb 23, 2023
031d93e
Bump golang.org/x/text from 0.3.7 to 0.3.8 (#5)
dependabot[bot] Feb 23, 2023
e5f3f07
Bump golang.org/x/net from 0.0.0-20220225172249-27dd8689420f to 0.7.0…
dependabot[bot] Feb 23, 2023
8aebef2
Bump github.com/gin-gonic/gin from 1.8.1 to 1.9.1 (#9)
dependabot[bot] Jun 3, 2023
fb2e70d
Bump github.com/gofiber/fiber/v2 from 2.36.0 to 2.43.0 (#10)
dependabot[bot] Aug 16, 2023
3e442f4
Bump golang.org/x/net from 0.10.0 to 0.17.0 (#12)
dependabot[bot] Oct 14, 2023
f9809d5
Bump github.com/gofiber/fiber/v2 from 2.43.0 to 2.49.2 (#11)
dependabot[bot] Oct 14, 2023
62f9d33
Added echo middleware
Feb 16, 2024
caac7a6
Added echo middleware
nerdyslacker Feb 16, 2024
be675d2
Updated documentation
Feb 16, 2024
b532346
Updated documentation
nerdyslacker Feb 16, 2024
94aa104
update go mod
Feb 16, 2024
41fbc0c
update go mod
nerdyslacker Feb 16, 2024
db04927
Added tests for echo middleware
nerdyslacker Feb 18, 2024
44f4910
Added tests for echo middleware
nerdyslacker Feb 18, 2024
fdb4c1b
Merge branch 'master' of https://github.com/nerdyslacker/opa-middleware
nerdyslacker Feb 18, 2024
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
Prev Previous commit
Next Next commit
Remove unused test, add documentation
Signed-off-by: Mathis Joffre <[email protected]>
  • Loading branch information
Joffref committed Aug 24, 2022
commit 6eed7255cff323a946041806ee2a279aa994311f
219 changes: 173 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Open Policy Agent Gin Middleware
# Open Policy Agent Middleware

This middleware integrates Open Policy Agent (OPA) to your gin app.
This middleware integrates Open Policy Agent (OPA) to your http/gin/fiber app.
You can use it to enforce policies on endpoints.
You can use OPA as local policy engine, or as a remote policy engine.

Expand All @@ -10,75 +10,202 @@ You can use OPA as local policy engine, or as a remote policy engine.
go get github.com/Joffref/opa-middleware
```

## Usage
### Local policy engine
## Usage Generic with OPA and HTTP

### Local based policy engine

```go
package main

import (
"github.com/Joffref/gin-opa-middleware"
"github.com/gin-gonic/gin"
"github.com/Joffref/opa-middleware/config"
"github.com/Joffref/opa-middleware/middleware/http"
"net/http"
)

var policy = `
package example.authz
var Policy = `
package policy

default allow := false
default allow = false

allow {
input.method == "GET"
input.path = "/api/v1/users"
input.method = "GET"
}`

type H struct {
Name string
}

func (h *H) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World : " + h.Name))
}

func main() {
r := gin.Default()
r.Use(opa.Middleware(context.Background(), &opa.Config{
Policy: policy,
Query: "data.example.authz.allow",
InputCreationMethod: func(c *gin.Context) (map[string]interface{}, error) {
return map[string]interface{}{
"method": c.Request.Method,
}, nil
handler, err := httpmiddleware.NewHTTPMiddleware(
&config.Config{
Policy: Policy,
Query: "data.policy.allow",
InputCreationMethod: func(r *http.Request) (map[string]interface{}, error) {
return map[string]interface{}{
"path": r.URL.Path,
"method": r.Method,
}, nil
},
ExceptedResult: true,
DeniedStatusCode: 403,
DeniedMessage: "Forbidden",
},
ExceptedResult: true,
DeniedStatusCode: 403,
Debug: true,
Logger: log.New(gin.DefaultWriter, "[opa] ", log.LstdFlags),
}))
r.GET("/", func(c *gin.Context) {
c.String(200, "Hello World!")
})
r.Run()
&H{
Name: "John Doe",
},
)
if err != nil {
panic(err)
}
err = http.ListenAndServe(":8080", handler)
if err != nil {
return
}
}
```

### Remote based policy engine

```go
package main

import (
"github.com/Joffref/opa-middleware/config"
"github.com/Joffref/opa-middleware/middleware/http"
"net/http"
)

type H struct {
Name string
}

func (h *H) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World : " + h.Name))
}

func main() {
handler, err := httpmiddleware.NewHTTPMiddleware(
&config.Config{
URL: "http://localhost:8181",
Query: "data.policy.allow",
InputCreationMethod: func(r *http.Request) (map[string]interface{}, error) {
return map[string]interface{}{
"path": r.URL.Path,
"method": r.Method,
}, nil
},
ExceptedResult: true,
DeniedStatusCode: 403,
DeniedMessage: "Forbidden",
},
&H{
Name: "John Doe",
},
)
if err != nil {
panic(err)
}
err = http.ListenAndServe(":8080", handler)
if err != nil {
return
}
}
```
### Remote policy engine

## Usage with GIN
```go
package main

import (
"github.com/Joffref/gin-opa-middleware"
"github.com/Joffref/opa-middleware/config"
ginmiddleware "github.com/Joffref/opa-middleware/middleware/gin"
"github.com/gin-gonic/gin"
)

func main() {
r := gin.Default()
r.Use(opa.Middleware(context.Background(), &opa.Config{
URL: "http://localhost:8181",
Query: "data.example.authz.allow",
InputCreationMethod: func(c *gin.Context) (map[string]interface{}, error) {
return map[string]interface{}{
"method": c.Request.Method,
}, nil
},
ExceptedResult: true,
DeniedStatusCode: 403,
Debug: true,
Logger: log.New(gin.DefaultWriter, "[opa] ", log.LstdFlags),
}))
r.GET("/", func(c *gin.Context) {
c.String(200, "Hello World!")
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
err := r.Run(":8080")
middleware, err := ginmiddleware.NewGinMiddleware(
&config.Config{
URL: "https://opa.example.com/",
Query: "data.policy.allow",
},
func(c *gin.Context) (map[string]interface{}, error) {
return map[string]interface{}{
"path": c.Request.URL.Path,
"method": c.Request.Method,
}, nil
},
)
if err != nil {
return
return
}
r.Use(middleware.Use())
err = r.Run(":8080")
if err != nil {
return
}
}
```

## Usage with Fiber
```go
package main

import (
"github.com/Joffref/opa-middleware/config"
fibermiddleware "github.com/Joffref/opa-middleware/middleware/fiber"
"github.com/gofiber/fiber/v2"
"log"
"time"
)

func main() {
app := fiber.New()
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Hello World!")
})

middleware, err := fibermiddleware.NewFiberMiddleware(&config.Config{
URL: "http://localhost:8080/",
Query: "data.policy.allow",
DeniedStatusCode: 403,
DeniedMessage: "Forbidden",
Headers: map[string]string{
"Content-Type": "application/json",
},
IgnoredHeaders: []string{
"X-Request-Id",
},
Debug: true,
Logger: log.New(log.Writer(), "", log.LstdFlags),
ExceptedResult: true,
Timeout: 5 * time.Second,
},
func(c *fiber.Ctx) (map[string]interface{}, error) {
return map[string]interface{}{
"path": c.Path(),
"method": c.Method(),
}, nil
},
)
if err != nil {
return
}
app.Use(middleware.Use())
err = app.Listen(":3000")
if err != nil {
return
}
}
```
117 changes: 0 additions & 117 deletions middleware/fiber/middleware_test.go

This file was deleted.

Loading