Skip to content

Commit 009ca89

Browse files
authored
Merge pull request #6 from eko/compiled-api
Added /v1/compiled API allowing to retrieve compiled policies data
2 parents fea1b05 + da25a0b commit 009ca89

File tree

9 files changed

+318
-14
lines changed

9 files changed

+318
-14
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
@compiled
2+
Feature: compiled
3+
Test compiled-policies APIs
4+
5+
Scenario: List compiled policies
6+
Given I authenticate with username "admin" and password "changeme"
7+
And I send "POST" request to "/v1/principals" with payload:
8+
"""
9+
{
10+
"id": "my-principal",
11+
"attributes": [
12+
{"key": "email", "value": "[email protected]"}
13+
]
14+
}
15+
"""
16+
And the response code should be 200
17+
And I send "POST" request to "/v1/resources" with payload:
18+
"""
19+
{
20+
"id": "post.123",
21+
"kind": "post",
22+
"value": "123",
23+
"attributes": [
24+
{"key": "owner_email", "value": "[email protected]"}
25+
]
26+
}
27+
"""
28+
And the response code should be 200
29+
And I send "POST" request to "/v1/policies" with payload:
30+
"""
31+
{
32+
"id": "my-post-policy",
33+
"resources": [
34+
"post.*"
35+
],
36+
"actions": ["update", "delete"],
37+
"attribute_rules": [
38+
"principal.email == resource.owner_email"
39+
]
40+
}
41+
"""
42+
And the response code should be 200
43+
And I wait "500ms"
44+
When I send "GET" request to "/v1/compiled?filter=policy_id:contains:my-post-policy&sort=action_id:asc"
45+
Then the response code should be 200
46+
And the response should match json:
47+
"""
48+
{
49+
"data": [
50+
{
51+
"action_id": "delete",
52+
"created_at": "2100-01-01T01:00:00Z",
53+
"policy_id": "my-post-policy",
54+
"principal_id": "my-principal",
55+
"resource_kind": "post",
56+
"resource_value": "123",
57+
"updated_at": "2100-01-01T01:00:00Z",
58+
"version": 4102448400
59+
},
60+
{
61+
"action_id": "update",
62+
"created_at": "2100-01-01T01:00:00Z",
63+
"policy_id": "my-post-policy",
64+
"principal_id": "my-principal",
65+
"resource_kind": "post",
66+
"resource_value": "123",
67+
"updated_at": "2100-01-01T01:00:00Z",
68+
"version": 4102448400
69+
}
70+
],
71+
"page": 0,
72+
"size": 100,
73+
"total": 2
74+
}
75+
"""

backend/internal/entity/model/compiled.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package model
33
import "time"
44

55
type CompiledPolicy struct {
6-
PolicyID string `gorm:"index"`
7-
PrincipalID string `gorm:"index"`
8-
ResourceKind string `gorm:"index"`
9-
ResourceValue string `gorm:"index"`
10-
ActionID string `gorm:"index"`
11-
Version int64 `gorm:"index"`
6+
PolicyID string `json:"policy_id" gorm:"index"`
7+
PrincipalID string `json:"principal_id" gorm:"index"`
8+
ResourceKind string `json:"resource_kind" gorm:"index"`
9+
ResourceValue string `json:"resource_value" gorm:"index"`
10+
ActionID string `json:"action_id" gorm:"index"`
11+
Version int64 `json:"version" gorm:"index"`
1212
CreatedAt time.Time `json:"created_at"`
1313
UpdatedAt time.Time `json:"updated_at"`
1414
}

backend/internal/fixtures/initializer.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var (
2121
"actions": {"list", "get"},
2222
"audits": {"get"},
2323
"clients": {"list", "get", "create", "delete"},
24+
"compiled": {"list"},
2425
"policies": {"list", "get", "create", "update", "delete"},
2526
"principals": {"list", "get", "create", "update", "delete"},
2627
"resources": {"list", "get", "create", "update", "delete"},

backend/internal/http/docs/docs.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,45 @@ const docTemplate = `{
762762
}
763763
}
764764
},
765+
"/v1/policies/{identifier}/matches": {
766+
"get": {
767+
"security": [
768+
{
769+
"Authentication": []
770+
}
771+
],
772+
"produces": [
773+
"application/json"
774+
],
775+
"tags": [
776+
"Policy"
777+
],
778+
"summary": "Retrieve compiled policies",
779+
"responses": {
780+
"200": {
781+
"description": "OK",
782+
"schema": {
783+
"type": "array",
784+
"items": {
785+
"$ref": "#/definitions/model.CompiledPolicy"
786+
}
787+
}
788+
},
789+
"404": {
790+
"description": "Not Found",
791+
"schema": {
792+
"$ref": "#/definitions/model.ErrorResponse"
793+
}
794+
},
795+
"500": {
796+
"description": "Internal Server Error",
797+
"schema": {
798+
"$ref": "#/definitions/model.ErrorResponse"
799+
}
800+
}
801+
}
802+
}
803+
},
765804
"/v1/principals": {
766805
"get": {
767806
"security": [
@@ -2161,6 +2200,35 @@ const docTemplate = `{
21612200
}
21622201
}
21632202
},
2203+
"model.CompiledPolicy": {
2204+
"type": "object",
2205+
"properties": {
2206+
"action_id": {
2207+
"type": "string"
2208+
},
2209+
"created_at": {
2210+
"type": "string"
2211+
},
2212+
"policy_id": {
2213+
"type": "string"
2214+
},
2215+
"principal_id": {
2216+
"type": "string"
2217+
},
2218+
"resource_kind": {
2219+
"type": "string"
2220+
},
2221+
"resource_value": {
2222+
"type": "string"
2223+
},
2224+
"updated_at": {
2225+
"type": "string"
2226+
},
2227+
"version": {
2228+
"type": "integer"
2229+
}
2230+
}
2231+
},
21642232
"model.ErrorResponse": {
21652233
"type": "object",
21662234
"properties": {

backend/internal/http/docs/swagger.json

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,45 @@
753753
}
754754
}
755755
},
756+
"/v1/policies/{identifier}/matches": {
757+
"get": {
758+
"security": [
759+
{
760+
"Authentication": []
761+
}
762+
],
763+
"produces": [
764+
"application/json"
765+
],
766+
"tags": [
767+
"Policy"
768+
],
769+
"summary": "Retrieve compiled policies",
770+
"responses": {
771+
"200": {
772+
"description": "OK",
773+
"schema": {
774+
"type": "array",
775+
"items": {
776+
"$ref": "#/definitions/model.CompiledPolicy"
777+
}
778+
}
779+
},
780+
"404": {
781+
"description": "Not Found",
782+
"schema": {
783+
"$ref": "#/definitions/model.ErrorResponse"
784+
}
785+
},
786+
"500": {
787+
"description": "Internal Server Error",
788+
"schema": {
789+
"$ref": "#/definitions/model.ErrorResponse"
790+
}
791+
}
792+
}
793+
}
794+
},
756795
"/v1/principals": {
757796
"get": {
758797
"security": [
@@ -2152,6 +2191,35 @@
21522191
}
21532192
}
21542193
},
2194+
"model.CompiledPolicy": {
2195+
"type": "object",
2196+
"properties": {
2197+
"action_id": {
2198+
"type": "string"
2199+
},
2200+
"created_at": {
2201+
"type": "string"
2202+
},
2203+
"policy_id": {
2204+
"type": "string"
2205+
},
2206+
"principal_id": {
2207+
"type": "string"
2208+
},
2209+
"resource_kind": {
2210+
"type": "string"
2211+
},
2212+
"resource_value": {
2213+
"type": "string"
2214+
},
2215+
"updated_at": {
2216+
"type": "string"
2217+
},
2218+
"version": {
2219+
"type": "integer"
2220+
}
2221+
}
2222+
},
21552223
"model.ErrorResponse": {
21562224
"type": "object",
21572225
"properties": {

backend/internal/http/docs/swagger.yaml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,25 @@ definitions:
286286
updated_at:
287287
type: string
288288
type: object
289+
model.CompiledPolicy:
290+
properties:
291+
action_id:
292+
type: string
293+
created_at:
294+
type: string
295+
policy_id:
296+
type: string
297+
principal_id:
298+
type: string
299+
resource_kind:
300+
type: string
301+
resource_value:
302+
type: string
303+
updated_at:
304+
type: string
305+
version:
306+
type: integer
307+
type: object
289308
model.ErrorResponse:
290309
properties:
291310
error:
@@ -877,6 +896,30 @@ paths:
877896
summary: Updates a policy
878897
tags:
879898
- Policy
899+
/v1/policies/{identifier}/matches:
900+
get:
901+
produces:
902+
- application/json
903+
responses:
904+
"200":
905+
description: OK
906+
schema:
907+
items:
908+
$ref: '#/definitions/model.CompiledPolicy'
909+
type: array
910+
"404":
911+
description: Not Found
912+
schema:
913+
$ref: '#/definitions/model.ErrorResponse'
914+
"500":
915+
description: Internal Server Error
916+
schema:
917+
$ref: '#/definitions/model.ErrorResponse'
918+
security:
919+
- Authentication: []
920+
summary: Retrieve compiled policies
921+
tags:
922+
- Policy
880923
/v1/principals:
881924
get:
882925
parameters:
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package handler
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/eko/authz/backend/internal/entity/manager"
7+
"github.com/eko/authz/backend/internal/entity/repository"
8+
"github.com/eko/authz/backend/internal/http/handler/model"
9+
"github.com/gofiber/fiber/v2"
10+
)
11+
12+
// Retrieve compiled policies
13+
//
14+
// @security Authentication
15+
// @Summary Retrieve compiled policies
16+
// @Tags Policy
17+
// @Produce json
18+
// @Success 200 {object} []model.CompiledPolicy
19+
// @Failure 404 {object} model.ErrorResponse
20+
// @Failure 500 {object} model.ErrorResponse
21+
// @Router /v1/policies/{identifier}/matches [Get]
22+
func CompiledList(
23+
compiledManager manager.CompiledPolicy,
24+
) fiber.Handler {
25+
return func(c *fiber.Ctx) error {
26+
page, size, err := paginate(c)
27+
if err != nil {
28+
return returnError(c, http.StatusInternalServerError, err)
29+
}
30+
31+
// List policies
32+
compiledPolicies, total, err := compiledManager.GetRepository().Find(
33+
repository.WithPage(page),
34+
repository.WithSize(size),
35+
repository.WithFilter(httpFilterToORM(c)),
36+
repository.WithSort(httpSortToORM(c)),
37+
)
38+
if err != nil {
39+
return returnError(c, http.StatusInternalServerError, err)
40+
}
41+
42+
return c.JSON(model.NewPaginated(compiledPolicies, total, page, size))
43+
}
44+
}

0 commit comments

Comments
 (0)