Beispiel #1
0
def test_invalid_http_get_access_token(monkeypatch):
    monkeypatch.setattr(
        'processor.helper.httpapi.restapi_azure.get_from_currentdata',
        mock_get_from_currentdata)
    monkeypatch.setattr(os, 'getenv', mock_getenv)
    monkeypatch.setattr(
        'processor.helper.httpapi.restapi_azure.http_post_request',
        mock_invalid_http_post_request)
    from processor.helper.httpapi.restapi_azure import get_access_token
    val = get_access_token()
    assert val is None
Beispiel #2
0
def populate_azure_snapshot(snapshot, container=None, snapshot_type='azure'):
    """ Populates the resources from azure."""
    dbname = config_value('MONGODB', 'dbname')
    snapshot_source = get_field_value(snapshot, 'source')
    snapshot_user = get_field_value(snapshot, 'testUser')
    snapshot_nodes = get_field_value(snapshot, 'nodes')
    snapshot_data, valid_snapshotids = validate_snapshot_nodes(snapshot_nodes)
    client_id, client_secret, sub_name, sub_id, tenant_id = \
        get_web_client_data(snapshot_type, snapshot_source, snapshot_user)
    if not client_id:
        # logger.info("No client_id in the snapshot to access azure resource!...")
        raise Exception("No client id in the snapshot to access azure resource!...")

    # Read the client secrets from envirnment variable
    if not client_secret:
        client_secret = os.getenv(snapshot_user, None)
        if client_secret:
            logger.info('Client Secret from environment variable, Secret: %s', '*' * len(client_secret))
        
    # Read the client secrets from the vault
    if not client_secret:
        client_secret = get_vault_data(client_id)
        if client_secret:
            logger.info('Client Secret from Vault, Secret: %s', '*' * len(client_secret))
        elif get_from_currentdata(CUSTOMER):
            logger.error("Client Secret key does not set in a vault")
            raise Exception("Client Secret key does not set in a vault")

    if not client_secret:
        raise Exception("No `client_secret` key in the connector file to access azure resource!...")

    logger.info('\t\tSubscription: %s', sub_id)
    logger.info('\t\tTenant: %s', tenant_id)
    logger.info('\t\tclient: %s', client_id)
    put_in_currentdata('clientId', client_id)
    put_in_currentdata('clientSecret', client_secret)
    put_in_currentdata('subscriptionId', sub_id)
    put_in_currentdata('tenant_id', tenant_id)
    token = get_access_token()
    logger.debug('TOKEN: %s', token)
    if not token:
        logger.info("Unable to get access token, will not run tests....")
        raise Exception("Unable to get access token, will not run tests....")
        # return {}

    # snapshot_nodes = get_field_value(snapshot, 'nodes')
    # snapshot_data, valid_snapshotids = validate_snapshot_nodes(snapshot_nodes)
    if valid_snapshotids and token and snapshot_nodes:
        for node in snapshot_nodes:
            validate = node['validate'] if 'validate' in node else True
            if 'path' in  node:
                data = get_node(token, sub_name, sub_id, node, snapshot_user, snapshot_source)
                if data:
                    if validate:
                        if get_dbtests():
                            if get_collection_size(data['collection']) == 0:
                                # Creating indexes for collection
                                create_indexes(
                                    data['collection'], 
                                    config_value(DATABASE, DBNAME), 
                                    [
                                        ('snapshotId', pymongo.ASCENDING),
                                        ('timestamp', pymongo.DESCENDING)
                                    ]
                                )

                                create_indexes(
                                    data['collection'], 
                                    config_value(DATABASE, DBNAME), 
                                    [
                                        ('_id', pymongo.DESCENDING),
                                        ('timestamp', pymongo.DESCENDING),
                                        ('snapshotId', pymongo.ASCENDING)
                                    ]
                                )
                            insert_one_document(data, data['collection'], dbname, check_keys=False)
                        else:
                            snapshot_dir = make_snapshots_dir(container)
                            if snapshot_dir:
                                store_snapshot(snapshot_dir, data)
                        if 'masterSnapshotId' in node:
                            snapshot_data[node['snapshotId']] = node['masterSnapshotId']
                        else:
                            snapshot_data[node['snapshotId']] = True
                    # else:
                    #     snapshot_data[node['snapshotId']] = False
                    node['status'] = 'active'
                else:
                    # TODO alert if notification enabled or summary for inactive.
                    node['status'] = 'inactive'
                logger.debug('Type: %s', type(data))
            else:
                alldata = get_all_nodes(
                    token, sub_name, sub_id, node, snapshot_user, snapshot_source)
                if alldata:
                    snapshot_data[node['masterSnapshotId']] = []
                    for data in alldata:
                        # insert_one_document(data, data['collection'], dbname)
                        found_old_record = False
                        for masterSnapshotId, snapshot_list in snapshot_data.items():
                            old_record = None
                            if isinstance(snapshot_list, list):
                                for item in snapshot_list:
                                    if item["path"] == data['path']:
                                        old_record = item

                                if old_record:
                                    found_old_record = True
                                    if node['masterSnapshotId'] not in old_record['masterSnapshotId']:
                                        old_record['masterSnapshotId'].append(
                                            node['masterSnapshotId'])

                        if not found_old_record:
                            snapshot_data[node['masterSnapshotId']].append(
                                {
                                    'masterSnapshotId': [node['masterSnapshotId']],
                                    'snapshotId': data['snapshotId'],
                                    'path': data['path'],
                                    'validate': validate,
                                    'status': 'active'
                                })
                    # snapshot_data[node['masterSnapshotId']] = True
                logger.debug('Type: %s', type(alldata))
        delete_from_currentdata('resources')
        delete_from_currentdata('clientId')
        delete_from_currentdata('client_secret')
        delete_from_currentdata('subscriptionId')
        delete_from_currentdata('tenant_id')
        delete_from_currentdata('token')
    return snapshot_data
Beispiel #3
0
def populate_snapshot_azure(snapshot_json, fssnapshot):
    """ Populates the resources from azure."""
    snapshot_data, valid_snapshotids = fssnapshot.validate_snapshot_ids_in_nodes(
        snapshot_json)
    client_id, client_secret, sub_name, sub_id, tenant_id, connector_type = get_web_client_data(
        snapshot_json, fssnapshot)

    if not client_id:
        logger.info(
            "No client_id in the snapshot to access azure resource!...")
        # raise Exception("No client id in the snapshot to access azure resource!...")
        raise SnapshotsException(
            "Container %s failure as no client id in the snapshot to access azure resource!..."
            % fssnapshot.container)

    # Read the client secrets from the vault
    if not client_secret:
        client_secret = get_vault_data(client_id)
        if client_secret:
            logger.info('Client Secret from Vault, Secret: %s',
                        '*' * len(client_secret))
        elif fssnapshot.get_value(CUSTOMER):
            logger.error("Client Secret key does not set in a vault")
            raise SnapshotsException(
                "Client Secret key does not set in a vault")

    if not client_secret:
        raise SnapshotsException(
            "No `client_secret` key in the connector file to access azure resource!..."
        )

    logger.info('Sub:%s, tenant:%s, client: %s', sub_id, tenant_id, client_id)
    fssnapshot.store_value('clientId', client_id)
    fssnapshot.store_value('clientSecret', client_secret)
    fssnapshot.store_value('subscriptionId', sub_id)
    fssnapshot.store_value('tenant_id', tenant_id)
    token = get_access_token()
    logger.debug('TOKEN: %s', token)
    if not token:
        logger.info("Unable to get access token, will not run tests....")
        raise SnapshotsException(
            "Unable to get access token, will not run tests....")

    snapshot_source = get_field_value(snapshot_json, 'source')
    snapshot_user = get_field_value(snapshot_json, 'testUser')
    for node in fssnapshot.get_snapshot_nodes(snapshot_json):
        validate = node['validate'] if 'validate' in node else True
        if 'path' in node:
            data = get_snapshot_node(fssnapshot, token, sub_name, sub_id, node,
                                     snapshot_user, snapshot_source,
                                     connector_type)
            if data and validate:
                fssnapshot.store_data_node(data)
                snapshot_data[node['snapshotId']] = node[
                    'masterSnapshotId'] if 'masterSnapshotId' in node else True
                node['status'] = 'active'
            else:
                # TODO alert if notification enabled or summary for inactive.
                node['status'] = 'inactive'
            logger.debug('Type: %s', type(data))
        else:
            # Crawler Operation
            alldata = get_snapshot_nodes(fssnapshot, token, sub_name, sub_id,
                                         node, snapshot_user, snapshot_source,
                                         connector_type)
            if alldata:
                snapshot_data[node['masterSnapshotId']] = []
                for data in alldata:
                    found_old_record = False
                    for masterSnapshotId, snapshot_list in snapshot_data.items(
                    ):
                        old_record = None
                        if isinstance(snapshot_list, list):
                            for item in snapshot_list:
                                if item["path"] == data['path']:
                                    old_record = item

                            if old_record:
                                found_old_record = True
                                if node['masterSnapshotId'] not in old_record[
                                        'masterSnapshotId']:
                                    old_record['masterSnapshotId'].append(
                                        node['masterSnapshotId'])

                    if not found_old_record:
                        snapshot_data[node['masterSnapshotId']].append({
                            'masterSnapshotId': [node['masterSnapshotId']],
                            'snapshotId':
                            data['snapshotId'],
                            'path':
                            data['path'],
                            'validate':
                            validate,
                            'status':
                            'active'
                        })
            logger.debug('Type: %s', type(alldata))
    return snapshot_data