Skip to content

Commit af66fc1

Browse files
committed
opa bundle api post
Signed-off-by: Batuhan Apaydın <[email protected]>
1 parent b876d74 commit af66fc1

File tree

6 files changed

+243
-0
lines changed

6 files changed

+243
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
---
2+
title: "Share and distribute Open Policy Agent Bundles with functions"
3+
description: "In this post you'll learn how to share and distribute Open Policy Agent Bundles with functions"
4+
date: 2020-11-24
5+
image: /images/2020-11-08-distribute-policies-with-the-power-of-openfaas/opa-bundle-api.png
6+
categories:
7+
- arkade
8+
- kubectl
9+
- faas-cli
10+
- opa
11+
- bundle api
12+
author_staff_member: developer-guy
13+
dark_background: true
14+
---
15+
16+
In this post you'll learn how to share and distribute Open Policy Agent Bundles with functions
17+
18+
<p align="center">
19+
<img height="128" src="/images/2020-11-08-distribute-policies-with-the-power-of-openfaas/opa-power-openfaas.png">
20+
</p>
21+
22+
# Share and distribute Open Policy Agent Bundles with functions
23+
24+
Let's clarify what is the motivation behind this post first.
25+
26+
One of the feature OpenFaaS is [auto-scaling](https://docs.openfaas.com/architecture/autoscaling/) mechanism. The auto-scaling means is that you can scale up/down your function instances as demand increases. Also OpenFaaS provides
27+
a feature called [zero-scale](https://docs.openfaas.com/architecture/autoscaling/#zero-scale). By enabling this feature , you can scaling to zero to recover idle resources is available in OpenFaaS.
28+
29+
Using OpenFaaS as an OPA's Bundle API , you can have all the features by default with less effort.Also, you can't have to manage build/push and deploy phases with your Bundle API's .
30+
31+
### What you will learn in this post ?
32+
33+
In this post we are gonna learn:
34+
35+
* [What is OPA (Open Policy Agent) ?](#whatisopa)
36+
* [How can we deploy OPA co-located with our service ?](#colocate-opa-service)
37+
* [How can OpenFaaS help us about the OPA ?](#openfaasopa)
38+
* [Demo](#demo)
39+
40+
### <a id="whatisopa"></a> What is OPA (Open Policy Agent) ?
41+
42+
OPA describes itself as a general purpose policy engine, for more detail you can look at the official [documentation](https://www.openpolicyagent.org/docs/latest/).
43+
44+
OPA's main goal is decoupling the policy decision-making from the policy enforcement. When your software needs to make policy decisions it queries OPA and supplies structured data (e.g., JSON) as input. OPA accepts arbitrary structured data as input.
45+
46+
![opa-decision-making](/images/2020-11-08-distribute-policies-with-the-power-of-openfaas/opa-policy-decision-make.png)
47+
> Credit: https://www.magalix.com/blog/introducing-policy-as-code-the-open-policy-agent-opa
48+
49+
### <a id="colocate-opa-service"></a> How can we deploy OPA co-located with our service ?
50+
51+
When it comes to deploying OPA, you have more than one option depending on your specific scenario:
52+
53+
* As a Go library
54+
* As a daemon
55+
56+
The recommended way is to run OPA is as a daemon. The reason is that this design increases performance and availability.By default, all of the policy and data that OPA uses to make decisions is kept in-memory for the low-latency and we should colocate OPA and the service to avoid the network latency also.
57+
58+
![opa-deploy-design](/images/2020-11-08-distribute-policies-with-the-power-of-openfaas/opa-deploy-design.png)
59+
> Credit: https://www.magalix.com/blog/introducing-policy-as-code-the-open-policy-agent-opa
60+
61+
## <a id="openfaasopa"></a> How can OpenFaaS help us about the OPA ?
62+
63+
OPA exposes a set of APIs that enable unified, logically centralized policy management which is called ["Management API's"](https://www.openpolicyagent.org/docs/latest/management/). Think of them as a `Control Plane` for the OPA instances working as a `Data Plane`. With the Management API's you can control the OPA instances like enable decision logging, configure the Bundle API etc.
64+
65+
Let's focus on Bundle API which is one of the Management API's for OPA.
66+
67+
Bundle API's purpose is to help OPA to load policies across the stack to the OPA instances.OPA can periodically download bundles of policy and data from remote HTTP servers. The policies and data are loaded on the fly without requiring a restart of OPA.
68+
69+
In this demo, we create a serverless function that mimics an OPA's Bundle API.Simply, this serverless function designed as a plain file server. When OPA's asks for the policies it basically returns bundles that ready on the filesystem as a response.
70+
71+
### <a id="demo"></a> Demo
72+
73+
You can find all the details about this demo in Github [repo](https://github.com/developer-guy/distribute-policies-with-the-power-of-openfaas).
74+
75+
Prerequisites
76+
* A Kubernetes cluster (kind, minikube, etc.)
77+
* OpenFaaS CLI
78+
* Arkade
79+
* Kubectl
80+
* KinD
81+
* Docker
82+
83+
# Setup
84+
85+
## 1. Setup Tools
86+
87+
* Arkade
88+
```sh
89+
$ curl -sLS https://dl.get-arkade.dev | sudo sh
90+
```
91+
92+
* KinD
93+
```sh
94+
$ arkade get kind
95+
```
96+
97+
* Kubectl
98+
```sh
99+
$ arkade get kubectl
100+
```
101+
102+
* OpenFaaS CLI
103+
```sh
104+
$ arkade get faas-cli
105+
```
106+
107+
### 2. Set Up Cluster
108+
109+
You can start a Kubernetes cluster with KinD if you don't have one already
110+
111+
```bash
112+
$ arkade get kind
113+
$ kind create cluster
114+
```
115+
116+
### 3. Deploy OpenFaaS
117+
118+
* Install OpenFaaS using Arkade
119+
120+
```sh
121+
$ arkade install openfaas
122+
```
123+
124+
* Verify Deployment
125+
126+
```sh
127+
$ kubectl rollout status -n openfaas deploy/gateway
128+
```
129+
130+
* Enable local access to Gateway
131+
```sh
132+
$ kubectl port-forward -n openfaas svc/gateway 8080:8080 &
133+
```
134+
135+
### 4. Configure faas-cli
136+
137+
* Access password that available in the basic-auth secret in openfaas namespace
138+
139+
```sh
140+
$ PASSWORD=$(kubectl get secret -n openfaas basic-auth -o jsonpath="{.data.basic-auth-password}" | base64 --decode; echo)
141+
```
142+
143+
* Login with using the password to Gateway
144+
145+
```sh
146+
$ echo -n $PASSWORD | faas-cli login --username admin --password-stdin
147+
```
148+
149+
### 5. Deploy Function
150+
151+
* Go the functions directory , pull the right template and deploy the function
152+
153+
```sh
154+
$ cd functions
155+
$ faas-cli template store pull golang-middleware
156+
$ faas-cli up -f bundle-api.yml
157+
```
158+
159+
### 6. Load Images
160+
161+
* Load images from Docker Hub to KinD
162+
163+
```sh
164+
$ docker image pull openpolicyagent/opa:latest
165+
$ kind load docker-image openpolicyagent/opa:latest
166+
$ docker image pull openpolicyagent/demo-restful-api:0.2
167+
$ kind load docker-image openpolicyagent/demo-restful-api:0.2
168+
```
169+
170+
### 7. Deploy application
171+
172+
* Deploy application with located OPA, detail: [deployment.yaml](https://github.com/developer-guy/distribute-policies-with-the-power-of-openfaas/blob/master/hack/manifests/deployment.yaml)
173+
174+
```sh
175+
$ cd ../hack/manifests <br>
176+
$ kubectl apply -f deployment.yaml
177+
```
178+
179+
* Verify Deployment
180+
181+
```sh
182+
$ kubectl rollout status deployment demo-restful-api
183+
```
184+
185+
* Enable local access to application
186+
187+
```sh
188+
$ kubectl port-forward svc/demo-restful-api 5000:80 &
189+
```
190+
191+
# Test
192+
193+
[Rego](https://www.openpolicyagent.org/docs/latest/#rego) is the DSL for the OPA. We can author our policies using the rego.
194+
195+
For this tutorial, our desired policy is:
196+
197+
* People can see their own salaries (GET /finance/salary/{user} is permitted for {user})
198+
* A manager can see their direct reports’ salaries (GET /finance/salary/{user} is permitted for {user}’s manager)
199+
200+
![authz.rego](/images/2020-11-08-distribute-policies-with-the-power-of-openfaas/authz-policy.png)
201+
202+
### Check that alice can see her own salary
203+
204+
* This command will succeed, because alice wants to see your own salary.
205+
206+
```sh
207+
$ curl --user alice:password localhost:5000/finance/salary/alice
208+
```
209+
210+
### Check that bob CANNOT see charlie’s salary.
211+
212+
* bob is not charlie’s manager, so the following command will fail.
213+
214+
```sh
215+
$ curl --user bob:password localhost:5000/finance/salary/charlie
216+
```
217+
218+
* bob is the alice's manager, so the following command will succeed.
219+
220+
```sh
221+
$ curl --user bob:password localhost:5000/finance/salary/alice
222+
```
223+
224+
### Join the community
225+
226+
Have you got questions, comments, or suggestions? Join the community on [Slack](https://slack.openfaas.io).
227+
228+
Would you like help to set up your OpenFaaS installation, or someone to call when things don't quite go to plan? [Our Premium Subscription plan](https://www.openfaas.com/support/) gives you a say in the project roadmap, a support contact, and access to Enterprise-grade authentication with OIDC.
229+
230+
### Acknowledgements
231+
232+
* Special Thanks to [Alex Ellis](https://twitter.com/alexellisuk) for all guidance and for merging changes into OpenFaaS to better support this workflow.
233+
* Special Thanks to [Furkan Türkal](https://twitter.com/furkanturkaI) for all the support.
234+
235+
### References
236+
237+
* https://www.magalix.com/blog/introducing-policy-as-code-the-open-policy-agent-opa
238+
239+
* https://docs.openfaas.com
240+
241+
* https://www.openpolicyagent.org/docs/latest/http-api-authorization/
242+
243+
* https://github.com/developer-guy/distribute-policies-with-the-power-of-openfaas
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)