|
|
@ -45,14 +45,12 @@ func NewNodeActions(resource_actions NodeActions, wildcard_actions []string) Nod
|
|
|
|
|
|
|
|
|
|
|
|
type PerNodePolicy struct {
|
|
|
|
type PerNodePolicy struct {
|
|
|
|
GraphNode
|
|
|
|
GraphNode
|
|
|
|
NodeActions map[NodeID]NodeActions
|
|
|
|
Actions map[NodeID]NodeActions
|
|
|
|
WildcardActions NodeActions
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type PerNodePolicyJSON struct {
|
|
|
|
type PerNodePolicyJSON struct {
|
|
|
|
GraphNodeJSON
|
|
|
|
GraphNodeJSON
|
|
|
|
NodeActions map[string]map[string][]string `json:"allowed_actions"`
|
|
|
|
Actions map[string]map[string][]string `json:"actions"`
|
|
|
|
WildcardActions map[string][]string `json:"wildcard_actions"`
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (policy *PerNodePolicy) Type() NodeType {
|
|
|
|
func (policy *PerNodePolicy) Type() NodeType {
|
|
|
@ -61,30 +59,24 @@ func (policy *PerNodePolicy) Type() NodeType {
|
|
|
|
|
|
|
|
|
|
|
|
func (policy *PerNodePolicy) Serialize() ([]byte, error) {
|
|
|
|
func (policy *PerNodePolicy) Serialize() ([]byte, error) {
|
|
|
|
allowed_actions := map[string]map[string][]string{}
|
|
|
|
allowed_actions := map[string]map[string][]string{}
|
|
|
|
for principal, actions := range(policy.NodeActions) {
|
|
|
|
for principal, actions := range(policy.Actions) {
|
|
|
|
allowed_actions[principal.String()] = actions
|
|
|
|
allowed_actions[principal.String()] = actions
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return json.MarshalIndent(&PerNodePolicyJSON{
|
|
|
|
return json.MarshalIndent(&PerNodePolicyJSON{
|
|
|
|
GraphNodeJSON: NewGraphNodeJSON(&policy.GraphNode),
|
|
|
|
GraphNodeJSON: NewGraphNodeJSON(&policy.GraphNode),
|
|
|
|
NodeActions: allowed_actions,
|
|
|
|
Actions: allowed_actions,
|
|
|
|
WildcardActions: policy.WildcardActions,
|
|
|
|
|
|
|
|
}, "", " ")
|
|
|
|
}, "", " ")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewPerNodePolicy(id NodeID, node_actions map[NodeID]NodeActions, wildcard_actions NodeActions) PerNodePolicy {
|
|
|
|
func NewPerNodePolicy(id NodeID, actions map[NodeID]NodeActions) PerNodePolicy {
|
|
|
|
if node_actions == nil {
|
|
|
|
if actions == nil {
|
|
|
|
node_actions = map[NodeID]NodeActions{}
|
|
|
|
actions = map[NodeID]NodeActions{}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if wildcard_actions == nil {
|
|
|
|
|
|
|
|
wildcard_actions = NewNodeActions(nil, nil)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return PerNodePolicy{
|
|
|
|
return PerNodePolicy{
|
|
|
|
GraphNode: NewGraphNode(id),
|
|
|
|
GraphNode: NewGraphNode(id),
|
|
|
|
NodeActions: node_actions,
|
|
|
|
Actions: actions,
|
|
|
|
WildcardActions: wildcard_actions,
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -95,17 +87,17 @@ func LoadPerNodePolicy(ctx *Context, id NodeID, data []byte, nodes NodeMap) (Nod
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
allowed_actions := map[NodeID]NodeActions{}
|
|
|
|
actions := map[NodeID]NodeActions{}
|
|
|
|
for principal_str, actions := range(j.NodeActions) {
|
|
|
|
for principal_str, node_actions := range(j.Actions) {
|
|
|
|
principal_id, err := ParseID(principal_str)
|
|
|
|
principal_id, err := ParseID(principal_str)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
allowed_actions[principal_id] = actions
|
|
|
|
actions[principal_id] = node_actions
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
policy := NewPerNodePolicy(id, allowed_actions, j.WildcardActions)
|
|
|
|
policy := NewPerNodePolicy(id, actions)
|
|
|
|
nodes[id] = &policy
|
|
|
|
nodes[id] = &policy
|
|
|
|
|
|
|
|
|
|
|
|
err = RestoreGraphNode(ctx, &policy.GraphNode, j.GraphNodeJSON, nodes)
|
|
|
|
err = RestoreGraphNode(ctx, &policy.GraphNode, j.GraphNodeJSON, nodes)
|
|
|
@ -117,11 +109,7 @@ func LoadPerNodePolicy(ctx *Context, id NodeID, data []byte, nodes NodeMap) (Nod
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (policy *PerNodePolicy) Allows(action string, resource string, principal NodeID) bool {
|
|
|
|
func (policy *PerNodePolicy) Allows(action string, resource string, principal NodeID) bool {
|
|
|
|
if policy.WildcardActions.Allows(action, resource) == true {
|
|
|
|
node_actions, exists := policy.Actions[principal]
|
|
|
|
return true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
node_actions, exists := policy.NodeActions[principal]
|
|
|
|
|
|
|
|
if exists == false {
|
|
|
|
if exists == false {
|
|
|
|
return false
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -132,3 +120,58 @@ func (policy *PerNodePolicy) Allows(action string, resource string, principal No
|
|
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type SimplePolicy struct {
|
|
|
|
|
|
|
|
GraphNode
|
|
|
|
|
|
|
|
Actions NodeActions
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type SimplePolicyJSON struct {
|
|
|
|
|
|
|
|
GraphNodeJSON
|
|
|
|
|
|
|
|
Actions map[string][]string `json:"actions"`
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (policy *SimplePolicy) Type() NodeType {
|
|
|
|
|
|
|
|
return NodeType("simple_policy")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (policy *SimplePolicy) Serialize() ([]byte, error) {
|
|
|
|
|
|
|
|
return json.MarshalIndent(&SimplePolicyJSON{
|
|
|
|
|
|
|
|
GraphNodeJSON: NewGraphNodeJSON(&policy.GraphNode),
|
|
|
|
|
|
|
|
Actions: policy.Actions,
|
|
|
|
|
|
|
|
}, "", " ")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func NewSimplePolicy(id NodeID, actions NodeActions) SimplePolicy {
|
|
|
|
|
|
|
|
if actions == nil {
|
|
|
|
|
|
|
|
actions = NodeActions{}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return SimplePolicy{
|
|
|
|
|
|
|
|
GraphNode: NewGraphNode(id),
|
|
|
|
|
|
|
|
Actions: actions,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func LoadSimplePolicy(ctx *Context, id NodeID, data []byte, nodes NodeMap) (Node, error) {
|
|
|
|
|
|
|
|
var j SimplePolicyJSON
|
|
|
|
|
|
|
|
err := json.Unmarshal(data, &j)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
policy := NewSimplePolicy(id, j.Actions)
|
|
|
|
|
|
|
|
nodes[id] = &policy
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = RestoreGraphNode(ctx, &policy.GraphNode, j.GraphNodeJSON, nodes)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return &policy, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (policy *SimplePolicy) Allows(action string, resource string, principal NodeID) bool {
|
|
|
|
|
|
|
|
return policy.Actions.Allows(action, resource)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|