-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
190 lines (150 loc) · 4.44 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright 2022 Styra Inc. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"github.com/alecthomas/kong"
"github.com/gorilla/mux"
"github.com/styrainc/entitlements-samples/go-sample"
"github.com/styrainc/entitlements-samples/go-sample/playground"
"github.com/open-policy-agent/opa/logging"
"github.com/open-policy-agent/opa/sdk"
)
var CLI struct {
Storage string `name:"path" short:"p" type:"path" default:"./" help:"Directory where persistent data should be stored."`
Port int `name:"port" short:"P" type:"int" default:8123 help:"Port where API should be served."`
Config string `name:"config" short:"c" type:"path" help:"Path to OPA configuration file (sdk mode only)"`
Rule string `name:"rule" short:"r" default:"/main/main" type:"string" help:"OPA rule path (sdk mode only)"`
Allow string `name:"allow" short:"a" default:"outcome/allow" type:"string" help:"path within the OPA rule to extract the allow/deny decision"`
OPA string `name:"opa" short:"o" type:"string" help:"URL for the OPA server (http mode only)"`
Mode string `name:"mode" short:"m" type:"string" default:"sdk" help:"Mode in which to use OPA, choices are 'sdk', 'http', 'allow-all', 'deny-all'"`
Playground bool `name:"playground" short:"g" help:"Enable the /playground web UI. Only works in sdk mode."`
}
var dummyAllow string = `
{
"ID": "ffffffff-ffff-ffff-ffff-ffffffffffff",
"result": {
"allowed": true,
"entz": [],
"outcome": {
"allow": true,
"decision_type": "ALLOWED",
"enforced": [
{
"allowed": true,
"entz": [],
"message": "Request was matched"
}
],
"monitored": [],
"policy_type": "rules",
"stacks": {},
"system_type": "template.entitlements:0.1"
}
}
}
`
var dummyDeny string = `
{
"ID": "ffffffff-ffff-ffff-ffff-ffffffffffff",
"result": {
"allowed": false,
"entz": [],
"outcome": {
"allow": true,
"decision_type": "DENIED",
"enforced": [
{
"denied": true,
"entz": [],
"message": "Request was matched"
}
],
"monitored": [],
"policy_type": "rules",
"stacks": {},
"system_type": "template.entitlements:0.1"
}
}
}
`
func main() {
kong.Parse(&CLI)
var decider sample.OPADecider
if CLI.Mode == "sdk" {
if CLI.Config == "" {
panic("config must be provided in sdk mode")
}
if CLI.Rule == "" {
panic("rule must be provided in sdk mode")
}
ctx := context.Background()
f, err := os.Open(CLI.Config)
if err != nil {
panic(err)
}
defer f.Close()
// create a new OPA client with the config
opa, err := sdk.New(ctx, sdk.Options{
Config: f,
// This is not suggested for production use, but is
// nice for the sample as it allows seeing when OPA
// has updated the policy bundle.
Logger: logging.New(),
})
if err != nil {
panic(err)
}
defer opa.Stop(ctx)
decider = sample.NewSDKDecider(opa, ctx, CLI.Rule)
if CLI.Playground {
playground.SetOPARule("/main/main", "outcome/allow")
playground.SetOPA(opa, ctx)
}
} else if CLI.Mode == "http" {
decider = sample.NewHTTPDecider(CLI.OPA)
} else if CLI.Mode == "allow-all" {
decision := &sample.OPADecision{}
err := json.Unmarshal([]byte(dummyAllow), decision)
if err != nil {
panic(err)
}
decider = sample.NewDummyDecider(decision)
} else if CLI.Mode == "deny-all" {
decision := &sample.OPADecision{}
err := json.Unmarshal([]byte(dummyDeny), decision)
if err != nil {
panic(err)
}
decider = sample.NewDummyDecider(decision)
} else {
panic(fmt.Sprintf("mode '%s' is not one of sdk, http, allow-all, deny-all", CLI.Mode))
}
if CLI.Playground {
}
err := sample.SetStorageDir(CLI.Storage)
if err != nil {
panic(err)
}
sample.LoadFromDisk()
r := mux.NewRouter().StrictSlash(false)
carsRouter := r.PathPrefix("/cars")
carsRouter.Handler(sample.NewEntitlementsHandler(decider, sample.GetAPIHandler()))
if CLI.Playground {
fmt.Printf("Enabling playground...\n")
if CLI.Mode != "sdk" {
panic("entz-playground can only be enabled in sdk mode")
}
playgroundRouter := r.PathPrefix("/")
playgroundRouter.Handler(playground.GetAPIHandler())
}
err = http.ListenAndServe(fmt.Sprintf(":%d", CLI.Port), r)
if err != nil {
panic(err)
}
}