1
1
# Python Open Policy Agent (OPA) Client
2
2
3
+ [ ![ MIT licensed] ( https://img.shields.io/github/license/Turall/OPA-python-client )] ( https://raw.githubusercontent.com/Turall/OPA-python-client/master/LICENSE )
4
+ [ ![ GitHub stars] ( https://img.shields.io/github/stars/Turall/OPA-python-client.svg )] ( https://github.com/Turall/OPA-python-client/stargazers )
5
+ [ ![ GitHub forks] ( https://img.shields.io/github/forks/Turall/OPA-python-client.svg )] ( https://github.com/Turall/OPA-python-client/network )
6
+ [ ![ GitHub issues] ( https://img.shields.io/github/issues-raw/Turall/OPA-python-client )] ( https://github.com/Turall/OPA-python-client/issues )
3
7
[ ![ Downloads] ( https://pepy.tech/badge/opa-python-client )] ( https://pepy.tech/project/opa-python-client )
4
8
9
+
5
10
See offical documentation page [ Open Policy Agent] ( https://www.openpolicyagent.org/docs/latest/ )
6
11
7
12
8
13
### Installation ###
9
14
10
15
``` sh
11
- $ pip install OPA-python-client
16
+ $ pip install OPA-python-client
12
17
```
13
18
19
+ Alternatively, if you prefer to use ` poetry ` for package dependencies:
20
+
21
+ ``` bash
22
+ $ poetry shell
23
+ $ poetry add OPA-python-client
24
+ ```
14
25
15
26
16
27
17
- ## Usage Examples ##
28
+ ## Usage Examples
18
29
19
30
``` python
20
31
>> > from opa_client.opa import OpaClient
47
58
```
48
59
49
60
50
- ### Connection to OPA service ###
61
+ ### Connection to OPA service
51
62
52
63
``` python
53
64
from opa_client.opa import OpaClient
@@ -61,10 +72,9 @@ del client
61
72
```
62
73
63
74
64
- ### Connection to OPA service with SSL ###
75
+ ### Connection to OPA service with SSL
65
76
66
77
``` python
67
-
68
78
from opa_client.opa import OpaClient
69
79
70
80
@@ -82,9 +92,7 @@ del client
82
92
```
83
93
84
94
85
-
86
-
87
- ### Update policy from rego file ###
95
+ ### Update policy from rego file
88
96
89
97
``` python
90
98
from opa_client.opa import OpaClient
@@ -99,10 +107,9 @@ del client
99
107
```
100
108
101
109
102
- ### Update policy from URL ###
110
+ ### Update policy from URL
103
111
104
112
``` python
105
-
106
113
from opa_client.opa import OpaClient
107
114
108
115
client = OpaClient()
@@ -116,12 +123,10 @@ del client
116
123
```
117
124
118
125
119
- ### Delete policy ###
126
+ ### Delete policy
120
127
121
128
122
129
``` python
123
-
124
-
125
130
from opa_client.opa import OpaClient
126
131
127
132
client = OpaClient()
@@ -133,12 +138,10 @@ client.get_policies_list() # response is []
133
138
del client
134
139
```
135
140
136
- ### Get raw data from OPA service ###
141
+ ### Get raw data from OPA service
137
142
138
143
139
144
``` python
140
-
141
-
142
145
from opa_client.opa import OpaClient
143
146
144
147
client = OpaClient()
@@ -159,12 +162,11 @@ print(client.get_opa_raw_data("userinfo",query_params={"metrics": True}))
159
162
del client
160
163
```
161
164
162
- ### Save policy to file from OPA service ###
163
-
164
165
165
- ``` python
166
+ ### Save policy to file from OPA service
166
167
167
168
169
+ ``` python
168
170
from opa_client.opa import OpaClient
169
171
170
172
client = OpaClient()
@@ -174,12 +176,11 @@ client.opa_policy_to_file(policy_name="fromurl",path="/your/path",filename="exam
174
176
del client
175
177
```
176
178
177
- ### Delete data from OPA service ###
178
-
179
179
180
- ``` python
180
+ ### Delete data from OPA service
181
181
182
182
183
+ ``` python
183
184
from opa_client.opa import OpaClient
184
185
185
186
client = OpaClient()
@@ -190,12 +191,10 @@ del client
190
191
```
191
192
192
193
193
- ### Information about policy path and rules ###
194
+ ### Information about policy path and rules
194
195
195
196
196
197
``` python
197
-
198
-
199
198
from opa_client.opa import OpaClient
200
199
201
200
client = OpaClient()
@@ -208,12 +207,10 @@ del client
208
207
```
209
208
210
209
211
- ### Check permissions ###
210
+ ### Check permissions
212
211
213
212
214
213
``` python
215
-
216
-
217
214
from opa_client.opa import OpaClient
218
215
219
216
client = OpaClient()
@@ -250,7 +247,6 @@ hello {
250
247
251
248
check_data = {" message" : " world" }
252
249
client.check_policy_rule(input_data = check_data, package_path = " play" , rule_name = " hello" ) # response {'result': True}
253
-
254
250
```
255
251
256
252
### Execute an Ad-hoc Query
@@ -286,7 +282,6 @@ print(client.ad_hoc_query(query_params={"q": "data.userinfo.user_roles[name]"}))
286
282
# you can send body request
287
283
print (client.ad_hoc_query(body = {" query" : " data.userinfo.user_roles[name] " }))
288
284
# response is {'result': [{'name': 'eve'}, {'name': 'alice'}, {'name': 'bob'}]}
289
-
290
285
```
291
286
292
287
### Check OPA healthy. If you want check bundels or plugins, add query params for this.
@@ -301,12 +296,14 @@ print(client.check_health({"bundle": True})) # response is True or False
301
296
# If your diagnostic url different than default url, you can provide it.
302
297
print (client.check_health(diagnostic_url = " http://localhost:8282/health" )) # response is True or False
303
298
print (client.check_health(query = {" bundle" : True }, diagnostic_url = " http://localhost:8282/health" )) # response is True or False
304
-
305
299
```
306
300
307
301
308
- # Contributing #
302
+ # Contributing
303
+
304
+ Fell free to open issue and send pull request.
309
305
310
- #### Free to open issue and send PR ####
306
+ Thanks To [ Contributors] ( https://github.com/Turall/OPA-python-client/graphs/contributors ) .
307
+ Contributions of any kind are welcome!
311
308
312
- ### OPA-python-client supports Python >= 3.5
309
+ Before you start please read [ CONTRIBUTING ] ( https://github.com/Turall/ OPA-python-client/blob/master/CONTRIBUTING.md )
0 commit comments