-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
105 lines (88 loc) · 2.13 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
package main
import (
"context"
"encoding/json"
"fmt"
"html/template"
"log"
"net"
"net/http"
"os"
"path/filepath"
"github.com/julienschmidt/httprouter"
"github.com/samber/lo"
)
const pageTemplatePath = "./resources/home.tpl.html"
// myfiles is a very fancy data store as you can tell
var myfiles = [...]file{
{
Owner: "spiffe://example.com/dataexporter",
Name: "customers_export.csv",
Location: "Belgium",
Classification: "CUSTOMERS_PII",
},
{
Owner: "spiffe://example.com/test",
Name: "testdata.csv",
Location: "Belgium",
Environment: "staging",
},
{
Owner: "[email protected]",
Name: "payslip_e9999_123.pdf",
Location: "Belgium",
Classification: "HR_PII",
EmployeeID: "e9999",
},
{
Owner: "[email protected]",
Name: "payslip_e8888_123.pdf",
Location: "USA",
Classification: "HR_PII",
EmployeeID: "e8888",
},
}
func main() {
ctx := context.Background()
services := lo.Must(createServices(ctx))
ro := httprouter.New()
ro.GET("/", handleErrors(services.homeHandle))
const addr = "127.0.0.1:8080"
srv := http.Server{
Addr: addr,
Handler: ro,
BaseContext: func(l net.Listener) context.Context {
return ctx
},
}
log.Println("serving on", "http://"+addr)
lo.Must0(srv.ListenAndServe())
}
func (s services) homeHandle(w http.ResponseWriter, r *http.Request, p httprouter.Params) error {
t, err := template.ParseFiles(pageTemplatePath)
if err != nil {
return err
}
user := s.user.get()
input := page{
User: struct2Map(user),
Files: make([]displayFile, len(myfiles)),
}
json.NewEncoder(os.Stdout).Encode(&input)
for i, fileToCheck := range myfiles {
authz, err := s.policy.eval(r.Context(), policyRequest{
Resource: fileToCheck,
Subject: user,
Action: "read",
})
if err != nil {
return fmt.Errorf("policy evaluation failed with unexpected error %w", err)
}
input.Files[i] = displayFile{
File: struct2Map(fileToCheck),
Authz: authz,
}
}
w.Header().Set("Content-Type", "text/html")
return t.ExecuteTemplate(w, filepath.Base(pageTemplatePath), &input)
}