Ejemplo n.º 1
0
    def __call__(self, environ, start_response) :
        try :
            block_ids = UnpackJSONRequest(environ)

        except Exception as e :
            logger.exception('CheckBlocksApp')
            return ErrorResponse(start_response, "unknown exception while unpacking block status request")

        try :
            block_status_list = self.block_store.check_blocks(block_ids, encoding='b64')

        except Exception as e :
            logger.exception('CheckBlocksApp')
            return ErrorResponse(start_response, "unknown exception while computing block status")

        try :
            result = json.dumps(block_status_list).encode()
        except Exception as e :
            logger.exception('CheckBlocksApp')
            return ErrorResponse(start_response, "unknown exception while packing response")

        status = "{0} {1}".format(HTTPStatus.OK.value, HTTPStatus.OK.name)
        headers = [
                   ('Content-Type', 'application/json'),
                   ('Content-Transfer-Encoding', 'utf-8'),
                   ('Content-Length', str(len(result)))
                   ]
        start_response(status, headers)
        return [result]
Ejemplo n.º 2
0
    def __call__(self, environ, start_response):
        try:
            request = UnpackMultipartRequest(environ)
            request_index = IndexMultipartRequest(request)
            encrypted_session_key = request.parts[
                request_index['encrypted_session_key']].content
            encrypted_request = request.parts[
                request_index['encrypted_request']].content
        except KeyError as ke:
            logger.error('missing field in request: %s', ke)
            return ErrorResponse(start_response,
                                 'missing field {0}'.format(ke))
        except Exception as e:
            logger.error("unknown exception unpacking request (Invoke); %s",
                         str(e))
            return ErrorResponse(start_response,
                                 "unknown exception while unpacking request")

        try:
            result = self.enclave.send_to_contract(encrypted_session_key,
                                                   encrypted_request)
        except Exception as e:
            logger.error('unknown exception processing request (Invoke); %s',
                         str(e))
            return ErrorResponse(start_response,
                                 'unknown exception processing request')

        status = "{0} {1}".format(HTTPStatus.OK.value, HTTPStatus.OK.name)
        headers = [('Content-Type', 'application/octet-stream'),
                   ('Content-Transfer-Encoding', 'utf-8'),
                   ('Content-Length', str(len(result)))]
        start_response(status, headers)
        return [result]
Ejemplo n.º 3
0
    def __call__(self, environ, start_response):
        try:
            # get the block id from the URL path
            block_id = environ['PATH_INFO'][1:]  # skip the '/'
            if block_id is None:
                return ErrorResponse(start_response,
                                     "request missing block id")
        except Exception as e:
            logger.exception('get block')
            return ErrorResponse(
                start_response,
                'unknown exception while unpacking the request')

        try:
            block_data = self.block_store.get_block(block_id, encoding='b64')
            if block_data is None:
                msg = "unknown block; {0}".format(block_id)
                return ErrorResponse(start_response,
                                     msg,
                                     status=HTTPStatus.NOT_FOUND)

        except Exception as e:
            logger.exception('get block')
            msg = "unknown exception while processing get block request; {0}".format(
                block_id)
            return ErrorResponse(start_response, msg)

        status = "{0} {1}".format(HTTPStatus.OK.value, HTTPStatus.OK.name)
        headers = [('Content-Type', 'application/octet-stream'),
                   ('Content-Transfer-Encoding', 'utf-8'),
                   ('Content-Length', str(len(block_data)))]
        start_response(status, headers)
        return [block_data]
Ejemplo n.º 4
0
    def __call__(self, environ, start_response):
        # get the block id from the URL path

        try:
            request = UnpackMultipartRequest(environ)
            if len(request.parts) < 1:
                return ErrorResponse(start_response,
                                     'missing block store operation')

            operation = request.parts[0]
            if operation.headers[b'Content-Type'] != b'application/json':
                return ErrorResponse(start_response,
                                     'missing block store operation')

            data = operation.text
            try:
                data = data.decode('utf-8')
            except AttributeError:
                pass

            minfo = json.loads(data)
            expiration = minfo['expiration']
        except Exception as e:
            logger.exception('StoreBlocksApp')
            return ErrorResponse(
                start_response,
                "unknown exception while unpacking block store request")

        try:
            # block_list will be an iterator for blocks in the request, this prevents
            # the need to make a copy of the data blocks
            block_list = self.block_data_iterator(request)
            raw_result = self.block_store.store_blocks(block_list,
                                                       expiration=expiration,
                                                       encoding='b64')
        except Exception as e:
            logger.exception('StoreBlocksApp')
            return ErrorResponse(start_response,
                                 "unknown exception while storing blocks")

        try:
            result = json.dumps(raw_result).encode('utf8')
        except Exception as e:
            logger.exception('StoreBlocksApp')
            return ErrorResponse(start_response,
                                 "unknown exception while packing response")

        status = "{0} {1}".format(HTTPStatus.OK.value, HTTPStatus.OK.name)
        headers = [('Content-Type', 'application/json'),
                   ('Content-Transfer-Encoding', 'utf-8'),
                   ('Content-Length', str(len(result)))]
        start_response(status, headers)
        return [result]
Ejemplo n.º 5
0
    def __call__(self, environ, start_response):
        try:
            minfo = UnpackJSONRequest(environ)
            contractid = minfo['contract_id']
            creatorid = minfo['creator_id']
            secrets = minfo['secrets']

            # verify the integrity of the secret list
            for secret in secrets:
                assert secret['pspk']
                assert secret['encrypted_secret']

        except KeyError as ke:
            logger.error('missing field in request (Verify): %s', ke)
            return ErrorResponse(start_response,
                                 'missing field {0}'.format(ke))
        except Exception as e:
            logger.error("unknown exception unpacking request (Verify); %s",
                         str(e))
            return ErrorResponse(start_response,
                                 "unknown exception while unpacking request")

        try:
            response = self.enclave.verify_secrets(contractid, creatorid,
                                                   secrets)

        except Exception as e:
            logger.error('unknown exception processing request (Verify); %s',
                         str(e))
            return ErrorResponse(start_response,
                                 'uknown exception processing request')

        try:
            result = json.dumps(dict(response)).encode()
        except Exception as e:
            logger.error("unknown exception packing response (Verify); %s",
                         str(e))
            return ErrorResponse(start_response,
                                 "unknown exception while packing response")

        status = "{0} {1}".format(HTTPStatus.OK.value, HTTPStatus.OK.name)
        headers = [('Content-Type', 'application/json'),
                   ('Content-Length', str(len(result)))]
        start_response(status, headers)
        return [result]
Ejemplo n.º 6
0
    def __call__(self, environ, start_response):
        try:
            block_ids = UnpackJSONRequest(environ)
        except Exception as e:
            logger.exception('get blocks')
            return ErrorResponse(
                start_response,
                'unknown exception while unpacking get blocks request')
        try:
            response = dict()
            for block_id in block_ids:
                block_data = self.block_store.get_block(block_id,
                                                        encoding='b64')
                if block_data is None:
                    msg = "unknown block; {0}".format(block_id)
                    return ErrorResponse(start_response,
                                         msg,
                                         status=HttpStatus.NOT_FOUND)
                response[block_id] = (None, block_data,
                                      'application/octet-stream')

        except Exception as e:
            logger.exception('get blocks')
            return ErrorResponse(
                start_response,
                "unknown exception while processing get blocks request")

        try:
            encoder = MultipartEncoder(response)
        except Exception as e:
            logger.exception('get blocks')
            return ErrorResponse(
                start_response,
                'unknown exception while packget get blocks response')

        status = "{0} {1}".format(HTTPStatus.OK.value, HTTPStatus.OK.name)
        headers = [('content-type', 'application/octet-stream'),
                   ('x-content-type', encoder.content_type),
                   ('content-transfer-encoding', 'utf-8'),
                   ('content-length', str(encoder.len))]
        start_response(status, headers)
        return [encoder.to_string()]
Ejemplo n.º 7
0
    def __call__(self, environ, start_response):
        try:
            block_ids = self.block_store.list_blocks(encoding='b64')
            result = json.dumps(block_ids).encode()

        except Exception as e:
            logger.exception('ListBlocksApp')
            return ErrorResponse(
                start_response,
                "unknown exception while processing list blocks request")

        status = "{0} {1}".format(HTTPStatus.OK.value, HTTPStatus.OK.name)
        headers = [('Content-Type', 'application/json'),
                   ('Content-Transfer-Encoding', 'utf-8'),
                   ('Content-Length', str(len(result)))]
        start_response(status, headers)
        return [result]
Ejemplo n.º 8
0
    def __call__(self, environ, start_response):
        """Return blockstore information
        """
        try:
            response = {'verifying_key': self.service_keys.verifying_key}
            result = json.dumps(response).encode()

        except Exception as e:
            logger.exception("info")
            return ErrorResponse(start_response,
                                 "exception; {0}".format(str(e)))

        status = "{0} {1}".format(HTTPStatus.OK.value, HTTPStatus.OK.name)
        headers = [('Content-Type', 'application/octet-stream'),
                   ('Content-Transfer-Encoding', 'utf-8'),
                   ('Content-Length', str(len(result)))]
        start_response(status, headers)
        return [result]
Ejemplo n.º 9
0
    def __call__(self, environ, start_response):
        try:
            response = dict()
            response['verifying_key'] = self.enclave.verifying_key
            response['encryption_key'] = self.enclave.encryption_key
            response['enclave_id'] = self.enclave.enclave_id
            response['interpreter'] = self.enclave.interpreter
            response['storage_service_url'] = self.storage_url

            result = json.dumps(response).encode()
        except Exception as e:
            logger.exception("info")
            return ErrorResponse(start_response,
                                 "exception; {0}".format(str(e)))

        status = "{0} {1}".format(HTTPStatus.OK.value, HTTPStatus.OK.name)
        headers = [('Content-Type', 'application/json'),
                   ('Content-Length', str(len(result)))]
        start_response(status, headers)
        return [result]
    def __call__(self, environ, start_response):
        # get the block id from the URL path

        try:
            request = UnpackMultipartRequest(environ)
            if len(request.parts) < 1:
                return ErrorResponse(start_response,
                                     'missing block store operation')

            operation = request.parts[0]
            if operation.headers[b'Content-Type'] != b'application/json':
                return ErrorResponse(start_response,
                                     'missing block store operation')

            data = operation.text
            try:
                data = data.decode('utf-8')
            except AttributeError:
                pass

            minfo = json.loads(data)
            duration = minfo['duration']
        except Exception as e:
            logger.exception('StoreBlocksApp')
            return ErrorResponse(
                start_response,
                "unknown exception while unpacking block store request")

        try:
            # block_list will be an iterator for blocks in the request, this prevents
            # the need to make a copy of the data blocks
            block_list = self.block_data_iterator(request)
            block_hashes = self.block_store.store_blocks(block_list,
                                                         duration=duration,
                                                         encoding='b64')
        except Exception as e:
            logger.exception('StoreBlocksApp')
            return ErrorResponse(start_response,
                                 "unknown exception while storing blocks")

        try:
            # going to just concatenate all hashes, safe since these are all fixed size
            signing_hash_accumulator = duration.to_bytes(32,
                                                         byteorder='big',
                                                         signed=False)
            signing_hash_accumulator += b''.join(block_hashes)

            signing_hash = hashlib.sha256(signing_hash_accumulator).digest()
            signature = self.service_keys.sign(signing_hash, encoding='b64')
        except Exception as e:
            logger.exception(
                "unknown exception packing response (BlockStatus); %s", str(e))
            return ErrorResponse('signature generation failed')

        encoding_fn = lambda x: base64.urlsafe_b64encode(x).decode()

        result = dict()
        result['signature'] = signature
        result['block_ids'] = list(map(encoding_fn, block_hashes))

        try:
            result = json.dumps(result).encode('utf8')
        except Exception as e:
            logger.exception('StoreBlocksApp')
            return ErrorResponse(start_response,
                                 "unknown exception while packing response")

        status = "{0} {1}".format(HTTPStatus.OK.value, HTTPStatus.OK.name)
        headers = [('Content-Type', 'application/json'),
                   ('Content-Transfer-Encoding', 'utf-8'),
                   ('Content-Length', str(len(result)))]
        start_response(status, headers)
        return [result]