编程实现基于属性的访问控制模型ABAC 策略管理部分设计思想 创建主体S、客体O、环境E、操作O的集合并写入数据库(文本数据库)。 创建访问控制策略:关于主体属性的规则、客体属性的规则、环境属性的规则的组合,加上操作,如: ct>.其中:p=<SR,OR,ER,op,effect>.其 SR=(sa1^sa2)v sa3 OR =(oa1Voa2)^oa3^o4 ER=(ea1^ea2^ea3)vea4 effect E{permit,deny) 访问控制决策设计思想 ·可信引用监控器启用,接收用户对权限的访问请求,如request=(SA,OA,EA,op), ·进行决策,决策结果为:允许或拒绝。 ·可信引用监控器参照策略库进行决策。 ·关于ABAC的元策略: ·出现冲突时拒绝优先; ·缺乏匹配的策略时默认决策为拒绝。
时间: 2023-06-16 07:07:22 浏览: 92
在实现基于属性的访问控制模型ABAC的策略管理部分时,我们需要设计一个数据库来存储主体S、客体O、环境E、操作O的集合。这个数据库可以使用文本数据库,比如SQLite。
我们可以为每个主体S、客体O、环境E、操作O定义一些属性,例如主体S的属性可以包括角色、部门、所在地等,客体O的属性可以包括类型、所属组织、机密级别等,环境E的属性可以包括时间、地点、网络连接状态等。
对于访问控制策略的设计,我们需要定义关于主体属性的规则、客体属性的规则、环境属性的规则的组合,加上操作。我们可以使用如下格式来表示访问控制策略:
p = <SR, OR, ER, op, effect>
其中,SR表示主体属性规则,OR表示客体属性规则,ER表示环境属性规则,op表示操作,effect表示访问控制效果,可以是permit或deny。
在访问控制决策的设计中,我们可以使用可信引用监控器来接收用户对权限的访问请求,例如request = (SA, OA, EA, op)。然后,可信引用监控器可以参照策略库进行决策,决策结果为允许或拒绝。
在ABAC的元策略中,我们可以定义一些规则来处理特殊情况。例如,当出现冲突时,我们可以定义拒绝优先。当缺乏匹配的策略时,默认决策为拒绝。这些规则可以帮助我们更好地管理访问控制策略,并确保系统的安全性。
相关问题
python编程题:编程实现基于属性的访问控制模型ABAC •策略管理部分设计思想 •创建主休S、客体O、环境E、操作O的集合并写人数据库 (文本数据库)。 ,创建访问控制策略:关于主体属性的规则、客体属性的规则、环境属性的规则的组合,加上操作
ABAC(Attribute-Based Access Control)是一种基于属性的访问控制模型,它通过对主体、客体、环境等各种属性进行定义和管理,从而实现对系统资源的访问控制。下面是一个Python实现的ABAC访问控制模型的例子。
首先,我们需要定义一些类来表示主体、客体、环境、操作等概念:
```python
class Subject:
def __init__(self, attributes):
self.attributes = attributes
class Object:
def __init__(self, attributes):
self.attributes = attributes
class Environment:
def __init__(self, attributes):
self.attributes = attributes
class Action:
def __init__(self, name):
self.name = name
```
然后,我们需要定义一个策略管理器,它负责从数据库中读取策略、匹配策略等操作:
```python
class PolicyManager:
def __init__(self, db):
self.db = db
def get_policy(self, subject, obj, environment):
policies = self.db.get_policies()
for policy in policies:
if self.match_policy(policy, subject, obj, environment):
return policy
def match_policy(self, policy, subject, obj, environment):
subject_matched = all(getattr(subject, k) == v for k, v in policy.subject.items())
obj_matched = all(getattr(obj, k) == v for k, v in policy.obj.items())
env_matched = all(getattr(environment, k) == v for k, v in policy.environment.items())
return subject_matched and obj_matched and env_matched
```
接下来,我们需要定义一个数据库类,它负责存储主体、客体、环境和策略等信息:
```python
class Database:
def __init__(self):
self.subjects = {}
self.objects = {}
self.environments = {}
self.policies = []
def add_subject(self, name, attributes):
self.subjects[name] = Subject(attributes)
def get_subject(self, name):
return self.subjects.get(name)
def add_object(self, name, attributes):
self.objects[name] = Object(attributes)
def get_object(self, name):
return self.objects.get(name)
def add_environment(self, name, attributes):
self.environments[name] = Environment(attributes)
def get_environment(self, name):
return self.environments.get(name)
def add_policy(self, policy):
self.policies.append(policy)
def get_policies(self):
return self.policies
```
最后,我们需要定义一个访问控制策略类,它由主体属性、客体属性、环境属性和操作组成:
```python
class Policy:
def __init__(self, subject, obj, environment, action):
self.subject = subject
self.obj = obj
self.environment = environment
self.action = action
```
现在,我们可以使用上面定义的类来创建一个ABAC访问控制模型。首先,我们创建一个数据库,并添加一些主体、客体、环境和策略信息:
```python
db = Database()
# Add subjects
db.add_subject('alice', {'department': 'sales', 'role': 'manager'})
db.add_subject('bob', {'department': 'hr', 'role': 'employee'})
# Add objects
db.add_object('document1', {'department': 'sales', 'level': 'internal'})
db.add_object('document2', {'department': 'hr', 'level': 'confidential'})
# Add environments
db.add_environment('location', {'country': 'us', 'city': 'new york'})
db.add_environment('time', {'hour': 10, 'day': 'monday'})
# Add policies
policy1 = Policy({'department': 'sales'}, {'level': 'internal'}, {}, Action('read'))
policy2 = Policy({'role': 'manager'}, {'level': 'internal'}, {}, Action('write'))
policy3 = Policy({'department': 'hr'}, {'level': 'confidential'}, {}, Action('read'))
policy4 = Policy({'department': 'hr'}, {'level': 'confidential'}, {'country': 'us'}, Action('read'))
policy5 = Policy({'department': 'sales'}, {'level': 'internal'}, {'city': 'new york'}, Action('read'))
db.add_policy(policy1)
db.add_policy(policy2)
db.add_policy(policy3)
db.add_policy(policy4)
db.add_policy(policy5)
```
然后,我们可以创建一个策略管理器,并使用它来测试访问控制模型:
```python
policy_manager = PolicyManager(db)
# Test access
alice = db.get_subject('alice')
document1 = db.get_object('document1')
location = db.get_environment('location')
can_read = policy_manager.get_policy(alice, document1, location).action.name == 'read'
print(can_read) # True
bob = db.get_subject('bob')
document2 = db.get_object('document2')
can_read = policy_manager.get_policy(bob, document2, location).action.name == 'read'
print(can_read) # False
time = db.get_environment('time')
can_read = policy_manager.get_policy(alice, document1, time).action.name == 'read'
print(can_read) # False
can_write = policy_manager.get_policy(alice, document1, location).action.name == 'write'
print(can_write) # True
```
上面的代码中,我们首先创建了一个策略管理器,并使用它来测试不同主体、客体和环境对不同操作的访问权限。根据定义的访问控制策略,我们可以看到访问控制模型能够正确地限制资源的访问权限。
编程实现基于属性点访问控制模型ABAC,条件:创建主体S,客体O,环境E,操作O的集合并写入数据库,创建访问控制策略,可信应用监控器启用,接收用户对权限的访问请求,进行决策,决策结果为拒绝或运行,出现冲突时拒绝优先,缺乏匹配的策略时默认决策为拒绝
ABAC模型(Attribute-Based Access Control)是一种基于属性的访问控制模型,可以根据主体、客体和环境的属性来进行访问控制决策。下面是一个基于属性点访问控制模型ABAC的Python实现,包括创建主体S、客体O、环境E,操作O的集合并写入数据库,创建访问控制策略,可信应用监控器启用,接收用户对权限的访问请求,进行决策等功能。
首先,我们需要安装一些Python库,包括flask、sqlalchemy和pyyaml,我们可以使用pip install命令进行安装。
```python
pip install flask sqlalchemy pyyaml
```
接下来,我们创建一个名为abac的Python包,并在其中创建如下几个文件:
- database.py:用于创建数据库和表格,以及向表格中添加数据。
- model.py:用于定义主体、客体、环境和操作的模型,以及定义访问策略和请求的模型。
- policy.py:用于创建和管理访问策略。
- decision.py:用于进行访问控制决策,并返回决策结果。
database.py:
```python
from flask_sqlalchemy import SQLAlchemy
from model import Subject, Object, Environment, Operation
db = SQLAlchemy()
def create_database():
db.create_all()
def add_subject(name, attributes):
subject = Subject(name=name, attributes=attributes)
db.session.add(subject)
db.session.commit()
def add_object(name, attributes):
object = Object(name=name, attributes=attributes)
db.session.add(object)
db.session.commit()
def add_environment(name, attributes):
environment = Environment(name=name, attributes=attributes)
db.session.add(environment)
db.session.commit()
def add_operation(name, attributes):
operation = Operation(name=name, attributes=attributes)
db.session.add(operation)
db.session.commit()
```
model.py:
```python
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import Table, Column, Integer, ForeignKey
from sqlalchemy.orm import relationship
db = SQLAlchemy()
class Subject(db.Model):
__tablename__ = 'subjects'
id = Column(Integer, primary_key=True)
name = Column(String(50))
attributes = Column(String(1000))
class Object(db.Model):
__tablename__ = 'objects'
id = Column(Integer, primary_key=True)
name = Column(String(50))
attributes = Column(String(1000))
class Environment(db.Model):
__tablename__ = 'environments'
id = Column(Integer, primary_key=True)
name = Column(String(50))
attributes = Column(String(1000))
class Operation(db.Model):
__tablename__ = 'operations'
id = Column(Integer, primary_key=True)
name = Column(String(50))
attributes = Column(String(1000))
class Policy(db.Model):
__tablename__ = 'policies'
id = Column(Integer, primary_key=True)
subject_id = Column(Integer, ForeignKey('subjects.id'))
object_id = Column(Integer, ForeignKey('objects.id'))
environment_id = Column(Integer, ForeignKey('environments.id'))
operation_id = Column(Integer, ForeignKey('operations.id'))
effect = Column(String(10))
subject = relationship("Subject")
object = relationship("Object")
environment = relationship("Environment")
operation = relationship("Operation")
class Request():
def __init__(self, subject_attributes, object_attributes, environment_attributes, operation_attributes):
self.subject_attributes = subject_attributes
self.object_attributes = object_attributes
self.environment_attributes = environment_attributes
self.operation_attributes = operation_attributes
```
policy.py:
```python
import yaml
from model import Policy, Request, Subject, Object, Environment, Operation
from sqlalchemy import or_
def create_policy(subject_name, object_name, environment_name, operation_name, effect):
policy = Policy(subject=Subject.query.filter_by(name=subject_name).first(),
object=Object.query.filter_by(name=object_name).first(),
environment=Environment.query.filter_by(name=environment_name).first(),
operation=Operation.query.filter_by(name=operation_name).first(),
effect=effect)
db.session.add(policy)
db.session.commit()
def load_policies(file_path):
with open(file_path, 'r') as f:
policies = yaml.load(f, Loader=yaml.FullLoader)
for policy in policies:
create_policy(policy['subject'], policy['object'], policy['environment'], policy['operation'], policy['effect'])
def check_request(request):
policies = Policy.query.filter_by(subject_id=request.subject_attributes['id'],
object_id=request.object_attributes['id'],
operation_id=request.operation_attributes['id']).all()
for policy in policies:
if eval(policy.environment.attributes + '\n' + policy.subject.attributes + '\n' + policy.object.attributes):
return policy.effect
return 'deny'
```
decision.py:
```python
from model import Request
from policy import check_request
def access_control(subject_attributes, object_attributes, environment_attributes, operation_attributes):
request = Request(subject_attributes, object_attributes, environment_attributes, operation_attributes)
return check_request(request)
```
最后,我们编写一个简单的Flask应用程序,用于创建数据库、添加主体、客体、环境和操作,以及对访问请求进行访问控制决策。在启动应用程序之前,请确保已经创建了数据库,并且已经添加了主体、客体、环境和操作。
```python
from flask import Flask, request, jsonify
from database import create_database, add_subject, add_object, add_environment, add_operation
from model import Subject, Object, Environment, Operation
from decision import access_control
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///abac.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
create_database()
add_subject('Alice', '{"role": "admin", "department": "finance"}')
add_subject('Bob', '{"role": "user", "department": "sales"}')
add_object('file', '{"type": "confidential", "owner": "Alice"}')
add_object('database', '{"type": "public", "owner": "Bob"}')
add_environment('time', '{"hour": 9}')
add_operation('read', '{"type": "read"}')
add_operation('write', '{"type": "write"}')
@app.route('/access_control', methods=['POST'])
def access_control_endpoint():
subject_attributes = request.json['subject_attributes']
object_attributes = request.json['object_attributes']
environment_attributes = request.json['environment_attributes']
operation_attributes = request.json['operation_attributes']
result = access_control(subject_attributes, object_attributes, environment_attributes, operation_attributes)
return jsonify({'result': result})
if __name__ == '__main__':
app.run(debug=True)
```
现在,我们可以使用POST请求调用/access_control端点,并传递主体、客体、环境和操作的属性,以获取访问控制决策结果。
阅读全文