Search in sources :

Example 16 with Domain

use of com.cloud.domain.Domain in project cloudstack by apache.

the class DefaultLoginAPIAuthenticatorCmd method authenticate.

@Override
public String authenticate(String command, Map<String, Object[]> params, HttpSession session, InetAddress remoteAddress, String responseType, StringBuilder auditTrailSb, final HttpServletRequest req, final HttpServletResponse resp) throws ServerApiException {
    // Disallow non POST requests
    if (HTTPMethod.valueOf(req.getMethod()) != HTTPMethod.POST) {
        throw new ServerApiException(ApiErrorCode.METHOD_NOT_ALLOWED, "Please use HTTP POST to authenticate using this API");
    }
    // FIXME: ported from ApiServlet, refactor and cleanup
    final String[] username = (String[]) params.get(ApiConstants.USERNAME);
    final String[] password = (String[]) params.get(ApiConstants.PASSWORD);
    String[] domainIdArr = (String[]) params.get(ApiConstants.DOMAIN_ID);
    if (domainIdArr == null) {
        domainIdArr = (String[]) params.get(ApiConstants.DOMAIN__ID);
    }
    final String[] domainName = (String[]) params.get(ApiConstants.DOMAIN);
    Long domainId = null;
    if ((domainIdArr != null) && (domainIdArr.length > 0)) {
        try {
            //check if UUID is passed in for domain
            domainId = _apiServer.fetchDomainId(domainIdArr[0]);
            if (domainId == null) {
                domainId = Long.parseLong(domainIdArr[0]);
            }
            // building the params for POST call
            auditTrailSb.append(" domainid=" + domainId);
        } catch (final NumberFormatException e) {
            s_logger.warn("Invalid domain id entered by user");
            auditTrailSb.append(" " + HttpServletResponse.SC_UNAUTHORIZED + " " + "Invalid domain id entered, please enter a valid one");
            throw new ServerApiException(ApiErrorCode.UNAUTHORIZED, _apiServer.getSerializedApiError(HttpServletResponse.SC_UNAUTHORIZED, "Invalid domain id entered, please enter a valid one", params, responseType));
        }
    }
    String domain = null;
    if (domainName != null) {
        domain = domainName[0];
        auditTrailSb.append(" domain=" + domain);
        if (domain != null) {
            // ensure domain starts with '/' and ends with '/'
            if (!domain.endsWith("/")) {
                domain += '/';
            }
            if (!domain.startsWith("/")) {
                domain = "/" + domain;
            }
        }
    }
    String serializedResponse = null;
    if (username != null) {
        final String pwd = ((password == null) ? null : password[0]);
        try {
            final Domain userDomain = _domainService.findDomainByIdOrPath(domainId, domain);
            if (userDomain != null) {
                domainId = userDomain.getId();
            } else {
                throw new CloudAuthenticationException("Unable to find the domain from the path " + domain);
            }
            final UserAccount userAccount = _accountService.getActiveUserAccount(username[0], domainId);
            if (userAccount != null && User.Source.SAML2 == userAccount.getSource()) {
                throw new CloudAuthenticationException("User is not allowed CloudStack login");
            }
            return ApiResponseSerializer.toSerializedString(_apiServer.loginUser(session, username[0], pwd, domainId, domain, remoteAddress, params), responseType);
        } catch (final CloudAuthenticationException ex) {
            // TODO: fall through to API key, or just fail here w/ auth error? (HTTP 401)
            try {
                session.invalidate();
            } catch (final IllegalStateException ise) {
            }
            auditTrailSb.append(" " + ApiErrorCode.ACCOUNT_ERROR + " " + ex.getMessage() != null ? ex.getMessage() : "failed to authenticate user, check if username/password are correct");
            serializedResponse = _apiServer.getSerializedApiError(ApiErrorCode.ACCOUNT_ERROR.getHttpCode(), ex.getMessage() != null ? ex.getMessage() : "failed to authenticate user, check if username/password are correct", params, responseType);
        }
    }
    // We should not reach here and if we do we throw an exception
    throw new ServerApiException(ApiErrorCode.ACCOUNT_ERROR, serializedResponse);
}
Also used : ServerApiException(org.apache.cloudstack.api.ServerApiException) CloudAuthenticationException(com.cloud.exception.CloudAuthenticationException) Domain(com.cloud.domain.Domain) UserAccount(com.cloud.user.UserAccount)

Example 17 with Domain

use of com.cloud.domain.Domain in project cloudstack by apache.

the class ApiResponseHelper method createLBHealthCheckPolicyResponse.

@Override
public LBHealthCheckResponse createLBHealthCheckPolicyResponse(HealthCheckPolicy healthcheckPolicy, LoadBalancer lb) {
    LBHealthCheckResponse hcResponse = new LBHealthCheckResponse();
    hcResponse.setlbRuleId(lb.getUuid());
    Account accountTemp = ApiDBUtils.findAccountById(lb.getAccountId());
    if (accountTemp != null) {
        hcResponse.setAccountName(accountTemp.getAccountName());
        Domain domain = ApiDBUtils.findDomainById(accountTemp.getDomainId());
        if (domain != null) {
            hcResponse.setDomainId(domain.getUuid());
            hcResponse.setDomainName(domain.getName());
        }
    }
    List<LBHealthCheckPolicyResponse> responses = new ArrayList<LBHealthCheckPolicyResponse>();
    LBHealthCheckPolicyResponse ruleResponse = new LBHealthCheckPolicyResponse(healthcheckPolicy);
    responses.add(ruleResponse);
    hcResponse.setRules(responses);
    hcResponse.setObjectName("healthcheckpolicies");
    return hcResponse;
}
Also used : ProjectAccount(com.cloud.projects.ProjectAccount) UserAccount(com.cloud.user.UserAccount) Account(com.cloud.user.Account) LBHealthCheckPolicyResponse(org.apache.cloudstack.api.response.LBHealthCheckPolicyResponse) ArrayList(java.util.ArrayList) LBHealthCheckResponse(org.apache.cloudstack.api.response.LBHealthCheckResponse) Domain(com.cloud.domain.Domain)

Example 18 with Domain

use of com.cloud.domain.Domain in project cloudstack by apache.

the class NetworkServiceImpl method listDomainSpecificNetworksByDomainPath.

private List<NetworkVO> listDomainSpecificNetworksByDomainPath(SearchCriteria<NetworkVO> sc, Filter searchFilter, String path, boolean isRecursive) {
    Set<Long> allowedDomains = new HashSet<Long>();
    if (path != null) {
        if (isRecursive) {
            allowedDomains = _domainMgr.getDomainChildrenIds(path);
        } else {
            Domain domain = _domainDao.findDomainByPath(path);
            allowedDomains.add(domain.getId());
        }
    }
    List<Long> networkIds = new ArrayList<Long>();
    List<NetworkDomainVO> maps = _networkDomainDao.listDomainNetworkMapByDomain(allowedDomains.toArray());
    for (NetworkDomainVO map : maps) {
        networkIds.add(map.getNetworkId());
    }
    if (!networkIds.isEmpty()) {
        SearchCriteria<NetworkVO> domainSC = _networksDao.createSearchCriteria();
        domainSC.addAnd("id", SearchCriteria.Op.IN, networkIds.toArray());
        domainSC.addAnd("aclType", SearchCriteria.Op.EQ, ACLType.Domain.toString());
        sc.addAnd("id", SearchCriteria.Op.SC, domainSC);
        return _networksDao.search(sc, searchFilter);
    } else {
        return new ArrayList<NetworkVO>();
    }
}
Also used : PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) NetworkVO(com.cloud.network.dao.NetworkVO) ArrayList(java.util.ArrayList) NetworkDomainVO(com.cloud.network.dao.NetworkDomainVO) Domain(com.cloud.domain.Domain) HashSet(java.util.HashSet)

Example 19 with Domain

use of com.cloud.domain.Domain in project cloudstack by apache.

the class AddVpnUserCmd method execute.

@Override
public void execute() {
    VpnUser vpnUser = _entityMgr.findById(VpnUser.class, getEntityId());
    Account account = _entityMgr.findById(Account.class, vpnUser.getAccountId());
    try {
        if (!_ravService.applyVpnUsers(vpnUser.getAccountId(), userName)) {
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to add vpn user");
        }
    } catch (Exception ex) {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to add vpn user due to resource unavailable");
    }
    VpnUsersResponse vpnResponse = new VpnUsersResponse();
    vpnResponse.setId(vpnUser.getUuid());
    vpnResponse.setUserName(vpnUser.getUsername());
    vpnResponse.setAccountName(account.getAccountName());
    Domain domain = _entityMgr.findById(Domain.class, account.getDomainId());
    if (domain != null) {
        vpnResponse.setDomainId(domain.getUuid());
        vpnResponse.setDomainName(domain.getName());
    }
    vpnResponse.setResponseName(getCommandName());
    vpnResponse.setObjectName("vpnuser");
    setResponseObject(vpnResponse);
}
Also used : Account(com.cloud.user.Account) VpnUser(com.cloud.network.VpnUser) ServerApiException(org.apache.cloudstack.api.ServerApiException) Domain(com.cloud.domain.Domain) VpnUsersResponse(org.apache.cloudstack.api.response.VpnUsersResponse) ServerApiException(org.apache.cloudstack.api.ServerApiException)

Example 20 with Domain

use of com.cloud.domain.Domain in project cloudstack by apache.

the class BrocadeVcsGuestNetworkGuruTest method testReserve.

@Test
public void testReserve() throws InsufficientVirtualNetworkCapacityException, URISyntaxException, InsufficientAddressCapacityException {
    final NetworkVO network = mock(NetworkVO.class);
    when(network.getName()).thenReturn("testnetwork");
    when(network.getState()).thenReturn(State.Implementing);
    when(network.getPhysicalNetworkId()).thenReturn(NETWORK_ID);
    when(network.getBroadcastUri()).thenReturn(new URI("vlan://14"));
    when(network.getDataCenterId()).thenReturn(NETWORK_ID);
    final NicProfile nic = mock(NicProfile.class);
    when(nic.getMacAddress()).thenReturn("macaddress");
    when(nic.getReservationStrategy()).thenReturn(ReservationStrategy.Start);
    final VirtualMachineProfile vmProfile = mock(VirtualMachineProfile.class);
    final DeployDestination dest = mock(DeployDestination.class);
    final DataCenterVO dc = mock(DataCenterVO.class);
    when(dest.getDataCenter()).thenReturn(dc);
    when(dcdao.findById((long) anyInt())).thenReturn(dc);
    final HostVO brocadeHost = mock(HostVO.class);
    when(hostdao.findById(anyLong())).thenReturn(brocadeHost);
    when(brocadeHost.getId()).thenReturn(NETWORK_ID);
    when(netmodel.findPhysicalNetworkId(anyLong(), (String) any(), (TrafficType) any())).thenReturn(NETWORK_ID);
    final BrocadeVcsDeviceVO brocadeDevice = mock(BrocadeVcsDeviceVO.class);
    when(brocadeDevice.getHostId()).thenReturn(NETWORK_ID);
    List<BrocadeVcsDeviceVO> devices = new ArrayList();
    devices.add(brocadeDevice);
    when(vcsdao.listByPhysicalNetwork(anyLong())).thenReturn(devices);
    final Domain dom = mock(Domain.class);
    when(dom.getName()).thenReturn("domain");
    final Account acc = mock(Account.class);
    when(acc.getAccountName()).thenReturn("accountname");
    final ReservationContext res = mock(ReservationContext.class);
    when(res.getDomain()).thenReturn(dom);
    when(res.getAccount()).thenReturn(acc);
    final AssociateMacToNetworkAnswer answer = mock(AssociateMacToNetworkAnswer.class);
    when(answer.getResult()).thenReturn(true);
    when(agentmgr.easySend(eq(NETWORK_ID), (Command) any())).thenReturn(answer);
    guru.reserve(nic, network, vmProfile, dest, res);
    verify(agentmgr, times(1)).easySend(eq(NETWORK_ID), (Command) any());
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) Account(com.cloud.user.Account) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) NetworkVO(com.cloud.network.dao.NetworkVO) ArrayList(java.util.ArrayList) NicProfile(com.cloud.vm.NicProfile) URI(java.net.URI) HostVO(com.cloud.host.HostVO) BrocadeVcsDeviceVO(com.cloud.network.BrocadeVcsDeviceVO) ReservationContext(com.cloud.vm.ReservationContext) AssociateMacToNetworkAnswer(com.cloud.agent.api.AssociateMacToNetworkAnswer) DeployDestination(com.cloud.deploy.DeployDestination) VirtualMachineProfile(com.cloud.vm.VirtualMachineProfile) Domain(com.cloud.domain.Domain) Test(org.junit.Test)

Aggregations

Domain (com.cloud.domain.Domain)81 Account (com.cloud.user.Account)42 ArrayList (java.util.ArrayList)23 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)20 Test (org.junit.Test)20 DeployDestination (com.cloud.deploy.DeployDestination)17 Network (com.cloud.network.Network)17 ReservationContext (com.cloud.vm.ReservationContext)17 DataCenter (com.cloud.dc.DataCenter)16 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)16 NetworkOffering (com.cloud.offering.NetworkOffering)16 HostVO (com.cloud.host.HostVO)15 NetworkVO (com.cloud.network.dao.NetworkVO)15 UserAccount (com.cloud.user.UserAccount)15 URI (java.net.URI)12 DomainVO (com.cloud.domain.DomainVO)11 ProjectAccount (com.cloud.projects.ProjectAccount)11 Project (com.cloud.projects.Project)10 NiciraNvpDeviceVO (com.cloud.network.NiciraNvpDeviceVO)8 DB (com.cloud.utils.db.DB)8