Search in sources :

Example 71 with Account

use of com.cloud.legacymodel.user.Account in project cosmic by MissionCriticalCloud.

the class NetworkACLServiceImpl method updateNetworkACL.

@Override
@ActionEvent(eventType = EventTypes.EVENT_NETWORK_ACL_UPDATE, eventDescription = "updating network acl", async = true)
public NetworkACL updateNetworkACL(final Long id, final String customId, final Boolean forDisplay, final String description, final String name) {
    final NetworkACLVO acl = _networkACLDao.findById(id);
    final Vpc vpc = _entityMgr.findById(Vpc.class, acl.getVpcId());
    final Account caller = CallContext.current().getCallingAccount();
    _accountMgr.checkAccess(caller, null, true, vpc);
    if (customId != null) {
        acl.setUuid(customId);
    }
    if (forDisplay != null) {
        acl.setDisplay(forDisplay);
    }
    if (description != null) {
        acl.setDescription(description);
    }
    if (name != null) {
        acl.setName(name);
    }
    _networkACLDao.update(id, acl);
    return _networkACLDao.findById(id);
}
Also used : Account(com.cloud.legacymodel.user.Account) Vpc(com.cloud.legacymodel.network.vpc.Vpc) ActionEvent(com.cloud.event.ActionEvent)

Example 72 with Account

use of com.cloud.legacymodel.user.Account in project cosmic by MissionCriticalCloud.

the class NetworkACLServiceImpl method listNetworkACLItems.

@Override
public Pair<List<? extends NetworkACLItem>, Integer> listNetworkACLItems(final ListNetworkACLsCmd cmd) {
    final Long networkId = cmd.getNetworkId();
    final Long id = cmd.getId();
    Long aclId = cmd.getAclId();
    final String trafficType = cmd.getTrafficType();
    final String protocol = cmd.getProtocol();
    final String action = cmd.getAction();
    final Map<String, String> tags = cmd.getTags();
    final Account caller = CallContext.current().getCallingAccount();
    final Filter filter = new Filter(NetworkACLItemVO.class, "id", false, cmd.getStartIndex(), cmd.getPageSizeVal());
    final SearchBuilder<NetworkACLItemVO> sb = _networkACLItemDao.createSearchBuilder();
    sb.and("id", sb.entity().getId(), Op.EQ);
    sb.and("aclId", sb.entity().getAclId(), Op.EQ);
    sb.and("trafficType", sb.entity().getTrafficType(), Op.EQ);
    sb.and("protocol", sb.entity().getProtocol(), Op.EQ);
    sb.and("action", sb.entity().getAction(), Op.EQ);
    if (tags != null && !tags.isEmpty()) {
        final SearchBuilder<ResourceTagVO> tagSearch = _resourceTagDao.createSearchBuilder();
        for (int count = 0; count < tags.size(); count++) {
            tagSearch.or().op("key" + String.valueOf(count), tagSearch.entity().getKey(), Op.EQ);
            tagSearch.and("value" + String.valueOf(count), tagSearch.entity().getValue(), Op.EQ);
            tagSearch.cp();
        }
        tagSearch.and("resourceType", tagSearch.entity().getResourceType(), Op.EQ);
        sb.groupBy(sb.entity().getId());
        sb.join("tagSearch", tagSearch, sb.entity().getId(), tagSearch.entity().getResourceId(), JoinBuilder.JoinType.INNER);
    }
    if (aclId == null) {
        // Join with network_acl table when aclId is not specified to list acl_items within permitted VPCs
        final SearchBuilder<NetworkACLVO> vpcSearch = _networkACLDao.createSearchBuilder();
        vpcSearch.and("vpcId", vpcSearch.entity().getVpcId(), Op.IN);
        sb.join("vpcSearch", vpcSearch, sb.entity().getAclId(), vpcSearch.entity().getId(), JoinBuilder.JoinType.INNER);
    }
    final SearchCriteria<NetworkACLItemVO> sc = sb.create();
    if (id != null) {
        sc.setParameters("id", id);
    }
    if (networkId != null) {
        final Network network = _networkDao.findById(networkId);
        aclId = network.getNetworkACLId();
        if (aclId == null) {
            // Return empty list
            return new Pair(new ArrayList<NetworkACLItem>(), 0);
        }
    }
    if (trafficType != null) {
        sc.setParameters("trafficType", trafficType);
    }
    if (aclId != null) {
        // Get VPC and check access
        final NetworkACL acl = _networkACLDao.findById(aclId);
        if (acl.getVpcId() != 0) {
            final Vpc vpc = _vpcDao.findById(acl.getVpcId());
            if (vpc == null) {
                throw new InvalidParameterValueException("Unable to find VPC associated with acl");
            }
            _accountMgr.checkAccess(caller, null, true, vpc);
        }
        sc.setParameters("aclId", aclId);
    } else {
        // ToDo: Add accountId to network_acl_item table for permission check
        // aclId is not specified
        // List permitted VPCs and filter aclItems
        final List<Long> permittedAccounts = new ArrayList<>();
        Long domainId = cmd.getDomainId();
        boolean isRecursive = cmd.isRecursive();
        final String accountName = cmd.getAccountName();
        final Long projectId = cmd.getProjectId();
        final boolean listAll = cmd.listAll();
        final Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<>(domainId, isRecursive, null);
        _accountMgr.buildACLSearchParameters(caller, id, accountName, projectId, permittedAccounts, domainIdRecursiveListProject, listAll, false);
        domainId = domainIdRecursiveListProject.first();
        isRecursive = domainIdRecursiveListProject.second();
        final ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
        final SearchBuilder<VpcVO> sbVpc = _vpcDao.createSearchBuilder();
        _accountMgr.buildACLSearchBuilder(sbVpc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
        final SearchCriteria<VpcVO> scVpc = sbVpc.create();
        _accountMgr.buildACLSearchCriteria(scVpc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
        final List<VpcVO> vpcs = _vpcDao.search(scVpc, null);
        final List<Long> vpcIds = new ArrayList<>();
        for (final VpcVO vpc : vpcs) {
            vpcIds.add(vpc.getId());
        }
        // Add vpc_id 0 to list acl_items in default ACL
        vpcIds.add(0L);
        sc.setJoinParameters("vpcSearch", "vpcId", vpcIds.toArray());
    }
    if (protocol != null) {
        sc.setParameters("protocol", protocol);
    }
    if (action != null) {
        sc.setParameters("action", action);
    }
    if (tags != null && !tags.isEmpty()) {
        int count = 0;
        sc.setJoinParameters("tagSearch", "resourceType", ResourceObjectType.NetworkACL.toString());
        for (final String key : tags.keySet()) {
            sc.setJoinParameters("tagSearch", "key" + String.valueOf(count), key);
            sc.setJoinParameters("tagSearch", "value" + String.valueOf(count), tags.get(key));
            count++;
        }
    }
    final Pair<List<NetworkACLItemVO>, Integer> result = _networkACLItemDao.searchAndCount(sc, filter);
    final List<NetworkACLItemVO> aclItemVOs = result.first();
    for (final NetworkACLItemVO item : aclItemVOs) {
        _networkACLItemDao.loadCidrs(item);
    }
    return new Pair<>(aclItemVOs, result.second());
}
Also used : Account(com.cloud.legacymodel.user.Account) Vpc(com.cloud.legacymodel.network.vpc.Vpc) ArrayList(java.util.ArrayList) NetworkACL(com.cloud.legacymodel.network.vpc.NetworkACL) NetworkACLItem(com.cloud.legacymodel.network.vpc.NetworkACLItem) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) Network(com.cloud.legacymodel.network.Network) ResourceTagVO(com.cloud.tags.ResourceTagVO) List(java.util.List) ArrayList(java.util.ArrayList) Pair(com.cloud.legacymodel.utils.Pair) Ternary(com.cloud.legacymodel.utils.Ternary) ListProjectResourcesCriteria(com.cloud.projects.Project.ListProjectResourcesCriteria) Filter(com.cloud.utils.db.Filter)

Example 73 with Account

use of com.cloud.legacymodel.user.Account in project cosmic by MissionCriticalCloud.

the class NetworkACLServiceImpl method replacePublicIpACL.

@Override
public boolean replacePublicIpACL(final long aclId, final long publicIpId) throws ResourceUnavailableException {
    final Account caller = CallContext.current().getCallingAccount();
    final IPAddressVO publicIp = _ipAddressDao.findById(publicIpId);
    if (publicIp == null) {
        throw new InvalidParameterValueException("Unable to find specified IP address");
    }
    final NetworkACL acl = _networkACLDao.findById(aclId);
    if (acl == null) {
        throw new InvalidParameterValueException("Unable to find specified NetworkACL");
    }
    if (publicIp.getVpcId() == null) {
        throw new InvalidParameterValueException("IP address is not part of a VPC: " + publicIp.getUuid());
    }
    if (aclId != NetworkACL.DEFAULT_DENY && aclId != NetworkACL.DEFAULT_ALLOW) {
        // ACL is not default DENY/ALLOW
        // ACL should be associated with a VPC
        final Vpc vpc = _entityMgr.findById(Vpc.class, acl.getVpcId());
        if (vpc == null) {
            throw new InvalidParameterValueException("Unable to find Vpc associated with the NetworkACL");
        }
        _accountMgr.checkAccess(caller, null, true, vpc);
        if (!publicIp.getVpcId().equals(acl.getVpcId())) {
            throw new InvalidParameterValueException("IP address: " + publicIpId + " and ACL: " + aclId + " do not belong to the same VPC");
        }
    }
    return _networkAclMgr.replacePublicIpACL(acl, publicIp);
}
Also used : Account(com.cloud.legacymodel.user.Account) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) Vpc(com.cloud.legacymodel.network.vpc.Vpc) IPAddressVO(com.cloud.network.dao.IPAddressVO) NetworkACL(com.cloud.legacymodel.network.vpc.NetworkACL)

Example 74 with Account

use of com.cloud.legacymodel.user.Account in project cosmic by MissionCriticalCloud.

the class NetworkACLServiceImpl method replaceNetworkACL.

@Override
public boolean replaceNetworkACL(final long aclId, final long networkId) throws ResourceUnavailableException {
    final Account caller = CallContext.current().getCallingAccount();
    final NetworkVO network = _networkDao.findById(networkId);
    if (network == null) {
        throw new InvalidParameterValueException("Unable to find specified Network");
    }
    final NetworkACL acl = _networkACLDao.findById(aclId);
    if (acl == null) {
        throw new InvalidParameterValueException("Unable to find specified NetworkACL");
    }
    if (network.getVpcId() == null) {
        throw new InvalidParameterValueException("Network is not part of a VPC: " + network.getUuid());
    }
    if (network.getTrafficType() != TrafficType.Guest) {
        throw new InvalidParameterValueException("Network ACL can be created just for networks of type " + TrafficType.Guest);
    }
    if (aclId != NetworkACL.DEFAULT_DENY && aclId != NetworkACL.DEFAULT_ALLOW) {
        // ACL is not default DENY/ALLOW
        // ACL should be associated with a VPC
        final Vpc vpc = _entityMgr.findById(Vpc.class, acl.getVpcId());
        if (vpc == null) {
            throw new InvalidParameterValueException("Unable to find Vpc associated with the NetworkACL");
        }
        _accountMgr.checkAccess(caller, null, true, vpc);
        if (!network.getVpcId().equals(acl.getVpcId())) {
            throw new InvalidParameterValueException("Network: " + networkId + " and ACL: " + aclId + " do not belong to the same VPC");
        }
    }
    return _networkAclMgr.replaceNetworkACL(acl, network);
}
Also used : Account(com.cloud.legacymodel.user.Account) NetworkVO(com.cloud.network.dao.NetworkVO) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) Vpc(com.cloud.legacymodel.network.vpc.Vpc) NetworkACL(com.cloud.legacymodel.network.vpc.NetworkACL)

Example 75 with Account

use of com.cloud.legacymodel.user.Account in project cosmic by MissionCriticalCloud.

the class FirewallManagerImpl method revokeIngressFirewallRule.

@Override
public boolean revokeIngressFirewallRule(final long ruleId, final boolean apply) {
    final Account caller = CallContext.current().getCallingAccount();
    final long userId = CallContext.current().getCallingUserId();
    return revokeFirewallRule(ruleId, apply, caller, userId);
}
Also used : Account(com.cloud.legacymodel.user.Account)

Aggregations

Account (com.cloud.legacymodel.user.Account)435 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)229 ActionEvent (com.cloud.event.ActionEvent)120 ArrayList (java.util.ArrayList)103 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)98 PermissionDeniedException (com.cloud.legacymodel.exceptions.PermissionDeniedException)78 User (com.cloud.legacymodel.user.User)73 DB (com.cloud.utils.db.DB)59 List (java.util.List)58 Pair (com.cloud.legacymodel.utils.Pair)53 Network (com.cloud.legacymodel.network.Network)48 CallContext (com.cloud.context.CallContext)47 DomainVO (com.cloud.domain.DomainVO)47 UserAccount (com.cloud.legacymodel.user.UserAccount)47 Filter (com.cloud.utils.db.Filter)47 TransactionStatus (com.cloud.utils.db.TransactionStatus)40 Domain (com.cloud.legacymodel.domain.Domain)39 ResourceUnavailableException (com.cloud.legacymodel.exceptions.ResourceUnavailableException)37 Test (org.junit.Test)36 Ternary (com.cloud.legacymodel.utils.Ternary)34