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);
}
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;
}
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>();
}
}
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);
}
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());
}
Aggregations