Esempio n. 1
0
 def test_client_is_represented_properly_without_bucket(self):
     client = Client(server_url="https://kinto.notmyidea.org/v1",
                     bucket=None)
     expected_repr = ("<KintoClient https://kinto.notmyidea.org/v1/>")
     assert str(client) == expected_repr
Esempio n. 2
0
 def setUp(self):
     self.session = mock.MagicMock()
     self.session.request.return_value = (mock.sentinel.response, mock.sentinel.count)
     self.client = Client(
         session=self.session, bucket='mybucket',
         collection='mycollection')
Esempio n. 3
0
def main():
    args = _get_args()

    client = Client(server_url=args.server,
                    auth=tuple(args.auth.split(':')),
                    bucket=args.source_bucket,
                    collection=args.source_col)

    if args.editor_auth is None:
        args.editor_auth = args.auth

    if args.reviewer_auth is None:
        args.reviewer_auth = args.auth

    editor_client = Client(server_url=args.server,
                           auth=tuple(args.editor_auth.split(':')),
                           bucket=args.source_bucket,
                           collection=args.source_col)
    reviewer_client = Client(server_url=args.server,
                             auth=tuple(args.reviewer_auth.split(':')),
                             bucket=args.source_bucket,
                             collection=args.source_col)

    # 0. initialize source bucket/collection (if necessary)
    server_info = client.server_info()
    editor_id = editor_client.server_info()['user']['id']
    reviewer_id = reviewer_client.server_info()['user']['id']
    print('Server: {0}'.format(args.server))
    print('Author: {user[id]}'.format(**server_info))
    print('Editor: {0}'.format(editor_id))
    print('Reviewer: {0}'.format(reviewer_id))

    # 0. check that this collection is well configured.
    signer_capabilities = server_info['capabilities']['signer']
    to_review_enabled = signer_capabilities.get('to_review_enabled', False)
    group_check_enabled = signer_capabilities.get('group_check_enabled', False)

    resources = [
        r for r in signer_capabilities['resources']
        if (args.source_bucket, args.source_col) == (r['source']['bucket'],
                                                     r['source']['collection'])
    ]
    assert len(resources) > 0, 'Specified source not configured to be signed'
    resource = resources[0]
    if to_review_enabled and 'preview' in resource:
        print(
            'Signoff: {source[bucket]}/{source[collection]} => {preview[bucket]}/{preview[collection]} => {destination[bucket]}/{destination[collection]}'
            .format(**resource))
    else:
        print(
            'Signoff: {source[bucket]}/{source[collection]} => {destination[bucket]}/{destination[collection]}'
            .format(**resource))
    print('Group check: {0}'.format(group_check_enabled))
    print('Review workflow: {0}'.format(to_review_enabled))

    print('_' * 80)

    bucket = client.create_bucket(if_not_exists=True)
    client.patch_bucket(permissions={
        'write': [editor_id, reviewer_id] + bucket['permissions']['write']
    },
                        if_match=bucket['data']['last_modified'],
                        safe=True)

    client.create_collection(if_not_exists=True)

    if args.reset:
        client.delete_records()
        existing = 0
    else:
        existing_records = client.get_records()
        existing = len(existing_records)

    if group_check_enabled:
        editors_group = signer_capabilities['editors_group']
        client.create_group(editors_group,
                            data={'members': [editor_id]},
                            if_not_exists=True)
        reviewers_group = signer_capabilities['reviewers_group']
        client.create_group(reviewers_group,
                            data={'members': [reviewer_id]},
                            if_not_exists=True)

    dest_client = Client(server_url=args.server,
                         bucket=resource['destination']['bucket'],
                         collection=resource['destination']['collection'])

    preview_client = None
    if to_review_enabled and 'preview' in resource:
        preview_bucket = resource['preview']['bucket']
        preview_collection = resource['preview']['collection']
        preview_client = Client(server_url=args.server,
                                bucket=preview_bucket,
                                collection=preview_collection)

    # 1. upload data
    print('Author uploads 20 random records')
    records = upload_records(client, 20)

    # 2. ask for a signature
    # 2.1 ask for review (noop on old versions)
    print('Editor asks for review')
    data = {"status": "to-review"}
    editor_client.patch_collection(data=data)
    # 2.2 check the preview collection (if enabled)
    if preview_client:
        print('Check preview collection')
        preview_records = preview_client.get_records()
        expected = existing + 20
        assert len(preview_records) == expected, '%s != %s records' % (
            len(preview_records), expected)
        metadata = preview_client.get_collection()['data']
        preview_signature = metadata.get('signature')
        assert preview_signature, 'Preview collection not signed'
        preview_timestamp = collection_timestamp(preview_client)
    # 2.3 approve the review
    print('Reviewer approves and triggers signature')
    data = {"status": "to-sign"}
    reviewer_client.patch_collection(data=data)

    # 3. upload more data
    print('Author creates 20 others records')
    upload_records(client, 20)

    print('Editor updates 5 random records')
    for toupdate in random.sample(records, 5):
        editor_client.patch_record(dict(newkey=_rand(10), **toupdate))

    print('Author deletes 5 random records')
    for todelete in random.sample(records, 5):
        client.delete_record(todelete['id'])

    expected = existing + 20 + 20 - 5

    # 4. ask again for a signature
    # 2.1 ask for review (noop on old versions)
    print('Editor asks for review')
    data = {"status": "to-review"}
    editor_client.patch_collection(data=data)
    # 2.2 check the preview collection (if enabled)
    if preview_client:
        print('Check preview collection')
        preview_records = preview_client.get_records()
        assert len(preview_records) == expected, '%s != %s records' % (
            len(preview_records), expected)
        # Diff size is 20 + 5 if updated records are also all deleted,
        # or 30 if deletions and updates apply to different records.
        diff_since_last = preview_client.get_records(_since=preview_timestamp)
        assert 25 <= len(
            diff_since_last
        ) <= 30, 'Changes since last signature are not consistent'

        metadata = preview_client.get_collection()['data']
        assert preview_signature != metadata[
            'signature'], 'Preview collection not updated'

    # 2.3 approve the review
    print('Reviewer approves and triggers signature')
    data = {"status": "to-sign"}
    reviewer_client.patch_collection(data=data)

    # 5. wait for the result

    # 6. obtain the destination records and serialize canonically.

    records = list(dest_client.get_records())
    assert len(records) == expected, '%s != %s records' % (len(records),
                                                           expected)
    timestamp = collection_timestamp(dest_client)
    serialized = canonical_json(records, timestamp)
    print('Hash is %r' % compute_hash(serialized))

    # 7. get back the signed hash

    dest_col = dest_client.get_collection()
    signature = dest_col['data']['signature']

    with open('pub', 'w') as f:
        f.write(signature['public_key'])

    # 8. verify the signature matches the hash
    signer = ECDSASigner(public_key='pub')
    try:
        signer.verify(serialized, signature)
        print('Signature OK')
    except Exception:
        print('Signature KO')
        raise
Esempio n. 4
0
def get_client(user, password):
    return Client(server_url="https://kinto.notmyidea.org/v1",
                  auth=('notes', password),
                  bucket='webnotesapp',
                  collection=user)
Esempio n. 5
0
from kinto_http import Client
from kinto_http.patch_type import BasicPatch, MergePatch, JSONPatch
credentials = ('admin', 's3cr3t')

client = Client(server_url='http://localhost:8888/v1', auth=credentials)

client.create_bucket(id='payments')

client.update_bucket(id='payments', permissions={"read": ["account: admin"]})

#To create a group.
client.create_group(id='receipts',
                    bucket='payments',
                    data={'members': ['admin', 'nhihieu']})

#To create a collection.
client.create_collection(id='receipts', bucket='payments')

#You can pass a python dictionary to create the record.
client.create_record(data={
    'status': 'done',
    'title': 'Todo #1'
},
                     permissions={'read': ['group:groupid']},
                     collection='receipts',
                     bucket='payments')

# You can use id to specify the record id when creating it.
client.create_record(id='todo2',
                     data={
                         'status': 'doing',
import re
from config import kinto_url, kinto_admin, kinto_password
from utils import accident_is_valid
from uuid import uuid5, NAMESPACE_URL

from kinto_http import Client

client = Client(server_url=kinto_url,
                auth=(kinto_admin, kinto_password),
                retry=10)

raw_accident_schema = client.get_collection(bucket='accidents',
                                            id='accidents_raw')

# use only the first type of the schema, we're having some fields with for
# example type: ["string", null] in there
raw_accident_schema = {
    key: {
        **value,
        **{
            'type': value['type'][0] if value['type'][0] else value['type']
        }
    }
    for (key, value) in (
        raw_accident_schema['data']['schema']['properties'].items())
}

for key, value in raw_accident_schema.items():
    if value.get('pattern') is not None:
        value['pattern'] = re.compile(value['pattern'])
Esempio n. 7
0
 def setUp(self):
     self.session = mock.MagicMock()
     self.client = Client(session=self.session,
                          bucket='mybucket',
                          collection='mycollection')
Esempio n. 8
0
#!/usr/bin/env python
# encoding: utf-8
from kinto_http import Client  # 也可以用requests手动实现
import requests
import time
import json
credentials = ('wwj', 'wwj-test')
server_url = 'http://paperweekly.just4fun.site:8888/v1'
client = Client(server_url=server_url, auth=credentials)
collection = 'forum2wechat_todo_qq'  #forum2wechat_todo  # forum2wechat_done
#写到配置文件里
bucket = 'paperweekly_qq'
lastest_thread_timestamp = None  # 作为session存储
threads_records_pattern = "/buckets/{bucket}/collections/{collection}/records".format(
    bucket=bucket, collection=collection)
threads_records_url = "{}{}".format(server_url, threads_records_pattern)
with open("./kinto_cli_cookie.json") as kinto_cli_cookie:
    cookie_data = json.loads(kinto_cli_cookie.read())
    now_timestamp = str(int(time.time())) + "000"
    lastest_thread_timestamp = cookie_data[
        'lastest_thread_timestamp'] if cookie_data[
            'lastest_thread_timestamp'] else now_timestamp  #如果为空则从现在开始


def push_thread(thread_id, username, title, content):  #使用魔法参数
    data = {
        'thread_id': thread_id,
        'username': username,
        'title': title,
        'content': content
    }