forked from sodafoundation/strato
/
policy.go
215 lines (190 loc) · 5.17 KB
/
policy.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright (c) 2018 Huawei Technologies Co., Ltd. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package policy
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"github.com/emicklei/go-restful"
log "github.com/golang/glog"
"github.com/opensds/multi-cloud/api/pkg/context"
"github.com/opensds/multi-cloud/api/pkg/model"
"github.com/opensds/multi-cloud/api/pkg/utils"
"github.com/opensds/multi-cloud/api/pkg/utils/config"
)
var enforcer *Enforcer
func init() {
enforcer = NewEnforcer(false)
RegisterRules(enforcer)
enforcer.LoadRules(false)
}
type DefaultRule struct {
Name string
CheckStr string
}
func listRules() []DefaultRule {
return []DefaultRule{
{Name: "context_is_admin", CheckStr: "role:admin"},
}
}
func RegisterRules(e *Enforcer) {
e.RegisterDefaults(listRules())
}
func NewEnforcer(overWrite bool) *Enforcer {
return &Enforcer{OverWrite: overWrite}
}
type Enforcer struct {
Rules map[string]BaseCheck
DefaultRules []DefaultRule
OverWrite bool
}
func (e *Enforcer) RegisterDefaults(rules []DefaultRule) {
e.DefaultRules = rules
}
func (e *Enforcer) Enforce(rule string, target map[string]string, cred map[string]interface{}) (bool, error) {
if err := e.LoadRules(false); err != nil {
return false, err
}
toRule, ok := e.Rules[rule]
if !ok {
err := fmt.Errorf("rule [%s] does not exist", rule)
return false, err
}
return check(toRule, target, cred, *e, ""), nil
}
func (e *Enforcer) Authorize(rule string, target map[string]string, cred map[string]interface{}) (bool, error) {
return e.Enforce(rule, target, cred)
}
func (e *Enforcer) LoadRules(forcedReload bool) error {
path := config.CONF.OsdsLet.PolicyPath
fileInfo, err := os.Stat(path)
if err != nil {
return err
}
// Load all policy files that in the specified path
if fileInfo.IsDir() {
files, err := ioutil.ReadDir(path)
if err != nil {
return err
}
for _, f := range files {
if !f.IsDir() && strings.HasSuffix(f.Name(), ".json") {
err := e.LoadPolicyFile(path, forcedReload, false)
if err != nil {
return err
}
}
}
return nil
} else {
return e.LoadPolicyFile(path, forcedReload, e.OverWrite)
}
}
func (e *Enforcer) UpdateRules(rules map[string]BaseCheck) {
if e.Rules == nil {
e.Rules = make(map[string]BaseCheck)
}
for k, c := range rules {
e.Rules[k] = c
}
}
func (e *Enforcer) LoadPolicyFile(path string, forcedReload bool, overWrite bool) error {
// if rules is already set or user doesn't want to force reload, return it.
if e.Rules != nil && !forcedReload {
return nil
}
data, err := ioutil.ReadFile(path)
if err != nil {
msg := fmt.Sprintf("Read policy file (%s) failed, reason:(%v)", path, err)
log.Error(msg)
return fmt.Errorf(msg)
}
r, err := NewRules(data, e.DefaultRules)
if err != nil {
return err
}
if overWrite {
e.Rules = r.Rules
} else {
e.UpdateRules(r.Rules)
}
return nil
}
func NewRules(data []byte, defaultRule []DefaultRule) (*Rules, error) {
r := &Rules{}
err := r.Load(data, defaultRule)
return r, err
}
type Rules struct {
Rules map[string]BaseCheck
}
func (r *Rules) Load(data []byte, defaultRules []DefaultRule) error {
rulesMap := map[string]string{}
err := json.Unmarshal(data, &rulesMap)
if err != nil {
log.Error(err.Error())
return err
}
// add default value
for _, r := range defaultRules {
if v, ok := rulesMap[r.Name]; ok {
log.Warningf("Policy rule (%s:%s) has conflict with default rule(%s:%s),abandon default value\n",
r.Name, v, r.Name, r.CheckStr)
} else {
rulesMap[r.Name] = r.CheckStr
}
}
if r.Rules == nil {
r.Rules = make(map[string]BaseCheck)
}
for k, v := range rulesMap {
r.Rules[k] = parseRule(v)
}
return nil
}
func (r *Rules) String() string {
b, _ := json.MarshalIndent(r.Rules, "", " ")
return string(b)
}
func Authorize(req *restful.Request, res *restful.Response, action string) bool {
if os.Getenv("AUTH_AuthStrategy") != "keystone" {
return true
}
ctx := context.GetContext(req)
credentials := ctx.ToPolicyValue()
//TenantId := httpCtx.Input.Param(":tenantId")
TenantId := req.PathParameter("tenantId")
target := map[string]string{
"tenant_id": TenantId,
}
log.Info("action: %v", action)
log.Info("target: %v", target)
log.Info("Credentials: %v", credentials)
log.V(8).Infof("Action: %v", action)
log.V(8).Infof("Target: %v", target)
log.V(8).Infof("Credentials: %v", credentials)
ok, err := enforcer.Authorize(action, target, credentials)
if err != nil {
log.Errorf("Authorize failed, %s", err)
}
if !ok {
model.HttpError(res, http.StatusForbidden, "Operation is not permitted")
} else {
ctx.IsAdmin = utils.Contained("admin", ctx.Roles)
}
return ok
}