본문으로 건너뛰기

관리 API

Management API는 모든 정책 및 그룹(역할) 작업(가져오기, 추가, 제거, 업데이트, 필터링)을 다룹니다. 아래에서 e는 Enforcer 인스턴스입니다.

필터링 API​

필터링 메서드는 (fieldIndex int, fieldValues ...string)를 인자로 받습니다: 매칭은 fieldIndex에서 시작되며, fieldValues는 연속된 위치에서 요구되는 값들입니다. 해당 위치의 와일드카드로 fieldValues에 ""를 사용하세요.

예시:

p, alice, book, read
p, bob, book, read
p, bob, book, write
p, alice, pen, get
p, bob, pen ,get
e.GetFilteredPolicy(1, "book") // returns: [["alice", "book", "read"], ["bob", "book", "read"], ["bob", "book", "write"]]

e.GetFilteredPolicy(1, "book", "read") // returns: [["alice", "book", "read"], ["bob", "book", "read"]]

e.GetFilteredPolicy(0, "alice", "", "read") // returns: [[alice book read]]

e.GetFilteredPolicy(0, "alice") // returns: [["alice", "book", "read"], ["alice", "pen", "get"]]

API 참조​

e, err := NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")

Enforce()​

Enforce는 "주체(subject)"가 "객체(object)"에 대해 "작업(action)"을 수행할 수 있는지 여부를 결정합니다. 입력 매개변수는 일반적으로 (sub, obj, act)입니다.

예를 들어:

ok, err := e.Enforce(request)

EnforceWithMatcher()​

EnforceWithMatcher use a custom matcher to decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (matcher, sub, obj, act), use model matcher by default when matcher is "".

예시:

ok, err := e.EnforceWithMatcher(matcher, request)

EnforceEx()​

EnforceEx explain enforcement by informing matched rules.

예시:

ok, reason, err := e.EnforceEx(request)

EnforceExWithMatcher()​

EnforceExWithMatcher use a custom matcher and explain enforcement by informing matched rules.

예시:

ok, reason, err := e.EnforceExWithMatcher(matcher, request)

BatchEnforce()​

BatchEnforce enforces each request and returns result in a bool array

예시:

boolArray, err := e.BatchEnforce(requests)

GetAllSubjects()​

GetAllSubjects gets the list of subjects that show up in the current policy.

예시:

allSubjects := e.GetAllSubjects()

GetAllNamedSubjects()​

GetAllNamedSubjects gets the list of subjects that show up in the current named policy.

예시:

allNamedSubjects := e.GetAllNamedSubjects("p")

GetAllObjects()​

GetAllObjects gets the list of objects that show up in the current policy.

예시:

allObjects := e.GetAllObjects()

GetAllNamedObjects()​

GetAllNamedObjects gets the list of objects that show up in the current named policy.

예시:

allNamedObjects := e.GetAllNamedObjects("p")

GetAllActions()​

GetAllActions gets the list of actions that show up in the current policy.

예시:

allActions := e.GetAllActions()

GetAllNamedActions()​

GetAllNamedActions gets the list of actions that show up in the current named policy.

예시:

allNamedActions := e.GetAllNamedActions("p")

GetAllRoles()​

GetAllRoles gets the list of roles that show up in the current policy.

예시:

allRoles = e.GetAllRoles()

GetAllNamedRoles()​

GetAllNamedRoles gets the list of roles that show up in the current named policy.

예시:

allNamedRoles := e.GetAllNamedRoles("g")

GetPolicy()​

GetPolicy gets all the authorization rules in the policy.

예시:

policy = e.GetPolicy()

GetFilteredPolicy()​

GetFilteredPolicy gets all the authorization rules in the policy, field filters can be specified.

예를 들어:

filteredPolicy := e.GetFilteredPolicy(0, "alice")

GetNamedPolicy()​

GetNamedPolicy는 명명된 정책(named policy)의 모든 권한 부여 규칙을 가져옵니다.

예를 들어:

namedPolicy := e.GetNamedPolicy("p")

GetFilteredNamedPolicy()​

GetFilteredNamedPolicy는 명명된 정책의 모든 권한 부여 규칙을 가져오며, 필드 필터를 지정할 수 있습니다.

예를 들어:

filteredNamedPolicy = e.GetFilteredNamedPolicy("p", 0, "bob")

GetGroupingPolicy()​

GetGroupingPolicy는 정책 내의 모든 역할 상속 규칙을 가져옵니다.

예를 들어:

groupingPolicy := e.GetGroupingPolicy()

GetFilteredGroupingPolicy()​

GetFilteredGroupingPolicy는 정책 내의 모든 역할 상속 규칙을 가져오며, 필드 필터를 지정할 수 있습니다.

예를 들어:

filteredGroupingPolicy := e.GetFilteredGroupingPolicy(0, "alice")

GetNamedGroupingPolicy()​

GetNamedGroupingPolicy는 정책 내의 모든 역할 상속 규칙을 가져옵니다.

예를 들어:

namedGroupingPolicy := e.GetNamedGroupingPolicy("g")

GetFilteredNamedGroupingPolicy()​

GetFilteredNamedGroupingPolicy는 정책 내의 모든 역할 상속 규칙을 가져옵니다.

예를 들어:

namedGroupingPolicy := e.GetFilteredNamedGroupingPolicy("g", 0, "alice")

HasPolicy()​

HasPolicy는 권한 부여 규칙이 존재하는지 여부를 판단합니다.

예를 들어:

hasPolicy := e.HasPolicy("data2_admin", "data2", "read")

HasNamedPolicy()​

HasNamedPolicy는 명명된 권한 부여 규칙이 존재하는지 여부를 판단합니다.

예를 들어:

hasNamedPolicy := e.HasNamedPolicy("p", "data2_admin", "data2", "read")

AddPolicy()​

AddPolicy는 현재 정책에 권한 부여 규칙을 추가합니다. 규칙이 이미 존재하면 함수는 false를 반환하며 규칙은 추가되지 않습니다. 그렇지 않으면 새로운 규칙을 추가하고 true를 반환합니다.

예를 들어:

added := e.AddPolicy('eve', 'data3', 'read')

AddPolicies()​

AddPolicies는 현재 정책에 권한 부여 규칙들을 추가합니다. 해당 작업은 원자적(atomic)입니다. 따라서 추가하려는 권한 부여 규칙들이 현재 정책과 일치하지 않는 경우, 함수는 false를 반환하며 어떤 정책 규칙도 현재 정책에 추가되지 않습니다. 모든 권한 부여 규칙들이 정책 규칙과 일치하는 경우, 함수는 true를 반환하며 각 정책 규칙이 현재 정책에 추가됩니다.

예를 들어:

rules := [][] string {
[]string {"jack", "data4", "read"},
[]string {"katy", "data4", "write"},
[]string {"leyo", "data4", "read"},
[]string {"ham", "data4", "write"},
}

areRulesAdded := e.AddPolicies(rules)

AddPoliciesEx()​

AddPoliciesEx는 현재 정책에 권한 부여 규칙들을 추가합니다. 규칙이 이미 존재하면 해당 규칙은 추가되지 않습니다. 하지만 AddPolicies와 달리, 다른 존재하지 않는 규칙들은 false를 즉시 반환하는 대신 추가됩니다.

예를 들어:

ok, err := e.AddPoliciesEx([][]string{{"user1", "data1", "read"}, {"user2", "data2", "read"}})

AddNamedPolicy()​

AddNamedPolicy는 현재 명명된 정책에 권한 부여 규칙을 추가합니다. 규칙이 이미 존재하면 함수는 false를 반환하며 규칙은 추가되지 않습니다. 그렇지 않으면 새로운 규칙을 추가하고 true를 반환합니다.

예를 들어:

added := e.AddNamedPolicy("p", "eve", "data3", "read")

AddNamedPolicies()​

AddNamedPolicies는 현재 명명된 정책에 권한 부여 규칙들을 추가합니다. 해당 작업은 원자적(atomic)입니다. 따라서 추가하려는 권한 부여 규칙들이 현재 정책과 일치하지 않는 경우, 함수는 false를 반환하며 어떤 정책 규칙도 현재 정책에 추가되지 않습니다. 모든 권한 부여 규칙들이 정책 규칙과 일치하는 경우, 함수는 true를 반환하며 각 정책 규칙이 현재 정책에 추가됩니다.

예를 들어:

rules := [][] string {
[]string {"jack", "data4", "read"},
[]string {"katy", "data4", "write"},
[]string {"leyo", "data4", "read"},
[]string {"ham", "data4", "write"},
}

areRulesAdded := e.AddNamedPolicies("p", rules)

AddNamedPoliciesEx()​

AddNamedPoliciesEx는 현재 named policy에 권한 부여 규칙을 추가합니다. 규칙이 이미 존재하는 경우, 해당 규칙은 추가되지 않습니다. 하지만 AddNamedPolicies와 달리, 다른 존재하지 않는 규칙들은 false를 반환하는 대신 추가됩니다.

예를 들어:

ok, err := e.AddNamedPoliciesEx("p", [][]string{{"user1", "data1", "read"}, {"user2", "data2", "read"}})

SelfAddPoliciesEx()​

SelfAddPoliciesEx는 autoNotifyWatcher가 비활성화된 상태로 현재 named policy에 권한 부여 규칙을 추가합니다. 규칙이 이미 존재하는 경우, 해당 규칙은 추가되지 않습니다. 하지만 SelfAddPolicies와 달리, 다른 존재하지 않는 규칙들은 false를 반환하는 대신 추가됩니다.

예를 들어:

ok, err := e.SelfAddPoliciesEx("p", "p", [][]string{{"user1", "data1", "read"}, {"user2", "data2", "read"}})

RemovePolicy()​

RemovePolicy는 현재 policy에서 권한 부여 규칙을 제거합니다.

예를 들어:

removed := e.RemovePolicy("alice", "data1", "read")

RemovePolicies()​

RemovePolicies는 현재 policy에서 권한 부여 규칙들을 제거합니다. 해당 연산은 원자적(atomic) 성격을 가집니다. 따라서 권한 부여 규칙들이 현재 policy와 일치하지 않는 규칙으로 구성된 경우, 함수는 false를 반환하며 현재 policy에서 어떤 규칙도 제거되지 않습니다. 모든 권한 부여 규칙들이 policy 규칙과 일치하는 경우, 함수는 true를 반환하고 각 policy 규칙은 현재 policy에서 제거됩니다.

예를 들어:

rules := [][] string {
[]string {"jack", "data4", "read"},
[]string {"katy", "data4", "write"},
[]string {"leyo", "data4", "read"},
[]string {"ham", "data4", "write"},
}

areRulesRemoved := e.RemovePolicies(rules)

RemoveFilteredPolicy()​

RemoveFilteredPolicy는 필드 필터를 지정하여 현재 policy에서 권한 부여 규칙을 제거합니다. RemovePolicy는 현재 policy에서 권한 부여 규칙을 제거합니다.

예를 들어:

removed := e.RemoveFilteredPolicy(0, "alice", "data1", "read")

RemoveNamedPolicy()​

RemoveNamedPolicy는 현재 named policy에서 권한 부여 규칙을 제거합니다.

예를 들어:

removed := e.RemoveNamedPolicy("p", "alice", "data1", "read")

RemoveNamedPolicies()​

RemoveNamedPolicies는 현재 named policy에서 권한 부여 규칙들을 제거합니다. 해당 연산은 원자적(atomic) 성격을 가집니다. 따라서 권한 부여 규칙들이 현재 policy와 일치하지 않는 규칙으로 구성된 경우, 함수는 false를 반환하며 현재 policy에서 어떤 규칙도 제거되지 않습니다. 모든 권한 부여 규칙들이 policy 규칙과 일치하는 경우, 함수는 true를 반환하고 각 policy 규칙은 현재 named policy에서 제거됩니다.

예를 들어:

rules := [][] string {
[]string {"jack", "data4", "read"},
[]string {"katy", "data4", "write"},
[]string {"leyo", "data4", "read"},
[]string {"ham", "data4", "write"},
}

areRulesRemoved := e.RemoveNamedPolicies("p", rules)

RemoveFilteredNamedPolicy()​

RemoveFilteredNamedPolicy는 필드 필터를 지정하여 현재 named policy에서 권한 부여 규칙을 제거합니다.

예를 들어:

removed := e.RemoveFilteredNamedPolicy("p", 0, "alice", "data1", "read")

HasGroupingPolicy()​

HasGroupingPolicy는 역할 상속 규칙이 존재하는지 확인합니다.

예를 들어:

has := e.HasGroupingPolicy("alice", "data2_admin")

HasNamedGroupingPolicy()​

HasNamedGroupingPolicy는 named 역할 상속 규칙이 존재하는지 확인합니다.

예를 들어:

has := e.HasNamedGroupingPolicy("g", "alice", "data2_admin")

AddGroupingPolicy()​

AddGroupingPolicy는 현재 policy에 역할 상속 규칙을 추가합니다. 규칙이 이미 존재하는 경우, 함수는 false를 반환하며 해당 규칙은 추가되지 않습니다. 그 외의 경우, 새로운 규칙을 추가하고 함수는 true를 반환합니다.

예를 들어:

added := e.AddGroupingPolicy("group1", "data2_admin")

AddGroupingPolicies()​

AddGroupingPolicies는 현재 policy에 역할 상속 규칙들을 추가합니다. 해당 연산은 원자적(atomic) 성격을 가집니다. 따라서 권한 부여 규칙들이 현재 policy와 일치하지 않는 규칙으로 구성된 경우, 함수는 false를 반환하며 현재 policy에 어떤 규칙도 추가되지 않습니다. 모든 권한 부여 규칙들이 policy 규칙과 일치하는 경우, 함수는 true를 반환하고 각 policy 규칙은 현재 policy에 추가됩니다.

예를 들어:

rules := [][] string {
[]string {"ham", "data4_admin"},
[]string {"jack", "data5_admin"},
}

areRulesAdded := e.AddGroupingPolicies(rules)

AddGroupingPoliciesEx()​

AddGroupingPoliciesEx는 현재 policy에 역할 상속 규칙들을 추가합니다. 규칙이 이미 존재하는 경우, 해당 규칙은 추가되지 않습니다. 하지만 AddGroupingPolicies와 달리, 다른 존재하지 않는 규칙들은 false를 반환하는 대신 추가됩니다.

예를 들어:

ok, err := e.AddGroupingPoliciesEx([][]string{{"user1", "member"}, {"user2", "member"}})

AddNamedGroupingPolicy()​

AddNamedGroupingPolicy adds a named role inheritance rule to the current policy. If the rule already exists, the function returns false and the rule will not be added. Otherwise the function returns true by adding the new rule.

예시:

added := e.AddNamedGroupingPolicy("g", "group1", "data2_admin")

AddNamedGroupingPolicies()​

AddNamedGroupingPolicies adds named role inheritance rules to the current policy. The operation is atomic in nature. Hence, if authorization rules consists of rules which are not consistent with the current policy, the function returns false and no policy rule is added to the current policy. If all the authorization rules are consistent with the policy rules, the function returns true and each policy rule is added to the current policy.

예시:

rules := [][] string {
[]string {"ham", "data4_admin"},
[]string {"jack", "data5_admin"},
}

areRulesAdded := e.AddNamedGroupingPolicies("g", rules)

AddNamedGroupingPoliciesEx()​

AddNamedGroupingPoliciesEx adds named role inheritance rules to the current policy. If the rule already exists, the rule will not be added. But unlike AddNamedGroupingPolicies, other non-existent rules are added instead of returning false directly

예시:

ok, err := e.AddNamedGroupingPoliciesEx("g", [][]string{{"user1", "member"}, {"user2", "member"}})

RemoveGroupingPolicy()​

RemoveGroupingPolicy removes a role inheritance rule from the current policy.

예시:

removed := e.RemoveGroupingPolicy("alice", "data2_admin")

RemoveGroupingPolicies()​

RemoveGroupingPolicies removes role inheritance rules from the current policy. The operation is atomic in nature. Hence, if authorization rules consists of rules which are not consistent with the current policy, the function returns false and no policy rule is removed from the current policy. If all the authorization rules are consistent with the policy rules, the function returns true and each policy rule is removed from the current policy.

예시:

rules := [][] string {
[]string {"ham", "data4_admin"},
[]string {"jack", "data5_admin"},
}

areRulesRemoved := e.RemoveGroupingPolicies(rules)

RemoveFilteredGroupingPolicy()​

RemoveFilteredGroupingPolicy removes a role inheritance rule from the current policy, field filters can be specified.

예시:

removed := e.RemoveFilteredGroupingPolicy(0, "alice")

RemoveNamedGroupingPolicy()​

RemoveNamedGroupingPolicy removes a role inheritance rule from the current named policy.

예시:

removed := e.RemoveNamedGroupingPolicy("g", "alice")

RemoveNamedGroupingPolicies()​

RemoveNamedGroupingPolicies removes named role inheritance rules from the current policy. The operation is atomic in nature. Hence, if authorization rules consists of rules which are not consistent with the current policy, the function returns false and no policy rule is removed from the current policy. If all the authorization rules are consistent with the policy rules, the function returns true and each policy rule is removed from the current policy.

예시:

rules := [][] string {
[]string {"ham", "data4_admin"},
[]string {"jack", "data5_admin"},
}

areRulesRemoved := e.RemoveNamedGroupingPolicies("g", rules)

RemoveFilteredNamedGroupingPolicy()​

RemoveFilteredNamedGroupingPolicy removes a role inheritance rule from the current named policy, field filters can be specified.

예시:

removed := e.RemoveFilteredNamedGroupingPolicy("g", 0, "alice")

UpdatePolicy()​

UpdatePolicy update a old policy to new policy.

예시:

updated, err := e.UpdatePolicy([]string{"eve", "data3", "read"}, []string{"eve", "data3", "write"})

UpdatePolicies()​

UpdatePolicies updates all old policies to new policies.

예시:

updated, err := e.UpdatePolicies([][]string{{"eve", "data3", "read"}, {"jack", "data3", "read"}}, [][]string{{"eve", "data3", "write"}, {"jack", "data3", "write"}})

UpdateNamedPolicy()​

UpdateNamedPolicy update a old policy to new policy in the named policy.

예시:

updated, err := e.UpdateNamedPolicy("p", []string{"eve", "data3", "read"}, []string{"eve", "data3", "write"})

UpdateNamedPolicies()​

UpdateNamedPolicies updates all old policies to new policies in the named policy.

예시:

updated, err := e.UpdateNamedPolicies("p", [][]string{{"eve", "data3", "read"}, {"jack", "data3", "read"}}, [][]string{{"eve", "data3", "write"}, {"jack", "data3", "write"}})

UpdateFilteredPolicies()​

UpdateFilteredPolicies updates all old policies that match the filter to new policies.

예를 들어:

updated, err := e.UpdateFilteredPolicies([][]string{{"eve", "data3", "write"}}, 0, "eve", "data3", "read")

AddFunction()​

AddFunction은 사용자 정의 함수를 추가합니다.

예를 들어:

func CustomFunction(key1 string, key2 string) bool {
if key1 == "/alice_data2/myid/using/res_id" && key2 == "/alice_data/:resource" {
return true
} else if key1 == "/alice_data2/myid/using/res_id" && key2 == "/alice_data2/:id/using/:resId" {
return true
} else {
return false
}
}

func CustomFunctionWrapper(args ...interface{}) (interface{}, error) {
key1 := args[0].(string)
key2 := args[1].(string)

return bool(CustomFunction(key1, key2)), nil
}

e.AddFunction("keyMatchCustom", CustomFunctionWrapper)

LoadFilteredPolicy()​

LoadFilteredPolicy는 파일/데이터베이스에서 필터링된 정책을 로드합니다.

예를 들어:

err := e.LoadFilteredPolicy()

LoadIncrementalFilteredPolicy()​

LoadIncrementalFilteredPolicy는 파일/데이터베이스에서 필터링된 정책을 추가합니다.

예를 들어:

err := e.LoadIncrementalFilteredPolicy()

UpdateGroupingPolicy()​

UpdateGroupingPolicy는 g 섹션의 oldRule을 newRule로 업데이트합니다.

예를 들어:

succeed, err : = e.UpdateGroupingPolicy([]string{"data3_admin", "data4_admin"}, []string{"admin", "data4_admin"})

UpdateNamedGroupingPolicy()​

UpdateNamedGroupingPolicy는 g 섹션에서 이름이 ptype인 oldRule을 newRule로 업데이트합니다.

예를 들어:

succeed, err : = e.UpdateGroupingPolicy("g1",[]string{"data3_admin", "data4_admin"}, []string{"admin", "data4_admin"})

SetFieldIndex()​

SetFieldIndex는 sub, obj, domain, priority의 관례적인 이름 및 위치를 맞춤 설정할 수 있습니다.

[policy_definition]
p = customized_priority, obj, act, eft, subject

예를 들어:

e.SetFieldIndex("p", constant.PriorityIndex, 0)
e.SetFieldIndex("p", constant.SubjectIndex, 4)

Comments are powered by giscus and GitHub Discussions. They load from giscus.app and GitHub only after you click the button below.