예제 #1
0
async def get_config(args):
    sec_key = args.sec_key
    if '/' in sec_key:
        sec, key = sec_key.split('/')
        r = get_sharedconfig().get_strict(sec, key)
    else:
        r = get_sharedconfig().get_section_strict(sec_key)
    print(json_pp(r))
예제 #2
0
    async def run(self, args):
        consumer = args.consumer
        if not consumer:
            raise Exception('void consumer')

        if not re.match(r'\w+$', consumer):
            raise Exception('invalid consumer')

        cfg = get_sharedconfig()
        coptions = cfg.get('consumers', consumer)
        if coptions:
            print('consumer {} already exist, secret is {}'.format(
                consumer, coptions['secret']))
            return

        coptions = {}
        coptions['secret'] = uuid.uuid4().hex
        coptions['seed'] = ssl.RAND_bytes(256).hex()

        c = get_cluster()
        await c.set_config('consumers', consumer, coptions)

        # TODO: limit the consumer size

        print('secret for', consumer, coptions['secret'])
예제 #3
0
async def verify_consumer_token(request, token):
    '''
    verifyToken(token)
    verify a token, it may be invalid or expired
    '''
    arr = token.split(':')
    if len(arr) != 4 or re.search(r'\s', token):
        raise ServiceError('invalid token', 'token {}'.format(token))

    consumer, expire_at, nonce, digest = arr
    cfg = get_sharedconfig()
    coptions = cfg.get('consumers', consumer)
    if not coptions:
        raise ServiceError('consumer not found')

    if int(expire_at) < time.time():
        raise ServiceError('token expired')

    digest_src = ':'.join([consumer, expire_at, nonce, coptions['seed']])
    m = sha256()
    m.update(digest_src.encode('utf-8'))
    new_digest = m.hexdigest()
    if new_digest != digest:
        raise ServiceError('verify failed')

    return {'consumer': consumer, 'expire_at': expire_at, 'verified': True}
예제 #4
0
async def create_consumer_token(request, consumer, secret, options=None):
    '''
    createToken(consumer, secret, options=None)
    Create a consume token by preallocated consumer and secret
    '''

    if not isinstance(consumer, str):
        raise ServiceError('invalid consumer')
    options = options or {}
    cfg = get_sharedconfig()
    coptions = cfg.get('consumers', consumer)
    if not coptions:
        raise ServiceError('consumer not found')

    if not isinstance(secret, str):
        raise ServiceError('invalid secret')

    if coptions['secret'] != secret:
        raise ServiceError('consumer verify failed')

    expire_in = int(options.get('expire_in', 3 * 365 * 86400))
    if expire_in < 60:
        raise ServiceError('cannot expire too early')

    expire_at = int(time.time() + expire_in)
    nonce = uuid.uuid4().hex
    digest_src = ':'.join([consumer, str(expire_at), nonce, coptions['seed']])
    m = sha256()
    m.update(digest_src.encode('utf-8'))
    digest = m.hexdigest()
    token = ':'.join([consumer, str(expire_at), nonce, digest])
    return {'consumer': consumer, 'token': token, 'expire_at': expire_at}
예제 #5
0
async def create_consumer(request, consumer, reuse):
    '''
    createConsumer(consumer, reuse)
    create a consumer for test, note that this methods is *ONLY* used for testing. Production consumer can be created using `bbox.py run aiobbox.contrib.consumer.create_consumer <name>`
    '''
    if not testing.test_mode:
        raise ServiceError('access denied')

    if (not isinstance(consumer, str) or not re.match(r'\w+$', consumer)):
        raise ServiceError('invalid consumer')

    cfg = get_sharedconfig()
    coptions = cfg.get('consumers', consumer)
    #if coptions:
    #    raise ServiceError('consumer already exist',
    #                       msg='already exist {}'.format(coptions))
    if coptions and reuse:
        return {'consumer': consumer, 'secret': coptions['secret']}

    coptions = {}
    coptions['secret'] = uuid.uuid4().hex
    coptions['seed'] = ssl.RAND_bytes(256).hex()

    c = get_cluster()
    await c.set_config('consumers', consumer, coptions)

    # TODO: limit the consumer size
    return {'consumer': consumer, 'secret': coptions['secret']}
예제 #6
0
async def load_config(args):
    jsonfile = args.jsonfile
    with open(jsonfile, 'r', encoding='utf-8') as f:
        new_sections = json.load(f)
    rem_set, add_set = get_sharedconfig().compare_sections(
        new_sections)
    if args.purge:
        for sec, key, value in rem_set:
            print("delete", sec, key)
            await get_cluster().del_config(sec, key)

    for sec, key, value in add_set:
        value = json.loads(value)
        print("set", sec, key)
        await get_cluster().set_config(sec, key, value)
예제 #7
0
async def dump_config(args):
    data = get_sharedconfig().dump_json()
    print(data)
예제 #8
0
파일: helpers.py 프로젝트: Freeza91/bbox
def has_consumer(consumer):
    cfg = get_sharedconfig()
    return cfg.has_key('consumers', consumer)