示例#1
0
def queryDpm(args, config):
    """ Download the policies from the DB and execute the related B2SAFE workflow

    @type  args:       list of objects
    @param args:       The list of input parameters
    @type  config:     ConfigLoader object
    @param config:     It contains the configuration parameters
    """
    debug = args.verbose
    # manage the checksum verification
    chk_veri = False
    chk_verify = config.SectionMap('Integrity')['checksum_verify']
    if chk_verify.lower() == 'true':
        chk_veri = True
    # load the policy schema path/url
    policySchemaUrl = None
    policySchema = config.SectionMap('Schemas')['policies']
    if policySchema.startswith('http://'):
        policySchemaUrl = policySchema
    # get the list of policies matching the input criteria
    conn = ServerConnector(args.config, args.test, "PolicyManager", debug)
    policy_files = getInfoPolicies(args,logger)
    if policy_files is not None:
        for url in policy_files:
            logger.info('Processing policy: %s', url)
            # load the XML policy schema
            pParser = PolicyParser(None, args.test, 'PolicyManager', debug)
            policySchemaDoc = pParser.parseXmlSchema([policySchemaUrl], 
                                                     [policySchema])
            if chk_veri:
                # get the id from the policy on the DB
                policyId = conn.getDocumentByUrl(url, '//*:policy/@uniqueid/data()')
                # get the status doc from the DB for the checksum
                status, doc = conn.getStatus(policyId)
                # parse the policy and validate against schema and checksum
                errMsg = pParser.parseFromUrl(url, policySchemaDoc, conn, 
                         status[staNs+':policy'][staNs+':checksum']['@method'],
                         status[staNs+':policy'][staNs+':checksum']['#text'])
            else:
                # parse the policy and validate against schema
                errMsg = pParser.parseFromUrl(url, policySchemaDoc, conn)
            if errMsg is not None:
                print 'ERROR: ' + errMsg
                exit(1)            
            if pParser.policy is not None:
                # load user mapping
                mapFilename = config.SectionMap('AccountMapping')['file']
                usermap = loadUserMap(mapFilename)
                # schedule the policy
                runPolicy(pParser.policy, usermap, args.test, 'PolicyManager',
                          debug)
示例#2
0
def addPolicyStatus(args, mylogger=None):
    """ Check if a status document exist for each policy 
        and, if not, it creates a new one.

    @type  args:       list of objects
    @param args:       The list of input parameters
                       considered to search for policies 
    @type logger:      loggin.logger object
    @param logger:     the logger
    """
    debug = args.verbose
    config = ConfigLoader(args.config)
    st_pre = config.SectionMap('DpmServer')['status_prefix']
    if mylogger is None:
        logger = setLoggingSystem(config, debug)
    else:
        logger = mylogger

    logger.info('Start to list the policies')
    conn = ServerConnector(args.config, args.test, "DBCommander", debug)

    attributes = {}
    # loading the default from config
    if len(config.SectionMap('PolicyFilters')) > 0:
        logger.debug('Loading the filter parameters from the config file')
        for par in config.SectionMap('PolicyFilters'):
            attributes[par] = config.SectionMap('PolicyFilters')[par]
    # loading the filter parameters from the input
    if args.filter:
        logger.debug('Loading the filter parameters from the input')
        pairs = args.filter.split(',')
        for pair in pairs:
            try:
                key, value = pair.split(':')
            except:
                print 'wrong value [{}] as a filter'.format(str(pair))
                sys.exit(1)
            attributes[key] = value
    # filter policies according to input time interval
    if args.start is not None:
        sdate = datetime.strptime(args.start, "%d-%m-%Y %H:%M")
        start = int(time.mktime(sdate.timetuple()))
        policies = conn.listPolicies(attributes, start)
        if args.end is not None:
            edate = datetime.strptime(args.end, "%d-%m-%Y %H:%M")
            end = int(time.mktime(edate.timetuple()))
            policies = conn.listPolicies(attributes, start, end)
    elif args.end is not None:
        edate = datetime.strptime(args.end, "%d-%m-%Y %H:%M")
        end = int(time.mktime(edate.timetuple()))
        policies = conn.listPolicies(attributes, 0, end)
    else:
        policies = conn.listPolicies(attributes)

    # listing of the policies matching the criteria of the dict "attributes"
    if policies is not None:
        for url in policies:
            logger.debug('Checking status for policy with URL: ' + url)
            community_name = (url.rsplit('/', 2)[-2]).split('_',2)[2]
            uniqueid = conn.getDocumentByUrl(url, "//*:policy/@uniqueid/data()")
            # get the status doc from the DB
            dbname = st_pre + community_name
            status, doc = conn.getStatus(uniqueid, dbname)
            if status is None:
                logger.debug('Status doc not found, creating a new one')
                response = conn.createStatus(uniqueid, 'NEW', dbname)
                print 'Added new status for policy {}, response: {}'.format(
                                                                     uniqueid,
                                                                     response)
            else:
                logger.debug('Status doc already available')
                print ('Status already available, nothing to add for policy {}'
                      ).format(uniqueid)
    else:
        print 'Policies not found'
    
    return policies
示例#3
0
def getInfoPolicies(args, mylogger=None):
    """ Download the policies from the DB and execute the related B2SAFE workflow

    @type  args:       list of objects
    @param args:       The list of input parameters
                       considered to search for policies 
    @type logger:      loggin.logger object
    @param logger:     the logger
    """
    debug = args.verbose
    config = ConfigLoader(args.config)
    st_pre = config.SectionMap('DpmServer')['status_prefix']
    if mylogger is None:
        logger = setLoggingSystem(config, debug)
    else:
        logger = mylogger
    logger.info('Start to list the policies')
    conn = ServerConnector(args.config, args.test, "PolicyManager", debug)
    # if a policy id is provided the whole policy doc is shown
    if args.subcmd == 'list' and args.id:
        logger.debug('Policy with id [{}] is downloaded'.format(args.id))
        polDict, polDoc = conn.getPolicy(args.id)
        print polDoc
        return None
    attributes = {}
    # loading the default from config
    if len(config.SectionMap('PolicyFilters')) > 0:
        logger.debug('Loading the filter parameters from the config file')
        for par in config.SectionMap('PolicyFilters'):
            attributes[par] = config.SectionMap('PolicyFilters')[par]
    # loading the filter parameters from the input
    if args.filter:
        logger.debug('Loading the filter parameters from the input')
        pairs = args.filter.split(',')
        for pair in pairs:
            try:
                key, value = pair.split(':')
            except:
                print 'wrong value [{}] as a filter'.format(str(pair))
                sys.exit(1)
            attributes[key] = value
    # filter policies according to input time interval
    if args.start is not None:
        sdate = datetime.strptime(args.start, "%d-%m-%Y %H:%M")
        start = int(time.mktime(sdate.timetuple()))
        policies = conn.listPolicies(attributes, start)
        if args.end is not None:
            edate = datetime.strptime(args.end, "%d-%m-%Y %H:%M")
            end = int(time.mktime(edate.timetuple()))
            policies = conn.listPolicies(attributes, start, end)
    elif args.end is not None:
        edate = datetime.strptime(args.end, "%d-%m-%Y %H:%M")
        end = int(time.mktime(edate.timetuple()))
        policies = conn.listPolicies(attributes, 0, end)
    else:
        policies = conn.listPolicies(attributes)
    # listing of the policies matching the criteria of the dict "attributes"
    if policies is not None:
        for url in policies:
            print url
            community_name = (url.rsplit('/', 2)[-2]).split('_',2)[2]
            if args.subcmd == 'list'and args.ext:
                # listing policies with extended attributes
                xmldoc = conn.getDocumentByUrl(url)
                pol = xmltodict.parse(xmldoc, process_namespaces=True)
                for key in pol[polNs+':policy']:
                    if isinstance(key, basestring) and key.startswith('@'):
                        print '{} = {}'.format(key[1:], 
                                               pol[polNs+':policy'][key])
                # get the status doc from the DB
                dbname = None
                if args.all:
                    dbname = st_pre + community_name
                status, doc = conn.getStatus(pol[polNs+':policy']['@uniqueid'], dbname)
                if status is None:
                    print 'status = '
                    print 'checksum = '
                else:
                    st = status[staNs+':policy'][staNs+':status']
                    if args.all:
                        print 'overall status = {}'.format(st[staNs+':overall'])
                        if (st[staNs+':details'] is not None
                            and st[staNs+':details'][staNs+':site'] is not None):
                            if isinstance(st[staNs+':details'][staNs+':site'], list):
                                for line in st[staNs+':details'][staNs+':site']:
                                    print '{: ^4}status[{}] = {}'.format('', 
                                                                  line['@name'],
                                                                  line['#text'])
                            else:
                                print '{: ^4}status[{}] = {}'.format('',
                                   st[staNs+':details'][staNs+':site']['@name'],
                                   st[staNs+':details'][staNs+':site']['#text'])
                    else:
                        print 'status = {}'.format(
                            status[staNs+':policy'][staNs+':status']
                                                   [staNs+':overall'])
                    print 'checksum = {}'.format(
                          status[staNs+':policy'][staNs+':checksum']['#text'])
            print '{: ^40}'.format('')
    else:
        print 'Nothing found'
    
    return policies