Beispiel #1
0
def get_nsx_security_group_id(session, cluster, neutron_id):
    """Return the NSX sec profile uuid for a given neutron sec group.

    First, look up the Neutron database. If not found, execute
    a query on NSX platform as the mapping might be missing.
    NOTE: Security groups are called 'security profiles' on the NSX backend.
    """
    nsx_id = nsx_db.get_nsx_security_group_id(session, neutron_id)
    if not nsx_id:
        # Find security profile on backend.
        # This is a rather expensive query, but it won't be executed
        # more than once for each security group in Neutron's lifetime
        nsx_sec_profiles = secgrouplib.query_security_profiles(
            cluster, '*',
            filters={'tag': neutron_id,
                     'tag_scope': 'q_sec_group_id'})
        # Only one result expected
        # NOTE(salv-orlando): Not handling the case where more than one
        # security profile is found with the same neutron port tag
        if not nsx_sec_profiles:
            LOG.warning("Unable to find NSX security profile for Neutron "
                        "security group %s", neutron_id)
            return
        elif len(nsx_sec_profiles) > 1:
            LOG.warning("Multiple NSX security profiles found for Neutron "
                        "security group %s", neutron_id)
        nsx_sec_profile = nsx_sec_profiles[0]
        nsx_id = nsx_sec_profile['uuid']
        with session.begin(subtransactions=True):
            # Create DB mapping
            nsx_db.add_neutron_nsx_security_group_mapping(
                session, neutron_id, nsx_id)
    return nsx_id
Beispiel #2
0
def fix_security_groups(resource, event, trigger, **kwargs):
    context_ = n_context.get_admin_context()
    sgs_with_missing_section = _find_missing_sections()
    sgs_with_missing_nsx_group = _find_missing_security_groups()
    if not sgs_with_missing_section and not sgs_with_missing_nsx_group:
        # no mismatches
        return

    with utils.NsxVPluginWrapper() as plugin:
        # If only the fw section is missing then create it.
        for sg_id in (set(sgs_with_missing_section.keys()) -
                      set(sgs_with_missing_nsx_group.keys())):
            neutron_sg.delete_security_group_section_mapping(sg_id)
            secgroup = plugin.get_security_group(context_, sg_id)
            plugin._create_fw_section_for_security_group(
                context_, secgroup,
                sgs_with_missing_section[sg_id]['nsx-securitygroup-id'])

        # If nsx security-group is missing then create both nsx security-group
        # and a new fw section (remove old one).
        for sg_id, sg in sgs_with_missing_nsx_group.items():
            secgroup = plugin.get_security_group(context_, sg_id)
            if sg_id not in sgs_with_missing_section:
                plugin._delete_section(sg['section-uri'])
            neutron_sg.delete_security_group_section_mapping(sg_id)
            neutron_sg.delete_security_group_backend_mapping(sg_id)
            plugin._process_security_group_create_backend_resources(
                context_, secgroup)
            nsx_id = nsx_db.get_nsx_security_group_id(context_.session, sg_id)
            for vnic_id in neutron_sg.get_vnics_in_security_group(sg_id):
                plugin._add_member_to_security_group(nsx_id, vnic_id)
Beispiel #3
0
def get_nsx_security_group_id(session, cluster, neutron_id):
    """Return the NSX sec profile uuid for a given neutron sec group.

    First, look up the Neutron database. If not found, execute
    a query on NSX platform as the mapping might be missing.
    NOTE: Security groups are called 'security profiles' on the NSX backend.
    """
    nsx_id = nsx_db.get_nsx_security_group_id(session, neutron_id)
    if not nsx_id:
        # Find security profile on backend.
        # This is a rather expensive query, but it won't be executed
        # more than once for each security group in Neutron's lifetime
        nsx_sec_profiles = secgrouplib.query_security_profiles(
            cluster,
            '*',
            filters={
                'tag': neutron_id,
                'tag_scope': 'q_sec_group_id'
            })
        # Only one result expected
        # NOTE(salv-orlando): Not handling the case where more than one
        # security profile is found with the same neutron port tag
        if not nsx_sec_profiles:
            LOG.warning(
                "Unable to find NSX security profile for Neutron "
                "security group %s", neutron_id)
            return
        elif len(nsx_sec_profiles) > 1:
            LOG.warning(
                "Multiple NSX security profiles found for Neutron "
                "security group %s", neutron_id)
        nsx_sec_profile = nsx_sec_profiles[0]
        nsx_id = nsx_sec_profile['uuid']
        with session.begin(subtransactions=True):
            # Create DB mapping
            nsx_db.add_neutron_nsx_security_group_mapping(
                session, neutron_id, nsx_id)
    return nsx_id
Beispiel #4
0
def migrate_sg_to_policy(resource, event, trigger, **kwargs):
    """Change the mode of a security group from rules to NSX policy"""
    if not kwargs.get('property'):
        LOG.error("Need to specify security-group-id and policy-id "
                  "parameters")
        return

    # input validation
    properties = admin_utils.parse_multi_keyval_opt(kwargs['property'])
    sg_id = properties.get('security-group-id')
    if not sg_id:
        LOG.error("Need to specify security-group-id parameter")
        return
    policy_id = properties.get('policy-id')
    if not policy_id:
        LOG.error("Need to specify policy-id parameter")
        return

    # validate that the security group exist and contains rules and no policy
    context_ = n_context.get_admin_context()
    with utils.NsxVPluginWrapper() as plugin:
        try:
            secgroup = plugin.get_security_group(context_, sg_id)
        except ext_sg.SecurityGroupNotFound:
            LOG.error("Security group %s was not found", sg_id)
            return
        if secgroup.get('policy'):
            LOG.error("Security group %s already uses a policy", sg_id)
            return

        # validate that the policy exists
        if not plugin.nsx_v.vcns.validate_inventory(policy_id):
            LOG.error("NSX policy %s was not found", policy_id)
            return

        # get the nsx id from the backend
        nsx_sg_id = nsx_db.get_nsx_security_group_id(context_.session,
                                                     sg_id,
                                                     moref=True)
        if not nsx_sg_id:
            LOG.error("Did not find security groups %s neutron ID", sg_id)
            return

        # Delete the rules from the security group
        LOG.info("Deleting the rules of security group: %s", sg_id)
        for rule in secgroup.get('security_group_rules', []):
            try:
                plugin.delete_security_group_rule(context_, rule['id'])
            except Exception as e:
                LOG.warning(
                    "Failed to delete rule %(r)s from security "
                    "group %(sg)s: %(e)s", {
                        'r': rule['id'],
                        'sg': sg_id,
                        'e': e
                    })
                # continue anyway

        # Delete the security group FW section
        LOG.info("Deleting the section of security group: %s", sg_id)
        try:
            section_uri = plugin._get_section_uri(context_.session, sg_id)
            plugin._delete_section(section_uri)
            nsxv_db.delete_neutron_nsx_section_mapping(context_.session, sg_id)
        except Exception as e:
            LOG.warning(
                "Failed to delete firewall section of security "
                "group %(sg)s: %(e)s", {
                    'sg': sg_id,
                    'e': e
                })
            # continue anyway

        # bind this security group to the policy in the backend and DB
        LOG.info("Binding the NSX security group %(nsx)s to policy "
                 "%(pol)s", {
                     'nsx': nsx_sg_id,
                     'pol': policy_id
                 })
        plugin._update_nsx_security_group_policies(policy_id, None, nsx_sg_id)
        with context_.session.begin(subtransactions=True):
            prop = context_.session.query(
                extended_secgroup.NsxExtendedSecurityGroupProperties).\
                filter_by(security_group_id=sg_id).one()
            prop[sg_policy.POLICY] = policy_id
        LOG.info("Done.")
Beispiel #5
0
 def get_nsgroup_id(self, sg_id):
     return nsx_db.get_nsx_security_group_id(self.context.session, sg_id)
Beispiel #6
0
 def get_nsgroup_id(self, sg_id):
     return nsx_db.get_nsx_security_group_id(
         self.context.session, sg_id)