private ServerChecker getChecker(String serverRef, Server server) throws InfrastructureException { // TODO replace with correct behaviour // workaround needed because we don't have server readiness check in the model // Create server readiness endpoint URL URL url; String token; try { String serverUrl = server.getUrl(); if ("terminal".equals(serverRef)) { serverUrl = serverUrl.replaceFirst("^ws", "http").replaceFirst("/pty$", "/"); } if ("wsagent/http".equals(serverRef) && !serverUrl.endsWith("/")) { // add trailing slash if it is not present serverUrl = serverUrl + '/'; } token = machineTokenProvider.getToken( runtimeIdentity.getOwnerId(), runtimeIdentity.getWorkspaceId()); url = UriBuilder.fromUri(serverUrl).build().toURL(); } catch (MalformedURLException e) { throw new InternalInfrastructureException( "Server " + serverRef + " URL is invalid. Error: " + e.getMessage(), e); } return doCreateChecker(url, serverRef, token); }
@Override public Pair<String, String> get(RuntimeIdentity runtimeIdentity) throws MachineTokenException { return Pair.of( MACHINE_TOKEN, machineTokenProvider.getToken( runtimeIdentity.getOwnerId(), runtimeIdentity.getWorkspaceId())); } }
singletonMap( HttpHeaders.AUTHORIZATION, "Bearer " + machineTokenProvider.getToken(userId, workspaceId)), successThreshold, 3,
/** * Gets information about started process. * * @param workspaceId workspace to get process * @param pid pid of started process * @return process response DTO * @throws ServerException when get of the process is failed */ public GetProcessResponseDto getProcess(String workspaceId, int pid) throws ServerException { try { return requestFactory .fromUrl(serverEndpoint + "/" + pid) .addQueryParam("token", machineTokenProvider.getToken(workspaceId)) .setAuthorizationHeader("none") // to prevent sending KC token .useGetMethod() .request() .asDto(GetProcessResponseDto.class); } catch (IOException | ApiException | MachineTokenException e) { throw new ServerException(e); } }
/** * Kills started process. * * @param workspaceId workspace kill process * @param pid pid of started process * @return kill process response DTO * @throws ServerException when kill of the process is failed */ public ProcessKillResponseDto killProcess(String workspaceId, int pid) throws ServerException { try { return requestFactory .fromUrl(serverEndpoint + "/" + pid) .useDeleteMethod() .addQueryParam("token", machineTokenProvider.getToken(workspaceId)) .setAuthorizationHeader("none") // to prevent sending KC token .request() .asDto(ProcessKillResponseDto.class); } catch (IOException | ApiException | MachineTokenException e) { throw new ServerException(e); } } }
private WorkspaceDto asDtoWithLinksAndToken(Workspace workspace) throws ServerException { WorkspaceDto workspaceDto = asDto(workspace).withLinks(linksGenerator.genLinks(workspace, getServiceContext())); RuntimeDto runtimeDto = workspaceDto.getRuntime(); if (runtimeDto != null) { try { runtimeDto.setMachineToken(machineTokenProvider.getToken(workspace.getId())); } catch (MachineAccessForbidden e) { // set runtime to null since user doesn't have the required permissions workspaceDto.setRuntime(null); } catch (MachineTokenException e) { throw new ServerException(e.getMessage(), e); } } return workspaceDto; }
/** * Starts a process within a given command. * * @param workspaceId workspace to run command * @param command command to execute * @param name command name * @param type command type * @return start process response DTO * @throws ServerException when submit of the process is failed */ public ProcessStartResponseDto startProcess( String workspaceId, String command, String name, String type) throws ServerException { ProcessStartRequestDto commandDto = newDto(ProcessStartRequestDto.class).withCommandLine(command).withName(name).withType(type); try { return requestFactory .fromUrl(serverEndpoint) .addQueryParam("token", machineTokenProvider.getToken(workspaceId)) .setAuthorizationHeader("none") // to prevent sending KC token .usePostMethod() .setBody(commandDto) .request() .asDto(ProcessStartResponseDto.class); } catch (IOException | ApiException | MachineTokenException e) { throw new ServerException(e); } }