-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (59 loc) · 1.39 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
package main
import (
"acl-test-go/config"
"acl-test-go/database"
"acl-test-go/model"
"acl-test-go/utils"
"context"
"fmt"
"log"
)
func main() {
db, err := database.NewConnection()
if err != nil {
log.Fatal(err)
}
models := []interface{}{
model.Token{},
model.Policie{},
model.Rule{},
}
if err := db.CreateTables(models); err != nil {
log.Fatal(err)
}
strp1 := fmt.Sprintf("firstbanks%s", utils.RandStringRunes(5))
policy1 := db.CreatePolicy(strp1, 1, []model.Rule{{
Capabilities: []string{"read"},
Path: "*",
Resource: "goldbank",
}, {
Capabilities: []string{"write"},
Path: "*",
Resource: "silverbank",
}})
strp2 := fmt.Sprintf("lastbank%s", utils.RandStringRunes(5))
policy2 := db.CreatePolicy(strp2, 1, []model.Rule{{
Resource: "copperbank",
Path: "*",
Capabilities: []string{"read", "write", "list"},
}})
secret, err := db.CreateSecret(false, 1, []string{policy1.Name, policy2.Name})
if err != nil {
log.Fatal(err)
}
fmt.Println(secret)
ctx := context.Background()
acl, err := config.New(ctx, db, secret)
if err != nil {
log.Fatal(err)
}
if err := acl.CheckAuthorized(ctx, "goldbank", "*", "list"); err != nil {
fmt.Println("Not Authorized")
return
}
if err := acl.CheckAuthorized(ctx, "silverbank", "*", "write"); err != nil {
fmt.Println("Not Authorized")
return
}
fmt.Println("Keep the flow")
}