Flask extension to use OPA as a client
Project description
Flask-OPA
Simple to use Flask extension that lets you use Open Policy Agent in your project.
How to run the application
If you want to try a demo check the code in examples
, but for development:
-
Run OPA in server mode
- Check the latest OPA release and download it.
- Put the binary file in the path of your system
- Allow its execution with something like
- Run opa in server mode with the sample policies
cd examples opa run -s -w data.json app.rego
-s
is to run it in server mode instead of opening the REPL-w
is for watching the changes of the data/policy files
-
Specify configuration variables
-
OPA_URL
url string that specifies the OPA url to evaluate your input. It includes the path of the policy. E.ghttp://localhost:8181/v1/data/examples/allow
. -
OPA_SECURED
boolean to specify if OPA will be enabled to your application.
See more at the rest api reference
-
-
Bind the OPA class to your Flask application
Its easy to bind the Flask-OPA library to your application. Just follow the following steps:
-
Create the OPA instance
app = Flask(__name__) app.config.from_pyfile('app.cfg') opa = OPA(app, parse_input)
Lets see the parameters that we passed to the OPA class:
parse_input
(Required) contains a method that returns the input data json to be evaluated by the policy, e.g.:
{ "input": { "method": "GET", "path": ["data", "jon"], "user": "paul" } }
url
(Optional) to use an specific url instead of theOPA_URL
optionally specified in the app configuration.allow_function
(Optional) predicate that determinate if the response from OPA allows (True) or denies (False) the request
If you want enforce the OPA security in your application you can create the OPA instance like this:
opa = OPA.secured(app, parse_input, url="http://localhost:8181/v1/data/package_name/allow")
or
opa = OPA(app, parse_input, url="http://localhost:8181/v1/data/package_name/allow").secured()
otherwise OPA will enforce your security only if
OPA_SECURED
isTrue
.Specify the logging level to
DEBUG
if you want to get access to Flask-OPA logs of its operations usingapp.logger.setLevel(logging.DEBUG)
-
Run your Flask application.
Status
Pre-release or Beta: The project has gone through multiple rounds of active development with a goal of reaching a stable release version, but is not there yet.
Path of Development: Active (October 16st 2018)
Author
Eliecer Hernandez - eliecerhdz@gmail.com. To know more of me please visit my website.
License
This project is licensed under the MIT License - see the LICENSE.md file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for Flask_OPA-0.2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7666fa29af6543bc96f7c67671d0006d47aa4c21978c7ffd9b408884c45e3660 |
|
MD5 | 02da822ca99eb2216dc76624f7c6661c |
|
BLAKE2b-256 | 3bffd286831d59a440d90c43ee829e98ac8274fb17d6f6fc35bfdabfe2815d4f |