This repository has been archived by the owner on Jul 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
composer.go
80 lines (64 loc) · 2.02 KB
/
composer.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
package policy
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"github.com/spf13/viper"
"github.com/factly/dega-server/service/core/model"
"github.com/factly/x/loggerx"
)
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
// Composer create keto policy
func Composer(oID int, sID int, inputPolicy policyReq) model.KetoPolicy {
allowedResources := []string{"categories", "formats", "media", "policies", "posts", "pages", "tags", "webhooks", "claims", "claimants", "fact-checks", "ratings", "google", "menus", "episodes", "podcasts"}
allowedActions := []string{"get", "create", "update", "delete", "publish"}
result := model.KetoPolicy{}
commanPolicyString := fmt.Sprint(":org:", oID, ":app:dega:space:", sID, ":")
result.ID = "id" + commanPolicyString + inputPolicy.Name
result.Description = inputPolicy.Description
result.Effect = "allow"
result.Resources = make([]string, 0)
result.Actions = make([]string, 0)
for _, each := range inputPolicy.Permissions {
if contains(allowedResources, each.Resource) {
result.Resources = append(result.Resources, "resources"+commanPolicyString+each.Resource)
var eachActions []string
for _, action := range each.Actions {
if contains(allowedActions, action) {
eachActions = append(eachActions, "actions"+commanPolicyString+each.Resource+":"+action)
}
}
result.Actions = append(result.Actions, eachActions...)
}
}
result.Subjects = inputPolicy.Users
buf := new(bytes.Buffer)
err := json.NewEncoder(buf).Encode(&result)
if err != nil {
loggerx.Error(err)
}
req, err := http.NewRequest("PUT", viper.GetString("keto_url")+"/engines/acp/ory/regex/policies", buf)
if err != nil {
loggerx.Error(err)
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return model.KetoPolicy{}
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
loggerx.Error(err)
}
return result
}