policy.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package policy
  15. import (
  16. "context"
  17. "reflect"
  18. "sort"
  19. "strings"
  20. "sync"
  21. "time"
  22. "yunion.io/x/jsonutils"
  23. "yunion.io/x/log"
  24. "yunion.io/x/pkg/errors"
  25. "yunion.io/x/pkg/gotypes"
  26. "yunion.io/x/pkg/util/netutils"
  27. "yunion.io/x/pkg/util/rbacscope"
  28. "yunion.io/x/onecloud/pkg/apis"
  29. identity_api "yunion.io/x/onecloud/pkg/apis/identity"
  30. "yunion.io/x/onecloud/pkg/cloudcommon/consts"
  31. "yunion.io/x/onecloud/pkg/httperrors"
  32. "yunion.io/x/onecloud/pkg/mcclient"
  33. "yunion.io/x/onecloud/pkg/mcclient/auth"
  34. "yunion.io/x/onecloud/pkg/mcclient/modules/identity"
  35. "yunion.io/x/onecloud/pkg/util/ctx"
  36. "yunion.io/x/onecloud/pkg/util/hashcache"
  37. "yunion.io/x/onecloud/pkg/util/rbacutils"
  38. "yunion.io/x/onecloud/pkg/util/tagutils"
  39. )
  40. const (
  41. PolicyDelegation = "delegate"
  42. PolicyActionList = rbacutils.ActionList
  43. PolicyActionGet = rbacutils.ActionGet
  44. PolicyActionUpdate = rbacutils.ActionUpdate
  45. PolicyActionPatch = rbacutils.ActionPatch
  46. PolicyActionCreate = rbacutils.ActionCreate
  47. PolicyActionDelete = rbacutils.ActionDelete
  48. PolicyActionPerform = rbacutils.ActionPerform
  49. )
  50. type PolicyFetchFunc func(ctx context.Context, token mcclient.TokenCredential) (*mcclient.SFetchMatchPoliciesOutput, error)
  51. var (
  52. PolicyManager *SPolicyManager
  53. DefaultPolicyFetcher PolicyFetchFunc
  54. )
  55. func init() {
  56. PolicyManager = &SPolicyManager{
  57. lock: &sync.Mutex{},
  58. }
  59. DefaultPolicyFetcher = auth.FetchMatchPolicies
  60. }
  61. type SPolicyManager struct {
  62. defaultPolicies map[rbacscope.TRbacScope][]*rbacutils.SRbacPolicy
  63. refreshInterval time.Duration
  64. policyCache *hashcache.Cache // policy cache
  65. permissionCache *hashcache.Cache // permission cache
  66. // fetchWorker *appsrv.SWorkerManager
  67. lock *sync.Mutex
  68. }
  69. type sPolicyData struct {
  70. Id string `json:"id"`
  71. Name string `json:"name"`
  72. Enabled bool `json:"enabled"`
  73. DomainId string `json:"domain_id"`
  74. IsPublic bool `json:"is_public"`
  75. PublicScope rbacscope.TRbacScope `json:"public_scope"`
  76. SharedDomains []apis.SharedDomain `json:"shared_domain"`
  77. Scope rbacscope.TRbacScope `json:"scope"`
  78. Policy jsonutils.JSONObject `json:"policy"`
  79. DomainTags tagutils.TTagSet `json:"domain_tags"`
  80. ProjectTags tagutils.TTagSet `json:"project_tags"`
  81. ObjectTags tagutils.TTagSet `json:"resource_tags"`
  82. OrgNodes []identity_api.SOrganizationNodeInfo `json:"org_nodes"`
  83. }
  84. func (data sPolicyData) getPolicy() (*rbacutils.SPolicy, error) {
  85. var domainTags, projectTags, objectTags tagutils.TTagSetList
  86. if len(data.DomainTags) > 0 {
  87. domainTags = domainTags.Append(data.DomainTags)
  88. }
  89. if len(data.ProjectTags) > 0 {
  90. projectTags = projectTags.Append(data.ProjectTags)
  91. }
  92. if len(data.ObjectTags) > 0 {
  93. objectTags = objectTags.Append(data.ObjectTags)
  94. }
  95. for i := range data.OrgNodes {
  96. orgNode := data.OrgNodes[i]
  97. switch orgNode.Type {
  98. case identity_api.OrgTypeDomain:
  99. domainTags = domainTags.Append(orgNode.Tags)
  100. case identity_api.OrgTypeProject:
  101. projectTags = projectTags.Append(orgNode.Tags)
  102. case identity_api.OrgTypeObject:
  103. objectTags = objectTags.Append(orgNode.Tags)
  104. }
  105. }
  106. return rbacutils.DecodePolicyData(domainTags, projectTags, objectTags, data.Policy)
  107. }
  108. func (manager *SPolicyManager) init(refreshInterval time.Duration, workerCount int) {
  109. manager.refreshInterval = refreshInterval
  110. // manager.InitSync(manager)
  111. if len(predefinedDefaultPolicies) > 0 {
  112. policiesMap := make(map[rbacscope.TRbacScope][]*rbacutils.SRbacPolicy)
  113. for i := range predefinedDefaultPolicies {
  114. policy := predefinedDefaultPolicies[i]
  115. if _, ok := policiesMap[policy.Scope]; !ok {
  116. policiesMap[policy.Scope] = make([]*rbacutils.SRbacPolicy, 0)
  117. }
  118. policies := policiesMap[policy.Scope]
  119. policies = append(policies, &policy)
  120. policiesMap[policy.Scope] = policies
  121. }
  122. manager.defaultPolicies = policiesMap
  123. }
  124. manager.policyCache = hashcache.NewCache(2048, refreshInterval)
  125. manager.permissionCache = hashcache.NewCache(2048, refreshInterval)
  126. defaultFetcherFuncAddr := reflect.ValueOf(DefaultPolicyFetcher).Pointer()
  127. remoteFetcherFuncAddr := reflect.ValueOf(auth.FetchMatchPolicies).Pointer()
  128. log.Debugf("DefaultPolicyFetcher: %x RemotePolicyFetcher: %x", defaultFetcherFuncAddr, remoteFetcherFuncAddr)
  129. // var isDB bool
  130. // if defaultFetcherFuncAddr == remoteFetcherFuncAddr {
  131. // remote fetcher, so start watcher
  132. // isDB = false
  133. // } else {
  134. // isDB = true
  135. // }
  136. // if workerCount <= 0 {
  137. // workerCount = 1
  138. // }
  139. // log.Infof("policy fetch worker count %d", workerCount)
  140. // manager.fetchWorker = appsrv.NewWorkerManager("policyFetchWorker", workerCount, 2048, isDB)
  141. }
  142. func getMaskedLoginIp(userCred mcclient.TokenCredential) string {
  143. loginIp, _ := netutils.NewIPV4Addr(userCred.GetLoginIp())
  144. return loginIp.NetAddr(16).String()
  145. }
  146. func policyKey(userCred mcclient.TokenCredential) string {
  147. if userCred == nil || len(userCred.GetTokenString()) == 0 || auth.IsGuestToken(userCred) {
  148. return auth.GUEST_TOKEN
  149. }
  150. keys := []string{userCred.GetProjectId()}
  151. roles := userCred.GetRoleIds()
  152. if len(roles) > 0 {
  153. sort.Strings(roles)
  154. }
  155. keys = append(keys, strings.Join(roles, ":"))
  156. keys = append(keys, getMaskedLoginIp(userCred))
  157. return strings.Join(keys, "-")
  158. }
  159. func permissionKey(scope rbacscope.TRbacScope, userCred mcclient.TokenCredential, service string, resource string, action string, extra ...string) string {
  160. queryKeys := []string{string(scope)}
  161. queryKeys = append(queryKeys, userCred.GetProjectId())
  162. roles := userCred.GetRoleIds()
  163. if len(roles) > 0 {
  164. sort.Strings(roles)
  165. }
  166. queryKeys = append(queryKeys, strings.Join(roles, ":"))
  167. queryKeys = append(queryKeys, getMaskedLoginIp(userCred))
  168. if rbacutils.WILD_MATCH == service || len(service) == 0 {
  169. service = rbacutils.WILD_MATCH
  170. }
  171. queryKeys = append(queryKeys, service)
  172. if rbacutils.WILD_MATCH == resource || len(resource) == 0 {
  173. resource = rbacutils.WILD_MATCH
  174. }
  175. queryKeys = append(queryKeys, resource)
  176. if rbacutils.WILD_MATCH == action || len(action) == 0 {
  177. action = rbacutils.WILD_MATCH
  178. }
  179. queryKeys = append(queryKeys, action)
  180. if len(extra) > 0 {
  181. queryKeys = append(queryKeys, extra...)
  182. }
  183. return strings.Join(queryKeys, "-")
  184. }
  185. func (manager *SPolicyManager) AllowScope(userCred mcclient.TokenCredential, service string, resource string, action string, extra ...string) (rbacscope.TRbacScope, rbacutils.SPolicyResult) {
  186. for _, scope := range []rbacscope.TRbacScope{
  187. rbacscope.ScopeSystem,
  188. rbacscope.ScopeDomain,
  189. rbacscope.ScopeProject,
  190. rbacscope.ScopeUser,
  191. } {
  192. result := manager.allow(scope, userCred, service, resource, action, extra...)
  193. if result.Result == rbacutils.Allow {
  194. return scope, result
  195. }
  196. }
  197. return rbacscope.ScopeNone, rbacutils.PolicyDeny
  198. }
  199. func (manager *SPolicyManager) Allow(targetScope rbacscope.TRbacScope, userCred mcclient.TokenCredential, service string, resource string, action string, extra ...string) rbacutils.SPolicyResult {
  200. var retryScopes []rbacscope.TRbacScope
  201. switch targetScope {
  202. case rbacscope.ScopeSystem:
  203. retryScopes = []rbacscope.TRbacScope{
  204. rbacscope.ScopeSystem,
  205. }
  206. case rbacscope.ScopeDomain:
  207. retryScopes = []rbacscope.TRbacScope{
  208. rbacscope.ScopeSystem,
  209. rbacscope.ScopeDomain,
  210. }
  211. case rbacscope.ScopeProject:
  212. retryScopes = []rbacscope.TRbacScope{
  213. rbacscope.ScopeSystem,
  214. rbacscope.ScopeDomain,
  215. rbacscope.ScopeProject,
  216. }
  217. case rbacscope.ScopeUser:
  218. retryScopes = []rbacscope.TRbacScope{
  219. rbacscope.ScopeSystem,
  220. rbacscope.ScopeUser,
  221. }
  222. }
  223. for _, scope := range retryScopes {
  224. result := manager.allow(scope, userCred, service, resource, action, extra...)
  225. if result.Result == rbacutils.Allow {
  226. return result
  227. }
  228. }
  229. return rbacutils.PolicyDeny
  230. }
  231. func (manager *SPolicyManager) fetchMatchedPolicies(userCred mcclient.TokenCredential) (*mcclient.SFetchMatchPoliciesOutput, error) {
  232. key := policyKey(userCred)
  233. val := manager.policyCache.AtomicGet(key)
  234. if !gotypes.IsNil(val) {
  235. // cache hit
  236. return val.(*mcclient.SFetchMatchPoliciesOutput), nil
  237. }
  238. pg, err := DefaultPolicyFetcher(ctx.CtxWithTime(), userCred)
  239. if err != nil {
  240. return nil, errors.Wrap(err, "DefaultPolicyFetcher")
  241. }
  242. manager.policyCache.AtomicSet(key, pg)
  243. return pg, nil
  244. }
  245. func (manager *SPolicyManager) allow(scope rbacscope.TRbacScope, userCred mcclient.TokenCredential, service string, resource string, action string, extra ...string) rbacutils.SPolicyResult {
  246. // check permission
  247. key := permissionKey(scope, userCred, service, resource, action, extra...)
  248. val := manager.permissionCache.AtomicGet(key)
  249. if !gotypes.IsNil(val) {
  250. if consts.IsRbacDebug() {
  251. log.Debugf("query %s:%s:%s:%s from cache %s", service, resource, action, extra, val)
  252. }
  253. return val.(rbacutils.SPolicyResult)
  254. }
  255. // first download userCred policy
  256. policies, err := manager.fetchMatchedPolicies(userCred)
  257. if err != nil {
  258. log.Errorf("fetchMatchedPolicyGroup fail %s", err)
  259. return rbacutils.PolicyDeny
  260. }
  261. policySet, ok := policies.Policies[scope]
  262. if !ok {
  263. policySet = rbacutils.TPolicySet{}
  264. }
  265. result := manager.allowWithoutCache(policySet, scope, userCred, service, resource, action, extra...)
  266. manager.permissionCache.AtomicSet(key, result)
  267. return result
  268. }
  269. /*
  270. func (manager *SPolicyManager) findPolicyByName(scope rbacscope.TRbacScope, name string) *rbacscope.SRbacPolicyCore {
  271. if policies, ok := manager.policies[scope]; ok {
  272. for i := range policies {
  273. if policies[i].Id == name || policies[i].Name == name {
  274. return policies[i].Policy
  275. }
  276. }
  277. }
  278. return nil
  279. }
  280. func getMatchedPolicyNames(policies []rbacscope.SPolicyInfo, userCred rbacscope.IRbacIdentity) []string {
  281. _, matchNames := rbacscope.GetMatchedPolicies(policies, userCred)
  282. return matchNames
  283. }
  284. func getMatchedPolicyRules(policies []rbacscope.SPolicyInfo, userCred rbacscope.IRbacIdentity, service string, resource string, action string, extra ...string) ([]rbacscope.SRbacRule, bool) {
  285. matchPolicies, _ := rbacscope.GetMatchedPolicies(policies, userCred)
  286. if len(matchPolicies) == 0 {
  287. return nil, false
  288. }
  289. return matchPolicies.GetMatchRules(service, resource, action, extra...), true
  290. }
  291. */
  292. func (manager *SPolicyManager) allowWithoutCache(policies rbacutils.TPolicySet, scope rbacscope.TRbacScope, userCred mcclient.TokenCredential, service string, resource string, action string, extra ...string) rbacutils.SPolicyResult {
  293. matchRules := rbacutils.TPolicyMatches{}
  294. if len(policies) == 0 {
  295. user, token := auth.GUEST_USER, auth.GUEST_TOKEN
  296. if userCred != nil {
  297. user = userCred.GetUserName()
  298. token = userCred.GetTokenString()
  299. }
  300. log.Warningf("no policies fetched for scope %s user %s token %s key %s", scope, user, token, policyKey(userCred))
  301. } else {
  302. matchRules = policies.GetMatchRules(service, resource, action, extra...)
  303. if consts.IsRbacDebug() {
  304. log.Debugf("service %s resource %s action %s extra %s matchRules: %s", service, resource, action, jsonutils.Marshal(extra), jsonutils.Marshal(matchRules))
  305. }
  306. }
  307. scopedDeny := false
  308. switch scope {
  309. case rbacscope.ScopeUser:
  310. if !isUserResource(service, resource) {
  311. scopedDeny = true
  312. }
  313. case rbacscope.ScopeProject:
  314. if !isProjectResource(service, resource) {
  315. scopedDeny = true
  316. }
  317. case rbacscope.ScopeDomain:
  318. if isSystemResource(service, resource) {
  319. scopedDeny = true
  320. }
  321. case rbacscope.ScopeSystem:
  322. // no deny at all for system scope
  323. }
  324. if scopedDeny {
  325. rule := rbacutils.SPolicyMatch{
  326. Rule: rbacutils.SRbacRule{
  327. Service: service,
  328. Resource: resource,
  329. Result: rbacutils.Deny,
  330. },
  331. }
  332. matchRules = append(matchRules, rule)
  333. }
  334. result := matchRules.GetResult()
  335. if result.Result.IsDeny() {
  336. // denied, try default policies
  337. defaultPolicies, ok := manager.defaultPolicies[scope]
  338. if ok {
  339. for i := range defaultPolicies {
  340. isMatched, _ := defaultPolicies[i].Match(userCred)
  341. if !isMatched {
  342. continue
  343. }
  344. rule := defaultPolicies[i].Rules.GetMatchRule(service, resource, action, extra...)
  345. if rule != nil {
  346. if consts.IsRbacDebug() {
  347. log.Debugf("service: %s resource: %s action: %s extra: %s match default policy: %s match rule: %s", service, resource, action, jsonutils.Marshal(extra), jsonutils.Marshal(defaultPolicies[i]), jsonutils.Marshal(rule))
  348. }
  349. matchRules = append(matchRules,
  350. rbacutils.SPolicyMatch{
  351. Rule: *rule,
  352. },
  353. )
  354. }
  355. }
  356. }
  357. result = matchRules.GetResult()
  358. }
  359. if consts.IsRbacDebug() {
  360. log.Debugf("[RBAC: %s] %s %s %s %s permission %s userCred: %s MatchRules: %d(%s)", scope, service, resource, action, jsonutils.Marshal(extra), result, userCred, len(matchRules), jsonutils.Marshal(matchRules))
  361. }
  362. return result
  363. }
  364. // result: allow/deny for the named policy
  365. // userResult: allow/deny for the matched policies of userCred
  366. func explainPolicy(userCred mcclient.TokenCredential, policyReq jsonutils.JSONObject, policyData *sPolicyData) ([]string, rbacutils.SPolicyResult, rbacutils.SPolicyResult, error) {
  367. _, request, result, userResult, err := explainPolicyInternal(userCred, policyReq, policyData)
  368. return request, result, userResult, err
  369. }
  370. func fetchPolicyDataByIdOrName(ctx context.Context, id string) (*sPolicyData, error) {
  371. s := auth.GetAdminSession(ctx, consts.GetRegion())
  372. data, err := identity.Policies.Get(s, id, nil)
  373. if err != nil {
  374. return nil, errors.Wrap(err, "modules.Policies.Get")
  375. }
  376. pdata := &sPolicyData{}
  377. err = data.Unmarshal(&pdata)
  378. if err != nil {
  379. return nil, errors.Wrap(err, "Unmarshal Policy Data")
  380. }
  381. return pdata, nil
  382. }
  383. func explainPolicyInternal(userCred mcclient.TokenCredential, policyReq jsonutils.JSONObject, policyData *sPolicyData) (rbacscope.TRbacScope, []string, rbacutils.SPolicyResult, rbacutils.SPolicyResult, error) {
  384. policySeq, err := policyReq.GetArray()
  385. if err != nil {
  386. return rbacscope.ScopeSystem, nil, rbacutils.PolicyDeny, rbacutils.PolicyDeny, httperrors.NewInputParameterError("invalid format")
  387. }
  388. service := rbacutils.WILD_MATCH
  389. resource := rbacutils.WILD_MATCH
  390. action := rbacutils.WILD_MATCH
  391. extra := make([]string, 0)
  392. if len(policySeq) > 1 {
  393. service, _ = policySeq[1].GetString()
  394. }
  395. if len(policySeq) > 2 {
  396. resource, _ = policySeq[2].GetString()
  397. }
  398. if len(policySeq) > 3 {
  399. action, _ = policySeq[3].GetString()
  400. }
  401. if len(policySeq) > 4 {
  402. for i := 4; i < len(policySeq); i += 1 {
  403. ev, _ := policySeq[i].GetString()
  404. extra = append(extra, ev)
  405. }
  406. }
  407. reqStrs := []string{service, resource, action}
  408. if len(extra) > 0 {
  409. reqStrs = append(reqStrs, extra...)
  410. }
  411. scopeStr, _ := policySeq[0].GetString()
  412. scope := rbacscope.String2Scope(scopeStr)
  413. userResult := PolicyManager.Allow(scope, userCred, service, resource, action, extra...)
  414. result := userResult
  415. if policyData != nil {
  416. if scope.HigherThan(policyData.Scope) {
  417. result = rbacutils.PolicyDeny
  418. } else {
  419. policy, err := policyData.getPolicy()
  420. if err != nil {
  421. return scope, reqStrs, rbacutils.PolicyDeny, userResult, errors.Wrap(err, "getPolicy")
  422. }
  423. match := policy.GetMatchRule(service, resource, action, extra...)
  424. result = rbacutils.PolicyDeny
  425. if match != nil {
  426. result.Result = match.Rule.Result
  427. result.DomainTags = match.DomainTags
  428. result.ProjectTags = match.ProjectTags
  429. result.ObjectTags = match.ObjectTags
  430. }
  431. }
  432. }
  433. return scope, reqStrs, result, userResult, nil
  434. }
  435. func ExplainRpc(ctx context.Context, userCred mcclient.TokenCredential, params jsonutils.JSONObject, name string) (jsonutils.JSONObject, error) {
  436. paramDict, err := params.GetMap()
  437. if err != nil {
  438. return nil, httperrors.NewInputParameterError("invalid input format")
  439. }
  440. var policyData *sPolicyData
  441. if len(name) > 0 {
  442. policyData, err = fetchPolicyDataByIdOrName(ctx, name)
  443. if err != nil {
  444. return nil, errors.Wrap(err, "fetchPolicyDataByIdOrName")
  445. }
  446. }
  447. ret := jsonutils.NewDict()
  448. for key, policyReq := range paramDict {
  449. reqStrs, result, userResult, err := explainPolicy(userCred, policyReq, policyData)
  450. if err != nil {
  451. return nil, err
  452. }
  453. reqStrs = append(reqStrs, string(result.Result))
  454. if len(name) > 0 {
  455. reqStrs = append(reqStrs, string(userResult.Result))
  456. }
  457. ret.Add(jsonutils.NewStringArray(reqStrs), key)
  458. }
  459. return ret, nil
  460. }
  461. func (manager *SPolicyManager) IsScopeCapable(userCred mcclient.TokenCredential, scope rbacscope.TRbacScope) bool {
  462. policies, err := manager.fetchMatchedPolicies(userCred)
  463. if err != nil {
  464. log.Errorf("fetchMatchedPolicyGroup fail %s", err)
  465. return false
  466. }
  467. if set, ok := policies.Policies[scope]; ok && len(set) > 0 {
  468. return true
  469. }
  470. return false
  471. }
  472. /*
  473. func (manager *SPolicyManager) MatchedPolicyNames(ctx context.Context, scope rbacscope.TRbacScope, ident rbacscope.IRbacIdentity) []string {
  474. policies, err := manager.fetchMatchedPolicies(ctx, userCred)
  475. if err != nil {
  476. log.Errorf("fetchMatchedPolicyGroup fail %s", err)
  477. return false
  478. }
  479. ret := make([]string, 0)
  480. policies, ok := manager.policies[scope]
  481. if !ok {
  482. return ret
  483. }
  484. return getMatchedPolicyNames(policies, userCred)
  485. }*/
  486. /*
  487. func (manager *SPolicyManager) AllPolicies() map[string][]string {
  488. ret := make(map[string][]string)
  489. for scope, p := range manager.policies {
  490. k := string(scope)
  491. ret[k] = make([]string, len(p))
  492. for i := range p {
  493. ret[k][i] = p[i].Name
  494. }
  495. }
  496. return ret
  497. }
  498. func (manager *SPolicyManager) RoleMatchPolicies(roleName string) []string {
  499. ident := rbacscope.NewRbacIdentity("", "", []string{roleName})
  500. ret := make([]string, 0)
  501. for _, policies := range manager.policies {
  502. for i := range policies {
  503. if matched, _ := policies[i].Policy.Match(ident); matched {
  504. ret = append(ret, policies[i].Name)
  505. }
  506. }
  507. }
  508. return ret
  509. }
  510. func (manager *SPolicyManager) GetMatchedPolicySet(userCred rbacscope.IRbacIdentity) (rbacscope.TRbacScope, rbacscope.TPolicySet) {
  511. for _, scope := range []rbacscope.TRbacScope{
  512. rbacscope.ScopeSystem,
  513. rbacscope.ScopeDomain,
  514. rbacscope.ScopeProject,
  515. } {
  516. macthed, _ := rbacscope.GetMatchedPolicies(manager.policies[scope], userCred)
  517. if len(macthed) > 0 {
  518. return scope, macthed
  519. }
  520. }
  521. return rbacscope.ScopeNone, nil
  522. }
  523. */