mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00
Merge branch 'master' into bug/1670/asset-language-api-fix
This commit is contained in:
commit
8310b04b17
@ -50,6 +50,11 @@ def drop_database(conn, dbname):
|
||||
def create_bigchain_secondary_index(conn, dbname):
|
||||
logger.info('Create `bigchain` secondary index.')
|
||||
|
||||
# secondary index on block id which should be unique
|
||||
conn.conn[dbname]['bigchain'].create_index('id',
|
||||
name='block_id',
|
||||
unique=True)
|
||||
|
||||
# to order blocks by timestamp
|
||||
conn.conn[dbname]['bigchain'].create_index([('block.timestamp',
|
||||
ASCENDING)],
|
||||
|
@ -8,9 +8,10 @@ from bigchaindb.common.exceptions import (InvalidHash, InvalidSignature,
|
||||
SybilError,
|
||||
DuplicateTransaction)
|
||||
from bigchaindb.common.transaction import Transaction
|
||||
from bigchaindb.common.utils import gen_timestamp, serialize
|
||||
from bigchaindb.common.utils import (gen_timestamp, serialize,
|
||||
validate_txn_obj, validate_key)
|
||||
from bigchaindb.common.schema import validate_transaction_schema
|
||||
from bigchaindb.backend.schema import validate_if_exists_asset_language
|
||||
from bigchaindb.backend.schema import validate_if_exists_language
|
||||
|
||||
|
||||
class Transaction(Transaction):
|
||||
@ -85,7 +86,9 @@ class Transaction(Transaction):
|
||||
@classmethod
|
||||
def from_dict(cls, tx_body):
|
||||
validate_transaction_schema(tx_body)
|
||||
validate_if_exists_asset_language(tx_body)
|
||||
validate_txn_obj('asset', tx_body['asset'], 'data', validate_key)
|
||||
validate_txn_obj('metadata', tx_body, 'metadata', validate_key)
|
||||
validate_if_exists_language(tx_body['asset'], 'data')
|
||||
return super().from_dict(tx_body)
|
||||
|
||||
@classmethod
|
||||
|
@ -34,7 +34,9 @@ from recommonmark.parser import CommonMarkParser
|
||||
# ones.
|
||||
import sphinx_rtd_theme
|
||||
|
||||
extensions = []
|
||||
extensions = [
|
||||
'sphinx.ext.autosectionlabel',
|
||||
]
|
||||
|
||||
# Add any paths that contain templates here, relative to this directory.
|
||||
templates_path = ['_templates']
|
||||
|
@ -88,5 +88,6 @@ More About BigchainDB
|
||||
assets
|
||||
smart-contracts
|
||||
transaction-concepts
|
||||
permissions
|
||||
timestamps
|
||||
Data Models <https://docs.bigchaindb.com/projects/server/en/latest/data-models/index.html>
|
||||
|
74
docs/root/source/permissions.rst
Normal file
74
docs/root/source/permissions.rst
Normal file
@ -0,0 +1,74 @@
|
||||
Permissions in BigchainDB
|
||||
-------------------------
|
||||
|
||||
BigchainDB lets users control what other users can do, to some extent. That ability resembles "permissions" in the \*nix world, "privileges" in the SQL world, and "access control" in the security world.
|
||||
|
||||
|
||||
Permission to Spend/Transfer an Output
|
||||
======================================
|
||||
|
||||
In BigchainDB, every output has an associated condition (crypto-condition).
|
||||
|
||||
To spend/transfer an unspent output, a user (or group of users) must fulfill the condition. Another way to say that is that only certain users have permission to spend the output. The simplest condition is of the form, "Only someone with the private key corresponding to this public key can spend this output." Much more elaborate conditions are possible, e.g. "To spend this output, …"
|
||||
|
||||
- "…anyone in the Accounting Group can sign."
|
||||
- "…three of these four people must sign."
|
||||
- "…either Bob must sign, or both Tom and Sylvia must sign."
|
||||
|
||||
For details, see `the documentation about conditions in BigchainDB <https://docs.bigchaindb.com/projects/server/en/latest/data-models/conditions.html>`_.
|
||||
|
||||
Once an output has been spent, it can't be spent again: *nobody* has permission to do that. That is, BigchainDB doesn't permit anyone to "double spend" an output.
|
||||
|
||||
|
||||
Write Permissions
|
||||
=================
|
||||
|
||||
When someone builds a TRANSFER transaction, they can put an arbitrary JSON object in the ``metadata`` field (within reason; real BigchainDB networks put a limit on the size of transactions). That is, they can write just about anything they want in a TRANSFER transaction.
|
||||
|
||||
Does that mean there are no "write permissions" in BigchainDB? Not at all!
|
||||
|
||||
A TRANSFER transaction will only be valid (allowed) if its inputs fulfill some previous outputs. The conditions on those outputs will control who can build valid TRANSFER transactions. In other words, one can interpret the condition on an output as giving "write permissions" to certain users to write something into the history of the associated asset.
|
||||
|
||||
As a concrete example, you could use BigchainDB to write a public journal where only you have write permissions. Here's how: First you'd build a CREATE transaction with the ``asset.data`` being something like ``{"title": "The Journal of John Doe"}``, with one output. That output would have an amount 1 and a condition that only you (who has your private key) can spend that output.
|
||||
Each time you want to append something to your journal, you'd build a new TRANSFER transaction with your latest entry in the ``metadata`` field, e.g.
|
||||
|
||||
.. code-block:: json
|
||||
|
||||
{"timestamp": "1508319582",
|
||||
"entry": "I visited Marmot Lake with Jane."}
|
||||
|
||||
The TRANSFER transaction would have one output. That output would have an amount 1 and a condition that only you (who has your private key) can spend that output. And so on. Only you would be able to append to the history of that asset (your journal).
|
||||
|
||||
The same technique could be used for scientific notebooks, supply-chain records, government meeting minutes, and so on.
|
||||
|
||||
You could do more elaborate things too. As one example, each time someone writes a TRANSFER transaction, they give *someone else* permission to spend it, setting up a sort of writers-relay or chain letter.
|
||||
|
||||
.. note::
|
||||
|
||||
Anyone can write any JSON (again, within reason) in the ``asset.data`` field of a CREATE transaction. They don't need permission.
|
||||
|
||||
|
||||
Read Permissions
|
||||
================
|
||||
|
||||
All the data stored in a BigchainDB network can be read by anyone with access to that network. One *can* store encrypted data, but if the decryption key ever leaks out, then the encrypted data can be read, decrypted, and leak out too. (Deleting the encrypted data is :doc:`not an option <immutable>`.)
|
||||
|
||||
The permission to read some specific information (e.g. a music file) can be thought of as an *asset*. (In many countries, that permission or "right" is a kind of intellectual property.)
|
||||
BigchainDB can be used to register that asset and transfer it from owner to owner.
|
||||
Today, BigchainDB does not have a way to restrict read access of data stored in a BigchainDB network, but many third-party services do offer that (e.g. Google Docs, Dropbox).
|
||||
In principle, a third party service could ask a BigchainDB network to determine if a particular user has permission to read some particular data. Indeed they could use BigchainDB to keep track of *all* the rights a user has for some data (not just the right to read it).
|
||||
That third party could also use BigchainDB to store audit logs, i.e. records of every read, write or other operation on stored data.
|
||||
|
||||
BigchainDB can be used in other ways to help parties exchange private data:
|
||||
|
||||
- It can be used to publicly disclose the *availability* of some private data (stored elsewhere). For example, there might be a description of the data and a price.
|
||||
- It can be used to record the TLS handshakes which two parties sent to each other to establish an encrypted and authenticated TLS connection, which they could use to exchange private data with each other. (The stored handshake information wouldn't be enough, by itself, to decrypt the data.) It would be a "proof of TLS handshake."
|
||||
- See the BigchainDB `Privacy Protocols repository <https://github.com/bigchaindb/privacy-protocols>`_ for more techniques.
|
||||
|
||||
|
||||
Role-Based Access Control (RBAC)
|
||||
================================
|
||||
|
||||
In September 2017, we published a `blog post about how one can define an RBAC sub-system on top of BigchainDB <https://blog.bigchaindb.com/role-based-access-control-for-bigchaindb-assets-b7cada491997>`_.
|
||||
At the time of writing (October 2017), doing so required the use of a plugin, so it's not possible using standard BigchainDB (which is what's available on `IPDB <https://ipdb.io/>`_). That may change in the future.
|
||||
If you're interested, `contact BigchainDB <https://www.bigchaindb.com/contact/>`_.
|
@ -299,12 +299,13 @@ def test_count_blocks(signed_create_tx):
|
||||
from bigchaindb.models import Block
|
||||
conn = connect()
|
||||
|
||||
assert query.count_blocks(conn) == 0
|
||||
|
||||
# create and insert some blocks
|
||||
block = Block(transactions=[signed_create_tx])
|
||||
conn.db.bigchain.insert_one(block.to_dict())
|
||||
conn.db.bigchain.insert_one(block.to_dict())
|
||||
|
||||
assert query.count_blocks(conn) == 2
|
||||
assert query.count_blocks(conn) == 1
|
||||
|
||||
|
||||
def test_count_backlog(signed_create_tx):
|
||||
|
@ -22,8 +22,8 @@ def test_init_creates_db_tables_and_indexes():
|
||||
'votes']
|
||||
|
||||
indexes = conn.conn[dbname]['bigchain'].index_information().keys()
|
||||
assert sorted(indexes) == ['_id_', 'asset_id', 'block_timestamp', 'inputs',
|
||||
'outputs', 'transaction_id']
|
||||
assert sorted(indexes) == ['_id_', 'asset_id', 'block_id', 'block_timestamp',
|
||||
'inputs', 'outputs', 'transaction_id']
|
||||
|
||||
indexes = conn.conn[dbname]['backlog'].index_information().keys()
|
||||
assert sorted(indexes) == ['_id_', 'assignee__transaction_timestamp',
|
||||
@ -86,8 +86,8 @@ def test_create_secondary_indexes():
|
||||
|
||||
# Bigchain table
|
||||
indexes = conn.conn[dbname]['bigchain'].index_information().keys()
|
||||
assert sorted(indexes) == ['_id_', 'asset_id', 'block_timestamp', 'inputs',
|
||||
'outputs', 'transaction_id']
|
||||
assert sorted(indexes) == ['_id_', 'asset_id', 'block_id', 'block_timestamp',
|
||||
'inputs', 'outputs', 'transaction_id']
|
||||
|
||||
# Backlog table
|
||||
indexes = conn.conn[dbname]['backlog'].index_information().keys()
|
||||
|
@ -47,10 +47,11 @@ def test_post_create_transaction_endpoint(b, client):
|
||||
assert res.json['outputs'][0]['public_keys'][0] == user_pub
|
||||
|
||||
|
||||
@pytest.mark.parametrize("nested", [False, True])
|
||||
@pytest.mark.parametrize("language,expected_status_code", [
|
||||
('danish', 202), ('dutch', 202), ('english', 202), ('finnish', 202),
|
||||
('french', 202), ('german', 202), ('hungarian', 202), ('italian', 202),
|
||||
('norwegian', 202), ('portuguese', 202), ('romanian', 202),
|
||||
('norwegian', 202), ('portuguese', 202), ('romanian', 202), ('none', 202),
|
||||
('russian', 202), ('spanish', 202), ('swedish', 202), ('turkish', 202),
|
||||
('da', 202), ('nl', 202), ('en', 202), ('fi', 202), ('fr', 202),
|
||||
('de', 202), ('hu', 202), ('it', 202), ('nb', 202), ('pt', 202),
|
||||
@ -59,24 +60,66 @@ def test_post_create_transaction_endpoint(b, client):
|
||||
])
|
||||
@pytest.mark.language
|
||||
@pytest.mark.bdb
|
||||
def test_post_create_transaction_with_language(b, client, language, expected_status_code):
|
||||
def test_post_create_transaction_with_language(b, client, nested, language,
|
||||
expected_status_code):
|
||||
from bigchaindb.models import Transaction
|
||||
from bigchaindb.backend.mongodb.connection import MongoDBConnection
|
||||
|
||||
if isinstance(b.connection, MongoDBConnection):
|
||||
user_priv, user_pub = crypto.generate_key_pair()
|
||||
lang_obj = {'language': language}
|
||||
|
||||
if nested:
|
||||
asset = {'root': lang_obj}
|
||||
else:
|
||||
asset = lang_obj
|
||||
|
||||
tx = Transaction.create([user_pub], [([user_pub], 1)],
|
||||
asset={'language': language})
|
||||
asset=asset)
|
||||
tx = tx.sign([user_priv])
|
||||
res = client.post(TX_ENDPOINT, data=json.dumps(tx.to_dict()))
|
||||
assert res.status_code == expected_status_code
|
||||
if res.status_code == 400:
|
||||
expected_error_message = (
|
||||
"Invalid transaction (ValidationError): MongoDB does not support "
|
||||
"text search for the language \"{}\". If you do not understand this "
|
||||
"error message then please rename key/field \"language\" to something "
|
||||
"else like \"lang\".").format(language)
|
||||
'Invalid transaction (ValidationError): MongoDB does not support '
|
||||
'text search for the language "{}". If you do not understand this '
|
||||
'error message then please rename key/field "language" to something '
|
||||
'else like "lang".').format(language)
|
||||
assert res.json['message'] == expected_error_message
|
||||
|
||||
|
||||
@pytest.mark.parametrize("field", ['asset', 'metadata'])
|
||||
@pytest.mark.parametrize("value,err_key,expected_status_code", [
|
||||
({'bad.key': 'v'}, 'bad.key', 400),
|
||||
({'$bad.key': 'v'}, '$bad.key', 400),
|
||||
({'$badkey': 'v'}, '$badkey', 400),
|
||||
({'bad\x00key': 'v'}, 'bad\x00key', 400),
|
||||
({'good_key': {'bad.key': 'v'}}, 'bad.key', 400),
|
||||
({'good_key': 'v'}, 'good_key', 202)
|
||||
])
|
||||
@pytest.mark.bdb
|
||||
def test_post_create_transaction_with_invalid_key(b, client, field, value,
|
||||
err_key, expected_status_code):
|
||||
from bigchaindb.models import Transaction
|
||||
from bigchaindb.backend.mongodb.connection import MongoDBConnection
|
||||
user_priv, user_pub = crypto.generate_key_pair()
|
||||
|
||||
if isinstance(b.connection, MongoDBConnection):
|
||||
if field == 'asset':
|
||||
tx = Transaction.create([user_pub], [([user_pub], 1)],
|
||||
asset=value)
|
||||
elif field == 'metadata':
|
||||
tx = Transaction.create([user_pub], [([user_pub], 1)],
|
||||
metadata=value)
|
||||
tx = tx.sign([user_priv])
|
||||
res = client.post(TX_ENDPOINT, data=json.dumps(tx.to_dict()))
|
||||
|
||||
assert res.status_code == expected_status_code
|
||||
if res.status_code == 400:
|
||||
expected_error_message = (
|
||||
'Invalid transaction (ValidationError): Invalid key name "{}" '
|
||||
'in {} object. The key name cannot contain characters '
|
||||
'".", "$" or null characters').format(err_key, field)
|
||||
assert res.json['message'] == expected_error_message
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user