/
manager_http.go
108 lines (95 loc) · 2.95 KB
/
manager_http.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
package policy
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"github.com/ory/hydra/pkg"
"github.com/ory/ladon"
"github.com/pkg/errors"
)
type jsonPolicy struct {
ID string `json:"id" gorethink:"id"`
Description string `json:"description" gorethink:"description"`
Subjects []string `json:"subjects" gorethink:"subjects"`
Effect string `json:"effect" gorethink:"effect"`
Resources []string `json:"resources" gorethink:"resources"`
Actions []string `json:"actions" gorethink:"actions"`
Conditions json.RawMessage `json:"conditions" gorethink:"conditions"`
}
type HTTPManager struct {
Endpoint *url.URL
Dry bool
FakeTLSTermination bool
Client *http.Client
}
// Create persists the policy.
func (m *HTTPManager) Create(policy ladon.Policy) error {
var r = pkg.NewSuperAgent(m.Endpoint.String())
r.Client = m.Client
r.Dry = m.Dry
r.FakeTLSTermination = m.FakeTLSTermination
return r.Create(policy)
}
// Update the policy.
func (m *HTTPManager) Update(policy ladon.Policy) error {
var r = pkg.NewSuperAgent(pkg.JoinURL(m.Endpoint, policy.GetID()).String())
r.Client = m.Client
r.Dry = m.Dry
r.FakeTLSTermination = m.FakeTLSTermination
return r.Update(policy)
}
// Get retrieves a policy.
func (m *HTTPManager) List(limit, offset int64) (ladon.Policies, error) {
var policies []*ladon.DefaultPolicy
var r = pkg.NewSuperAgent(m.Endpoint.String() + fmt.Sprintf("?limit=%d&offset=%d", limit, offset))
r.Client = m.Client
r.FakeTLSTermination = m.FakeTLSTermination
r.Dry = m.Dry
if err := r.Get(&policies); err != nil {
return nil, errors.WithStack(err)
}
ret := make(ladon.Policies, len(policies))
for k, p := range policies {
ret[k] = ladon.Policy(p)
}
return ret, nil
}
// Get retrieves a policy.
func (m *HTTPManager) Get(id string) (ladon.Policy, error) {
var policy = ladon.DefaultPolicy{
Conditions: ladon.Conditions{},
}
var r = pkg.NewSuperAgent(pkg.JoinURL(m.Endpoint, id).String())
r.Client = m.Client
r.FakeTLSTermination = m.FakeTLSTermination
r.Dry = m.Dry
if err := r.Get(&policy); err != nil {
return nil, errors.WithStack(err)
}
return &policy, nil
}
// Delete removes a policy.
func (m *HTTPManager) Delete(id string) error {
var r = pkg.NewSuperAgent(pkg.JoinURL(m.Endpoint, id).String())
r.Client = m.Client
r.Dry = m.Dry
r.FakeTLSTermination = m.FakeTLSTermination
return r.Delete()
}
// Finds all policies associated with the subject.
func (m *HTTPManager) FindPoliciesForSubject(subject string) (ladon.Policies, error) {
var policies []*ladon.DefaultPolicy
var r = pkg.NewSuperAgent(m.Endpoint.String() + "?subject=" + subject)
r.Client = m.Client
r.FakeTLSTermination = m.FakeTLSTermination
r.Dry = m.Dry
if err := r.Get(&policies); err != nil {
return nil, errors.WithStack(err)
}
ret := make(ladon.Policies, len(policies))
for k, p := range policies {
ret[k] = ladon.Policy(p)
}
return ret, nil
}