Search in sources :

Example 6 with GetVncPortCommand

use of com.cloud.agent.api.GetVncPortCommand in project cloudstack by apache.

the class NotAValidCommand method testGetVncPortCommand.

@Test
public void testGetVncPortCommand() {
    final GetVncPortCommand vncPortCommand = new GetVncPortCommand(1l, "Test");
    final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(vncPortCommand, citrixResourceBase);
    verify(citrixResourceBase, times(1)).getConnection();
    assertFalse(answer.getResult());
}
Also used : RebootAnswer(com.cloud.agent.api.RebootAnswer) CreateAnswer(com.cloud.agent.api.storage.CreateAnswer) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Answer(com.cloud.agent.api.Answer) GetVncPortCommand(com.cloud.agent.api.GetVncPortCommand) Test(org.junit.Test)

Example 7 with GetVncPortCommand

use of com.cloud.agent.api.GetVncPortCommand in project cloudstack by apache.

the class LibvirtComputingResourceTest method testGetVncPortCommandLibvirtException.

@SuppressWarnings("unchecked")
@Test
public void testGetVncPortCommandLibvirtException() {
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    final GetVncPortCommand command = new GetVncPortCommand(1l, "host");
    when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    try {
        when(libvirtUtilitiesHelper.getConnectionByVmName(command.getName())).thenThrow(LibvirtException.class);
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, libvirtComputingResource);
    assertFalse(answer.getResult());
    verify(libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
    try {
        verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(command.getName());
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
}
Also used : AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Answer(com.cloud.agent.api.Answer) CheckRouterAnswer(com.cloud.agent.api.CheckRouterAnswer) LibvirtRequestWrapper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper) GetVncPortCommand(com.cloud.agent.api.GetVncPortCommand) LibvirtException(org.libvirt.LibvirtException) LibvirtUtilitiesHelper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper) Test(org.junit.Test)

Example 8 with GetVncPortCommand

use of com.cloud.agent.api.GetVncPortCommand in project cloudstack by apache.

the class AgentHookBase method onConsoleAccessAuthentication.

@Override
public AgentControlAnswer onConsoleAccessAuthentication(ConsoleAccessAuthenticationCommand cmd) {
    Long vmId = null;
    String ticketInUrl = cmd.getTicket();
    if (ticketInUrl == null) {
        s_logger.error("Access ticket could not be found, you could be running an old version of console proxy. vmId: " + cmd.getVmId());
        return new ConsoleAccessAuthenticationAnswer(cmd, false);
    }
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Console authentication. Ticket in url for " + cmd.getHost() + ":" + cmd.getPort() + "-" + cmd.getVmId() + " is " + ticketInUrl);
    }
    if (!cmd.isReauthenticating()) {
        String ticket = ConsoleProxyServlet.genAccessTicket(cmd.getHost(), cmd.getPort(), cmd.getSid(), cmd.getVmId());
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Console authentication. Ticket in 1 minute boundary for " + cmd.getHost() + ":" + cmd.getPort() + "-" + cmd.getVmId() + " is " + ticket);
        }
        if (!ticket.equals(ticketInUrl)) {
            Date now = new Date();
            // considering of minute round-up
            String minuteEarlyTicket = ConsoleProxyServlet.genAccessTicket(cmd.getHost(), cmd.getPort(), cmd.getSid(), cmd.getVmId(), new Date(now.getTime() - 60 * 1000));
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Console authentication. Ticket in 2-minute boundary for " + cmd.getHost() + ":" + cmd.getPort() + "-" + cmd.getVmId() + " is " + minuteEarlyTicket);
            }
            if (!minuteEarlyTicket.equals(ticketInUrl)) {
                s_logger.error("Access ticket expired or has been modified. vmId: " + cmd.getVmId() + "ticket in URL: " + ticketInUrl + ", tickets to check against: " + ticket + "," + minuteEarlyTicket);
                return new ConsoleAccessAuthenticationAnswer(cmd, false);
            }
        }
    }
    if (cmd.getVmId() != null && cmd.getVmId().isEmpty()) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Invalid vm id sent from proxy(happens when proxy session has terminated)");
        }
        return new ConsoleAccessAuthenticationAnswer(cmd, false);
    }
    VirtualMachine vm = _instanceDao.findByUuid(cmd.getVmId());
    if (vm == null) {
        vm = _instanceDao.findById(Long.parseLong(cmd.getVmId()));
    }
    if (vm == null) {
        s_logger.error("Invalid vm id " + cmd.getVmId() + " sent from console access authentication");
        return new ConsoleAccessAuthenticationAnswer(cmd, false);
    }
    if (vm.getHostId() == null) {
        s_logger.warn("VM " + vmId + " lost host info, failed authentication request");
        return new ConsoleAccessAuthenticationAnswer(cmd, false);
    }
    HostVO host = _hostDao.findById(vm.getHostId());
    if (host == null) {
        s_logger.warn("VM " + vmId + "'s host does not exist, fail authentication request");
        return new ConsoleAccessAuthenticationAnswer(cmd, false);
    }
    String sid = cmd.getSid();
    if (sid == null || !sid.equals(vm.getVncPassword())) {
        s_logger.warn("sid " + sid + " in url does not match stored sid.");
        return new ConsoleAccessAuthenticationAnswer(cmd, false);
    }
    if (cmd.isReauthenticating()) {
        ConsoleAccessAuthenticationAnswer authenticationAnswer = new ConsoleAccessAuthenticationAnswer(cmd, true);
        authenticationAnswer.setReauthenticating(true);
        s_logger.info("Re-authentication request, ask host " + vm.getHostId() + " for new console info");
        GetVncPortAnswer answer = (GetVncPortAnswer) _agentMgr.easySend(vm.getHostId(), new GetVncPortCommand(vm.getId(), vm.getInstanceName()));
        if (answer != null && answer.getResult()) {
            Ternary<String, String, String> parsedHostInfo = ConsoleProxyServlet.parseHostInfo(answer.getAddress());
            if (parsedHostInfo.second() != null && parsedHostInfo.third() != null) {
                s_logger.info("Re-authentication result. vm: " + vm.getId() + ", tunnel url: " + parsedHostInfo.second() + ", tunnel session: " + parsedHostInfo.third());
                authenticationAnswer.setTunnelUrl(parsedHostInfo.second());
                authenticationAnswer.setTunnelSession(parsedHostInfo.third());
            } else {
                s_logger.info("Re-authentication result. vm: " + vm.getId() + ", host address: " + parsedHostInfo.first() + ", port: " + answer.getPort());
                authenticationAnswer.setHost(parsedHostInfo.first());
                authenticationAnswer.setPort(answer.getPort());
            }
        } else {
            s_logger.warn("Re-authentication request failed");
            authenticationAnswer.setSuccess(false);
        }
        return authenticationAnswer;
    }
    return new ConsoleAccessAuthenticationAnswer(cmd, true);
}
Also used : GetVncPortAnswer(com.cloud.agent.api.GetVncPortAnswer) GetVncPortCommand(com.cloud.agent.api.GetVncPortCommand) ConsoleAccessAuthenticationAnswer(com.cloud.agent.api.ConsoleAccessAuthenticationAnswer) Date(java.util.Date) HostVO(com.cloud.host.HostVO) VirtualMachine(com.cloud.vm.VirtualMachine)

Aggregations

GetVncPortCommand (com.cloud.agent.api.GetVncPortCommand)8 Answer (com.cloud.agent.api.Answer)7 AttachIsoCommand (com.cloud.agent.api.AttachIsoCommand)3 BackupSnapshotCommand (com.cloud.agent.api.BackupSnapshotCommand)3 CheckHealthCommand (com.cloud.agent.api.CheckHealthCommand)3 CheckNetworkCommand (com.cloud.agent.api.CheckNetworkCommand)3 CreatePrivateTemplateFromSnapshotCommand (com.cloud.agent.api.CreatePrivateTemplateFromSnapshotCommand)3 CreatePrivateTemplateFromVolumeCommand (com.cloud.agent.api.CreatePrivateTemplateFromVolumeCommand)3 CreateStoragePoolCommand (com.cloud.agent.api.CreateStoragePoolCommand)3 CreateVolumeFromSnapshotCommand (com.cloud.agent.api.CreateVolumeFromSnapshotCommand)3 DeleteStoragePoolCommand (com.cloud.agent.api.DeleteStoragePoolCommand)3 GetHostStatsCommand (com.cloud.agent.api.GetHostStatsCommand)3 GetStorageStatsCommand (com.cloud.agent.api.GetStorageStatsCommand)3 GetVmStatsCommand (com.cloud.agent.api.GetVmStatsCommand)3 MaintainCommand (com.cloud.agent.api.MaintainCommand)3 ManageSnapshotCommand (com.cloud.agent.api.ManageSnapshotCommand)3 MigrateCommand (com.cloud.agent.api.MigrateCommand)3 ModifyStoragePoolCommand (com.cloud.agent.api.ModifyStoragePoolCommand)3 NetworkUsageCommand (com.cloud.agent.api.NetworkUsageCommand)3 PingTestCommand (com.cloud.agent.api.PingTestCommand)3