Skip to content

Commit

Permalink
Add APIs to evaluate policies get result from OPA
Browse files Browse the repository at this point in the history
  • Loading branch information
baixinsui committed Sep 28, 2023
1 parent e513ae1 commit 024fd1c
Show file tree
Hide file tree
Showing 8 changed files with 510 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# install swag and update docs of openapi with swag
INSTALL_SWAG := $(shell go get -u github.com/swaggo/swag/cmd/swag;go install github.com/swaggo/swag/cmd/swag)
UPDATE_DOCS := $(shell swag fmt;swag init -o ./openapi/docs)
UPDATE_DOCS := $(shell swag fmt;swag init --parseDependency --parseInternal -o ./openapi/docs)

DIST := dist
EXECUTABLE := policy-man
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ or API annotations are updated, these files should be updated by the following c
go get -u github.com/swaggo/swag/cmd/swag
go install github.com/swaggo/swag/cmd/swag
swag fmt
swag init -o ./openapi/docs
swag init --parseDependency --parseInternal -o ./openapi/docs
```
All the above commands are written to the file [Makefile](./Makefile), You can also use commands in the chapter
Expand Down
159 changes: 155 additions & 4 deletions openapi/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ const docTemplate = `{
"application/json"
],
"tags": [
"Policies Evaluate"
"Policies Evaluation"
],
"summary": "Evaluate the policies",
"summary": "Evaluate the input by policies",
"parameters": [
{
"description": "evalCmdList",
Expand Down Expand Up @@ -67,6 +67,61 @@ const docTemplate = `{
}
}
},
"/evaluate/policies/raw": {
"post": {
"description": "Evaluate the input by all the policies and get raw result",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Policies Evaluation"
],
"summary": "Evaluate the input by policies",
"parameters": [
{
"description": "evalCmdList",
"name": "cmdList",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/server.evalCmdList"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/rego.Result"
}
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/server.ErrorResult"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/server.ErrorResult"
}
},
"502": {
"description": "Bad Gateway",
"schema": {
"$ref": "#/definitions/server.ErrorResult"
}
}
}
}
},
"/evaluate/policy": {
"post": {
"description": "Evaluate whether the input meets the policy",
Expand All @@ -77,9 +132,9 @@ const docTemplate = `{
"application/json"
],
"tags": [
"Policies Evaluate"
"Policies Evaluation"
],
"summary": "Evaluate the policy",
"summary": "Evaluate the input by policies",
"parameters": [
{
"description": "evalCmd",
Expand Down Expand Up @@ -119,6 +174,61 @@ const docTemplate = `{
}
}
},
"/evaluate/policy/raw": {
"post": {
"description": "Evaluate the input by the policy and get raw result",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Policies Evaluation"
],
"summary": "Evaluate the input by policies",
"parameters": [
{
"description": "evalCmd",
"name": "cmd",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/server.evalCmd"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/rego.Result"
}
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/server.ErrorResult"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/server.ErrorResult"
}
},
"502": {
"description": "Bad Gateway",
"schema": {
"$ref": "#/definitions/server.ErrorResult"
}
}
}
}
},
"/health": {
"get": {
"description": "Check health status of service",
Expand Down Expand Up @@ -214,6 +324,47 @@ const docTemplate = `{
}
},
"definitions": {
"rego.ExpressionValue": {
"type": "object",
"properties": {
"location": {
"$ref": "#/definitions/rego.Location"
},
"text": {
"type": "string"
},
"value": {}
}
},
"rego.Location": {
"type": "object",
"properties": {
"col": {
"type": "integer"
},
"row": {
"type": "integer"
}
}
},
"rego.Result": {
"type": "object",
"properties": {
"bindings": {
"$ref": "#/definitions/rego.Vars"
},
"expressions": {
"type": "array",
"items": {
"$ref": "#/definitions/rego.ExpressionValue"
}
}
}
},
"rego.Vars": {
"type": "object",
"additionalProperties": true
},
"server.ErrorResult": {
"type": "object",
"required": [
Expand Down
Loading

0 comments on commit 024fd1c

Please sign in to comment.