Skip to content

Commit

Permalink
feat(acl): add acl table (#109)
Browse files Browse the repository at this point in the history
* feat(acl): add acl table

* fix(acl): add data migration

* fix(java): try another package
  • Loading branch information
philloooo authored Apr 23, 2018
1 parent 695bd24 commit f787434
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ python:
addons:
apt:
packages:
- oracle-java8-installer
- oracle-java8-set-default

sudo: false

Expand Down
10 changes: 10 additions & 0 deletions indexd/index/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ def get_index():

urls = flask.request.args.getlist('url')

acl = flask.request.args.getlist('url')

file_name = flask.request.args.get('file_name')

version = flask.request.args.get('version')
Expand All @@ -92,6 +94,7 @@ def get_index():
file_name=file_name,
version=version,
urls=urls,
acl=acl,
hashes=hashes,
metadata=metadata,
)
Expand All @@ -104,6 +107,7 @@ def get_index():
'file_name': file_name,
'version': version,
'urls': urls,
'acl': acl,
'hashes': hashes,
'metadata': metadata,
}
Expand Down Expand Up @@ -188,6 +192,7 @@ def post_index_record():
form = flask.request.json['form']
size = flask.request.json['size']
urls = flask.request.json['urls']
acl = flask.request.json.get('acl', [])

hashes = flask.request.json['hashes']
file_name = flask.request.json.get('file_name')
Expand All @@ -203,6 +208,7 @@ def post_index_record():
metadata=metadata,
version=version,
urls=urls,
acl=acl,
hashes=hashes,
baseid=baseid,
)
Expand Down Expand Up @@ -231,6 +237,7 @@ def put_index_record(record):
file_name = flask.request.json.get('file_name')
version = flask.request.json.get('version')
urls = flask.request.json.get('urls')
acl = flask.request.json.get('acl')
metadata = flask.request.json.get('metadata')

did, baseid, rev = blueprint.index_driver.update(
Expand All @@ -239,6 +246,7 @@ def put_index_record(record):
file_name=file_name,
version=version,
urls=urls,
acl=acl,
metadata=metadata,
)

Expand Down Expand Up @@ -281,6 +289,7 @@ def add_index_record_version(record):
form = flask.request.json['form']
size = flask.request.json['size']
urls = flask.request.json['urls']
acl = flask.request.json.get('acl', [])
hashes = flask.request.json['hashes']
file_name = flask.request.json.get('file_name', None)
metadata = flask.request.json.get('metadata', None)
Expand All @@ -292,6 +301,7 @@ def add_index_record_version(record):
new_did=new_did,
size=size,
urls=urls,
acl=acl,
file_name=file_name,
metadata=metadata,
version=version,
Expand Down
100 changes: 90 additions & 10 deletions indexd/index/drivers/alchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ class IndexRecord(Base):
cascade='all, delete-orphan',
)

acl = relationship(
'IndexRecordACE',
backref='index_record',
cascade='all, delete-orphan',
)

hashes = relationship(
'IndexRecordHash',
backref='index_record',
Expand Down Expand Up @@ -92,7 +98,25 @@ class IndexRecordUrl(Base):
backref='index_record_url',
cascade='all, delete-orphan',
)
Index('index_record_url_idx', 'did')
__table_args__ = (
Index('index_record_url_idx', 'did'),
)


class IndexRecordACE(Base):
'''
index record access control entry representation.
'''

__tablename__ = 'index_record_ace'

did = Column(String, ForeignKey('index_record.did'), primary_key=True)
# access control entry
ace = Column(String, primary_key=True)

__table_args__ = (
Index('index_record_ace_idx', 'did'),
)


class IndexRecordMetadata(Base):
Expand All @@ -104,9 +128,9 @@ class IndexRecordMetadata(Base):
key = Column(String, primary_key=True)
did = Column(String, ForeignKey('index_record.did'), primary_key=True)
value = Column(String)
Index('index_record_metadata_idx', 'did')
Index('__did_key_idx', 'did', 'key')

__table_args__ = (
Index('index_record_metadata_idx', 'did'),
)

class IndexRecordUrlMetadata(Base):
"""
Expand All @@ -121,9 +145,9 @@ class IndexRecordUrlMetadata(Base):
__table_args__ = (
ForeignKeyConstraint(['did', 'url'],
['index_record_url.did', 'index_record_url.url']),
Index('index_record_url_metadata_idx', 'did'),
)
Index('index_record_url_metadata_idx', 'did')
Index('__did_url_key_idx', 'did', 'url', 'key')



class IndexRecordHash(Base):
Expand All @@ -135,7 +159,9 @@ class IndexRecordHash(Base):
did = Column(String, ForeignKey('index_record.did'), primary_key=True)
hash_type = Column(String, primary_key=True)
hash_value = Column(String)
Index('index_record_hash_idx', 'did')
__table_args__ = (
Index('index_record_hash_idx', 'did'),
)


class SQLAlchemyIndexDriver(IndexDriverABC):
Expand Down Expand Up @@ -196,6 +222,7 @@ def ids(self,
start=None,
size=None,
urls=None,
acl=None,
hashes=None,
file_name=None,
version=None,
Expand Down Expand Up @@ -223,6 +250,11 @@ def ids(self,
for u in urls:
query = query.filter(IndexRecordUrl.url == u)

if acl is not None and acl:
query = query.join(IndexRecord.acl)
for u in acl:
query = query.filter(IndexRecordACE.ace == u)

if hashes is not None and hashes:
for h, v in hashes.items():
sub = session.query(IndexRecordHash.did)
Expand Down Expand Up @@ -285,15 +317,18 @@ def add(self,
metadata=None,
version=None,
urls=None,
acl=None,
hashes=None,
baseid=None):
"""
Creates a new record given size, urls, hashes, metadata, file name and version
Creates a new record given size, urls, acl, hashes, metadata, file name and version
if did is provided, update the new record with the did otherwise create it
"""

if urls is None:
urls = []
if acl is None:
acl = []
if hashes is None:
hashes = {}
if metadata is None:
Expand Down Expand Up @@ -323,6 +358,11 @@ def add(self,
url=url,
) for url in urls]

record.acl = [IndexRecordACE(
did=record.did,
ace=ace,
) for ace in acl]

record.hashes = [IndexRecordHash(
did=record.did,
hash_type=h,
Expand Down Expand Up @@ -374,6 +414,7 @@ def get(self, did):
version = record.version

urls = [u.url for u in record.urls]
acl = [u.ace for u in record.acl]
hashes = {h.hash_type: h.hash_value for h in record.hashes}
metadata = {m.key: m.value for m in record.index_metadata}

Expand All @@ -388,6 +429,7 @@ def get(self, did):
'file_name': file_name,
'version': version,
'urls': urls,
'acl': acl,
'hashes': hashes,
'metadata': metadata,
'form': form,
Expand All @@ -398,7 +440,7 @@ def get(self, did):
return ret

def update(self,
did, rev, urls=None, file_name=None,
did, rev, urls=None, acl=None, file_name=None,
version=None, metadata=None):
"""
Updates an existing record with new values.
Expand Down Expand Up @@ -426,6 +468,15 @@ def update(self,
url=url
) for url in urls]

if acl is not None:
for ace in record.acl:
session.delete(ace)

record.acl = [IndexRecordACE(
did=record.did,
ace=ace
) for ace in acl]

if metadata is not None:
for md_record in record.index_metadata:
session.delete(md_record)
Expand Down Expand Up @@ -477,12 +528,15 @@ def add_version(self,
metadata=None,
version=None,
urls=None,
acl=None,
hashes=None):
"""
Add a record version given did
"""
if urls is None:
urls = []
if acl is None:
acl = []
if hashes is None:
hashes = {}
if metadata is None:
Expand Down Expand Up @@ -514,6 +568,11 @@ def add_version(self,
url=url,
) for url in urls]

record.acl = [IndexRecordACE(
did=record.did,
ace=ace,
) for ace in acl]

record.hashes = [IndexRecordHash(
did=record.did,
hash_type=h,
Expand Down Expand Up @@ -568,6 +627,7 @@ def get_all_versions(self, did):
file_name = record.file_name
version = record.version
urls = [u.url for u in record.urls]
acl = [u.ace for u in record.acl]
hashes = {h.hash_type: h.hash_value for h in record.hashes}
metadata = {m.key: m.value for m in record.index_metadata}

Expand All @@ -583,6 +643,7 @@ def get_all_versions(self, did):
'metadata': metadata,
'version': version,
'urls': urls,
'acl': acl,
'hashes': hashes,
'form': form,
'created_date': created_date,
Expand Down Expand Up @@ -628,6 +689,7 @@ def get_latest_version(self, did):
version = record.version

urls = [u.url for u in record.urls]
acl = [u.ace for u in record.acl]
hashes = {h.hash_type: h.hash_value for h in record.hashes}

created_date = record.created_date.isoformat()
Expand All @@ -642,6 +704,7 @@ def get_latest_version(self, did):
'metadata': metadata,
'version': version,
'urls': urls,
'acl': acl,
'hashes': hashes,
'form': form,
'created_date': created_date,
Expand Down Expand Up @@ -792,7 +855,24 @@ def migrate_5(session, **kwargs):
"CREATE INDEX {tb}_idx ON {tb} ( did )"
.format(tb=IndexRecordUrlMetadata.__tablename__))


def migrate_6(session, **kwargs):
existing_acls = (
session.query(IndexRecordMetadata).filter_by(key='acl').yield_per(1000)
)
for metadata in existing_acls:
acl = metadata.value.split(',')
for ace in acl:
entry = IndexRecordACE(
did=metadata.did,
ace=ace)
session.add(entry)
session.delete(metadata)


# ordered schema migration functions that the index should correspond to
# CURRENT_SCHEMA_VERSION - 1 when it's written
SCHEMA_MIGRATION_FUNCTIONS = [migrate_1, migrate_2, migrate_3, migrate_4, migrate_5]
SCHEMA_MIGRATION_FUNCTIONS = [
migrate_1, migrate_2, migrate_3, migrate_4, migrate_5,
migrate_6]
CURRENT_SCHEMA_VERSION = len(SCHEMA_MIGRATION_FUNCTIONS)
12 changes: 12 additions & 0 deletions indexd/index/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
"type": "string"
}
},
"acl": {
"type": "array",
"items": {
"type": "string"
}
},
"did": {
"type": "string",
"pattern": "^.*[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$"
Expand Down Expand Up @@ -124,6 +130,12 @@
"type": "string"
}
},
"acl": {
"type": "array",
"items": {
"type": "string"
}
},
"file_name": {
"type": "string"
},
Expand Down
Loading

0 comments on commit f787434

Please sign in to comment.