package acl
 
import (
"github.com/armon/go-radix"
)
 
var (
// allowAll is a singleton policy which allows all
// non-management actions
allowAll ACL
 
// denyAll is a singleton policy which denies all actions
denyAll ACL
 
// manageAll is a singleton policy which allows all
// actions, including management
manageAll ACL
)
 
func init() {
// Setup the singletons
allowAll = &StaticACL{
allowManage:  false,
defaultAllow: true,
}
denyAll = &StaticACL{
allowManage:  false,
defaultAllow: false,
}
manageAll = &StaticACL{
allowManage:  true,
defaultAllow: true,
}
}
 
// ACL is the interface for policy enforcement.
type ACL interface {
// ACLList checks for permission to list all the ACLs
ACLList() bool
 
// ACLModify checks for permission to manipulate ACLs
ACLModify() bool
 
// AgentRead checks for permission to read from agent endpoints for a
// given node.
AgentRead(string) bool
 
// AgentWrite checks for permission to make changes via agent endpoints
// for a given node.
AgentWrite(string) bool
 
// EventRead determines if a specific event can be queried.
EventRead(string) bool
 
// EventWrite determines if a specific event may be fired.
EventWrite(string) bool
 
// KeyRead checks for permission to read a given key
KeyRead(string) bool
 
// KeyWrite checks for permission to write a given key
KeyWrite(string) bool
 
// KeyWritePrefix checks for permission to write to an
// entire key prefix. This means there must be no sub-policies
// that deny a write.
KeyWritePrefix(string) bool
 
// KeyringRead determines if the encryption keyring used in
// the gossip layer can be read.
KeyringRead() bool
 
// KeyringWrite determines if the keyring can be manipulated
KeyringWrite() bool
 
// NodeRead checks for permission to read (discover) a given node.
NodeRead(string) bool
 
// NodeWrite checks for permission to create or update (register) a
// given node.
NodeWrite(string) bool
 
// OperatorRead determines if the read-only Consul operator functions
// can be used.
OperatorRead() bool
 
// OperatorWrite determines if the state-changing Consul operator
// functions can be used.
OperatorWrite() bool
 
// PrepardQueryRead determines if a specific prepared query can be read
// to show its contents (this is not used for execution).
PreparedQueryRead(string) bool
 
// PreparedQueryWrite determines if a specific prepared query can be
// created, modified, or deleted.
PreparedQueryWrite(string) bool
 
// ServiceRead checks for permission to read a given service
ServiceRead(string) bool
 
// ServiceWrite checks for permission to create or update a given
// service
ServiceWrite(string) bool
 
// SessionRead checks for permission to read sessions for a given node.
SessionRead(string) bool
 
// SessionWrite checks for permission to create sessions for a given
// node.
SessionWrite(string) bool
 
// Snapshot checks for permission to take and restore snapshots.
Snapshot() bool
}
 
// StaticACL is used to implement a base ACL policy. It either
// allows or denies all requests. This can be used as a parent
// ACL to act in a blacklist or whitelist mode.
type StaticACL struct {
allowManage  bool
defaultAllow bool
}
 
func (s *StaticACL) ACLList() bool {
return s.allowManage
}
 
func (s *StaticACL) ACLModify() bool {
return s.allowManage
}
 
func (s *StaticACL) AgentRead(string) bool {
return s.defaultAllow
}
 
func (s *StaticACL) AgentWrite(string) bool {
return s.defaultAllow
}
 
func (s *StaticACL) EventRead(string) bool {
return s.defaultAllow
}
 
func (s *StaticACL) EventWrite(string) bool {
return s.defaultAllow
}
 
func (s *StaticACL) KeyRead(string) bool {
return s.defaultAllow
}
 
func (s *StaticACL) KeyWrite(string) bool {
return s.defaultAllow
}
 
func (s *StaticACL) KeyWritePrefix(string) bool {
return s.defaultAllow
}
 
func (s *StaticACL) KeyringRead() bool {
return s.defaultAllow
}
 
func (s *StaticACL) KeyringWrite() bool {
return s.defaultAllow
}
 
func (s *StaticACL) NodeRead(string) bool {
return s.defaultAllow
}
 
func (s *StaticACL) NodeWrite(string) bool {
return s.defaultAllow
}
 
func (s *StaticACL) OperatorRead() bool {
return s.defaultAllow
}
 
func (s *StaticACL) OperatorWrite() bool {
return s.defaultAllow
}
 
func (s *StaticACL) PreparedQueryRead(string) bool {
return s.defaultAllow
}
 
func (s *StaticACL) PreparedQueryWrite(string) bool {
return s.defaultAllow
}
 
func (s *StaticACL) ServiceRead(string) bool {
return s.defaultAllow
}
 
func (s *StaticACL) ServiceWrite(string) bool {
return s.defaultAllow
}
 
func (s *StaticACL) SessionRead(string) bool {
return s.defaultAllow
}
 
func (s *StaticACL) SessionWrite(string) bool {
return s.defaultAllow
}
 
func (s *StaticACL) Snapshot() bool {
return s.allowManage
}
 
// AllowAll returns an ACL rule that allows all operations
func AllowAll() ACL {
return allowAll
}
 
// DenyAll returns an ACL rule that denies all operations
func DenyAll() ACL {
return denyAll
}
 
// ManageAll returns an ACL rule that can manage all resources
func ManageAll() ACL {
return manageAll
}
 
// RootACL returns a possible ACL if the ID matches a root policy
func RootACL(id string) ACL {
switch id {
case "allow":
return allowAll
case "deny":
return denyAll
case "manage":
return manageAll
default:
return nil
}
}
 
// PolicyACL is used to wrap a set of ACL policies to provide
// the ACL interface.
type PolicyACL struct {
// parent is used to resolve policy if we have
// no matching rule.
parent ACL
 
// agentRules contains the agent policies
agentRules *radix.Tree
 
// keyRules contains the key policies
keyRules *radix.Tree
 
// nodeRules contains the node policies
nodeRules *radix.Tree
 
// serviceRules contains the service policies
serviceRules *radix.Tree
 
// sessionRules contains the session policies
sessionRules *radix.Tree
 
// eventRules contains the user event policies
eventRules *radix.Tree
 
// preparedQueryRules contains the prepared query policies
preparedQueryRules *radix.Tree
 
// keyringRule contains the keyring policies. The keyring has
// a very simple yes/no without prefix matching, so here we
// don't need to use a radix tree.
keyringRule string
 
// operatorRule contains the operator policies.
operatorRule string
}
 
// New is used to construct a policy based ACL from a set of policies
// and a parent policy to resolve missing cases.
func New(parent ACL, policy *Policy) (*PolicyACL, error) {
p := &PolicyACL{
parent:             parent,
agentRules:         radix.New(),
keyRules:           radix.New(),
nodeRules:          radix.New(),
serviceRules:       radix.New(),
sessionRules:       radix.New(),
eventRules:         radix.New(),
preparedQueryRules: radix.New(),
}
 
// Load the agent policy
for _, ap := range policy.Agents {
p.agentRules.Insert(ap.Node, ap.Policy)
}
 
// Load the key policy
for _, kp := range policy.Keys {
p.keyRules.Insert(kp.Prefix, kp.Policy)
}
 
// Load the node policy
for _, np := range policy.Nodes {
p.nodeRules.Insert(np.Name, np.Policy)
}
 
// Load the service policy
for _, sp := range policy.Services {
p.serviceRules.Insert(sp.Name, sp.Policy)
}
 
// Load the session policy
for _, sp := range policy.Sessions {
p.sessionRules.Insert(sp.Node, sp.Policy)
}
 
// Load the event policy
for _, ep := range policy.Events {
p.eventRules.Insert(ep.Event, ep.Policy)
}
 
// Load the prepared query policy
for _, pq := range policy.PreparedQueries {
p.preparedQueryRules.Insert(pq.Prefix, pq.Policy)
}
 
// Load the keyring policy
p.keyringRule = policy.Keyring
 
// Load the operator policy
p.operatorRule = policy.Operator
 
return p, nil
}
 
// ACLList checks if listing of ACLs is allowed
func (p *PolicyACL) ACLList() bool {
return p.parent.ACLList()
}
 
// ACLModify checks if modification of ACLs is allowed
func (p *PolicyACL) ACLModify() bool {
return p.parent.ACLModify()
}
 
// AgentRead checks for permission to read from agent endpoints for a given
// node.
func (p *PolicyACL) AgentRead(node string) bool {
// Check for an exact rule or catch-all
_, rule, ok := p.agentRules.LongestPrefix(node)
 
if ok {
switch rule {
case PolicyRead, PolicyWrite:
return true
default:
return false
}
}
 
// No matching rule, use the parent.
return p.parent.AgentRead(node)
}
 
// AgentWrite checks for permission to make changes via agent endpoints for a
// given node.
func (p *PolicyACL) AgentWrite(node string) bool {
// Check for an exact rule or catch-all
_, rule, ok := p.agentRules.LongestPrefix(node)
 
if ok {
switch rule {
case PolicyWrite:
return true
default:
return false
}
}
 
// No matching rule, use the parent.
return p.parent.AgentWrite(node)
}
 
// Snapshot checks if taking and restoring snapshots is allowed.
func (p *PolicyACL) Snapshot() bool {
return p.parent.Snapshot()
}
 
// EventRead is used to determine if the policy allows for a
// specific user event to be read.
func (p *PolicyACL) EventRead(name string) bool {
// Longest-prefix match on event names
if _, rule, ok := p.eventRules.LongestPrefix(name); ok {
switch rule {
case PolicyRead, PolicyWrite:
return true
default:
return false
}
}
 
// Nothing matched, use parent
return p.parent.EventRead(name)
}
 
// EventWrite is used to determine if new events can be created
// (fired) by the policy.
func (p *PolicyACL) EventWrite(name string) bool {
// Longest-prefix match event names
if _, rule, ok := p.eventRules.LongestPrefix(name); ok {
return rule == PolicyWrite
}
 
// No match, use parent
return p.parent.EventWrite(name)
}
 
// KeyRead returns if a key is allowed to be read
func (p *PolicyACL) KeyRead(key string) bool {
// Look for a matching rule
_, rule, ok := p.keyRules.LongestPrefix(key)
if ok {
switch rule.(string) {
case PolicyRead, PolicyWrite:
return true
default:
return false
}
}
 
// No matching rule, use the parent.
return p.parent.KeyRead(key)
}
 
// KeyWrite returns if a key is allowed to be written
func (p *PolicyACL) KeyWrite(key string) bool {
// Look for a matching rule
_, rule, ok := p.keyRules.LongestPrefix(key)
if ok {
switch rule.(string) {
case PolicyWrite:
return true
default:
return false
}
}
 
// No matching rule, use the parent.
return p.parent.KeyWrite(key)
}
 
// KeyWritePrefix returns if a prefix is allowed to be written
func (p *PolicyACL) KeyWritePrefix(prefix string) bool {
// Look for a matching rule that denies
_, rule, ok := p.keyRules.LongestPrefix(prefix)
if ok && rule.(string) != PolicyWrite {
return false
}
 
// Look if any of our children have a deny policy
deny := false
p.keyRules.WalkPrefix(prefix, func(path string, rule interface{}) bool {
// We have a rule to prevent a write in a sub-directory!
if rule.(string) != PolicyWrite {
deny = true
return true
}
return false
})
 
// Deny the write if any sub-rules may be violated
if deny {
return false
}
 
// If we had a matching rule, done
if ok {
return true
}
 
// No matching rule, use the parent.
return p.parent.KeyWritePrefix(prefix)
}
 
// KeyringRead is used to determine if the keyring can be
// read by the current ACL token.
func (p *PolicyACL) KeyringRead() bool {
switch p.keyringRule {
case PolicyRead, PolicyWrite:
return true
case PolicyDeny:
return false
default:
return p.parent.KeyringRead()
}
}
 
// KeyringWrite determines if the keyring can be manipulated.
func (p *PolicyACL) KeyringWrite() bool {
if p.keyringRule == PolicyWrite {
return true
}
return p.parent.KeyringWrite()
}
 
// OperatorRead determines if the read-only operator functions are allowed.
func (p *PolicyACL) OperatorRead() bool {
switch p.operatorRule {
case PolicyRead, PolicyWrite:
return true
case PolicyDeny:
return false
default:
return p.parent.OperatorRead()
}
}
 
// NodeRead checks if reading (discovery) of a node is allowed
func (p *PolicyACL) NodeRead(name string) bool {
// Check for an exact rule or catch-all
_, rule, ok := p.nodeRules.LongestPrefix(name)
 
if ok {
switch rule {
case PolicyRead, PolicyWrite:
return true
default:
return false
}
}
 
// No matching rule, use the parent.
return p.parent.NodeRead(name)
}
 
// NodeWrite checks if writing (registering) a node is allowed
func (p *PolicyACL) NodeWrite(name string) bool {
// Check for an exact rule or catch-all
_, rule, ok := p.nodeRules.LongestPrefix(name)
 
if ok {
switch rule {
case PolicyWrite:
return true
default:
return false
}
}
 
// No matching rule, use the parent.
return p.parent.NodeWrite(name)
}
 
// OperatorWrite determines if the state-changing operator functions are
// allowed.
func (p *PolicyACL) OperatorWrite() bool {
if p.operatorRule == PolicyWrite {
return true
}
return p.parent.OperatorWrite()
}
 
// PreparedQueryRead checks if reading (listing) of a prepared query is
// allowed - this isn't execution, just listing its contents.
func (p *PolicyACL) PreparedQueryRead(prefix string) bool {
// Check for an exact rule or catch-all
_, rule, ok := p.preparedQueryRules.LongestPrefix(prefix)
 
if ok {
switch rule {
case PolicyRead, PolicyWrite:
return true
default:
return false
}
}
 
// No matching rule, use the parent.
return p.parent.PreparedQueryRead(prefix)
}
 
// PreparedQueryWrite checks if writing (creating, updating, or deleting) of a
// prepared query is allowed.
func (p *PolicyACL) PreparedQueryWrite(prefix string) bool {
// Check for an exact rule or catch-all
_, rule, ok := p.preparedQueryRules.LongestPrefix(prefix)
 
if ok {
switch rule {
case PolicyWrite:
return true
default:
return false
}
}
 
// No matching rule, use the parent.
return p.parent.PreparedQueryWrite(prefix)
}
 
// ServiceRead checks if reading (discovery) of a service is allowed
func (p *PolicyACL) ServiceRead(name string) bool {
// Check for an exact rule or catch-all
_, rule, ok := p.serviceRules.LongestPrefix(name)
 
if ok {
switch rule {
case PolicyRead, PolicyWrite:
return true
default:
return false
}
}
 
// No matching rule, use the parent.
return p.parent.ServiceRead(name)
}
 
// ServiceWrite checks if writing (registering) a service is allowed
func (p *PolicyACL) ServiceWrite(name string) bool {
// Check for an exact rule or catch-all
_, rule, ok := p.serviceRules.LongestPrefix(name)
 
if ok {
switch rule {
case PolicyWrite:
return true
default:
return false
}
}
 
// No matching rule, use the parent.
return p.parent.ServiceWrite(name)
}
 
// SessionRead checks for permission to read sessions for a given node.
func (p *PolicyACL) SessionRead(node string) bool {
// Check for an exact rule or catch-all
_, rule, ok := p.sessionRules.LongestPrefix(node)
 
if ok {
switch rule {
case PolicyRead, PolicyWrite:
return true
default:
return false
}
}
 
// No matching rule, use the parent.
return p.parent.SessionRead(node)
}
 
// SessionWrite checks for permission to create sessions for a given node.
func (p *PolicyACL) SessionWrite(node string) bool {
// Check for an exact rule or catch-all
_, rule, ok := p.sessionRules.LongestPrefix(node)
 
if ok {
switch rule {
case PolicyWrite:
return true
default:
return false
}
}
 
// No matching rule, use the parent.
return p.parent.SessionWrite(node)
}
 
 
 
 

acl.go的更多相关文章

  1. haproxy利用ACL规则封禁自定义IP地址拒绝访问

    现在有一个需求就是在发版的时候希望除公司IP外的外网访问服务的时候都是拒绝访问的 现在利用haproxy 的acl规则作出限制 errorfile       403 /etc/haproxy/err ...

  2. HAProxy的日志配置以及ACL规则实现负载均衡

    HAProxy配置日志策略 默认情况下,HAProxy是没有配置日志的在centos6.3下默认管理日志的是rsyslog,可以实现UDP日志的接收,将日志写入文件,写入数据库先检测rsyslog是否 ...

  3. CISCO VLAN ACL

    对于cisco VLAN ACL 首先得定义 standard ACL或 extented ACL用于抓取流量 注意这里的抓取流量不是最终的对流量的操作,而是决定什么样的流量用VLAN ACL 来处理 ...

  4. MAC OS X的ACL扩展权限设置

    在WEB开发时,网站是以_www的用户运行的,而我在本地是以liuwencan的用户编辑的.这就带来一个问题:如果所有文件属于liuwencan,那么网站运行需要写文件时就因无权限而失败:如果所有文件 ...

  5. linux ACL权限规划:getfacl,setfacl使用

    ACL即Access Control List 主要的目的是提供传统的owner,group,others的read,write,execute权限之外的具体权限设置,ACL可以针对单一用户.单一文件 ...

  6. ZooKeeper 笔记(5) ACL(Access Control List)访问控制列表

    zk做为分布式架构中的重要中间件,通常会在上面以节点的方式存储一些关键信息,默认情况下,所有应用都可以读写任何节点,在复杂的应用中,这不太安全,ZK通过ACL机制来解决访问权限问题,详见官网文档:ht ...

  7. Centos下ACL(访问控制列表)介绍(转)

    我们知道,在Linux操作系统中,传统的权限管理分是以三种身份(属主.属組以及其它人)搭配三种权限(可读.可写以及可执行),并且搭配三种特殊权限(SUID,SGID,SBIT),来实现对系统的安全保护 ...

  8. ACL权限设置命令setfacl和getfacl命令

    ACL权限设置命令setfacl和getfacl命令 setfacl命令是用来在命令行里设置ACL(访问控制列表).在命令行里,一系列的命令跟随以一系列的文件名. [TOC] 选项 |参数|说明|   ...

  9. ZooKeeper设置ACL权限控制

    ZK的节点有5种操作权限:CREATE.READ.WRITE.DELETE.ADMIN 也就是 增.删.改.查.管理权限,这5种权限简写为crwda(即:每个单词的首字符缩写)注:这5种权限中,del ...

  10. linux 账号管理与ACL权限设定

    此文涉及命令:useradd.usermod.userdel.passwd.chage.setfacl.getfacl.su.sudo.fingr.chfn.chsh.id.groupadd.grou ...

随机推荐

  1. 关于electron的sqlite3报错,需重新编译的问题

    你需要安装sqlite3的所有依赖项,例如vs.python等.或者简单的npm安装命令,它会安装windows下的所有依赖. npm install -g windows-build-tools 然 ...

  2. Android开发学习总结(二)——使用Android Studio搭建Android集成开发环境

    有很长一段时间没有更新博客了,最近实在是太忙了,没有时间去总结,现在终于可以有时间去总结一些Android上面的东西了,很久以前写过这篇关于使用Android Studio搭建Android集成开发环 ...

  3. 面试题-一个for循环输出一个棱形

    用一个for循环输出以下棱形 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ...

  4. 2.3MySQL 自带工具使用介绍

    1.mysql 首先看看“-e, --execute=name”参数,这个参数是告诉mysql,我只要执行“-e”后面的某个命令,而不是要通过mysql 连接登录到MySQL Server 上面.此参 ...

  5. Java学习不走弯路教程(7.Eclipse环境搭建)

    7.Eclipse环境搭建 在前几章,我们熟悉了DOS环境下编译和运行Java程序,对于大规模的程序编写,开发工具是必不可少的.Java的开发工具比较常用的是Eclipse.在接下来的教程中,我们将基 ...

  6. Python小游戏之 - 飞机大战美女 !

    用Python写的"飞机大战美女"小游戏 源代码如下: # coding=utf-8 import os import random import pygame # 用一个常量来存 ...

  7. Vue 仿B站滑动导航

    仿照B站制作的滑动导航功能,进行了部分优化,例如可定制默认选中元素,并将选中元素居中显示,可动态更改数据,可定制回调函数取的下标和选中元素内容,可根据需求制作N级联动 已开发成插件,使用方法与源码请前 ...

  8. ubuntu11.04安装nginx+php+mysql

    先列参考内容,后面我再补充点东西: http://www.4wei.cn/archives/1001436 http://www.gidot.net/blog/article.asp?id=322 上 ...

  9. Android Studio集成Lombok Plugin

    Lombok是Android Studio名列前茅的插件,有啥用,看这个:http://www.blogjava.NET/fancydeepin/archive/2012/07/12/lombok.h ...

  10. 多层嵌套的json数据

    很多时候我们见到的json数据都是多层嵌套的,就像下面这般: {"name":"桔子桑", "sex":"男", , & ...