use of org.eclipse.che.api.workspace.server.token.MachineTokenException in project che-server by eclipse-che.
the class WorkspaceService method asDtoWithLinksAndToken.
private WorkspaceDto asDtoWithLinksAndToken(WorkspaceImpl 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;
}
use of org.eclipse.che.api.workspace.server.token.MachineTokenException in project devspaces-images by redhat-developer.
the class WsAgentServerLivenessProbeConfigFactory method get.
@Override
public HttpProbeConfig get(String userId, String workspaceId, Server server) throws InternalInfrastructureException {
try {
// add check path
URI uri = UriBuilder.fromUri(server.getUrl()).path("/liveness").build();
int port;
if (uri.getPort() == -1) {
if ("http".equals(uri.getScheme())) {
port = 80;
} else {
port = 443;
}
} else {
port = uri.getPort();
}
return new HttpProbeConfig(port, uri.getHost(), uri.getScheme(), uri.getPath(), singletonMap(HttpHeaders.AUTHORIZATION, "Bearer " + machineTokenProvider.getToken(userId, workspaceId)), successThreshold, 3, 120, 10, 10);
} catch (MachineTokenException e) {
throw new InternalInfrastructureException("Failed to retrieve workspace token for ws-agent server liveness probe. Error: " + e.getMessage());
} catch (UriBuilderException e) {
throw new InternalInfrastructureException("Wsagent server liveness probe url is invalid. Error: " + e.getMessage());
}
}
use of org.eclipse.che.api.workspace.server.token.MachineTokenException in project che-server by eclipse-che.
the class MachineTokenRegistry method createToken.
/**
* Creates new token with given data.
*/
private String createToken(String userId, String workspaceId) throws MachineTokenException {
try {
final PrivateKey privateKey = signatureKeyManager.getOrCreateKeyPair(workspaceId).getPrivate();
final User user = userManager.getById(userId);
final Map<String, Object> header = new HashMap<>(2);
header.put("kind", MACHINE_TOKEN_KIND);
header.put("kid", workspaceId);
final Map<String, Object> claims = new HashMap<>();
// to ensure that each token is unique
claims.put(Claims.ID, UUID.randomUUID().toString());
claims.put(Constants.USER_ID_CLAIM, userId);
claims.put(Constants.USER_NAME_CLAIM, user.getName());
claims.put(Constants.WORKSPACE_ID_CLAIM, workspaceId);
// jwtproxy required claims
claims.put(Claims.ISSUER, "wsmaster");
claims.put(Claims.AUDIENCE, workspaceId);
claims.put(Claims.EXPIRATION, Instant.now().plus(365, DAYS).getEpochSecond());
// always
claims.put(Claims.NOT_BEFORE, -1);
claims.put(Claims.ISSUED_AT, Instant.now().getEpochSecond());
final String token = Jwts.builder().setClaims(claims).setHeader(header).signWith(RS256, privateKey).compact();
tokens.put(workspaceId, userId, token);
return token;
} catch (SignatureKeyManagerException | NotFoundException | ServerException ex) {
throw new MachineTokenException(format("Failed to generate machine token for user '%s' and workspace '%s'. Cause: '%s'", userId, workspaceId, ex.getMessage()), ex);
}
}
use of org.eclipse.che.api.workspace.server.token.MachineTokenException in project che-server by eclipse-che.
the class WsAgentServerLivenessProbeConfigFactory method get.
@Override
public HttpProbeConfig get(String userId, String workspaceId, Server server) throws InternalInfrastructureException {
try {
// add check path
URI uri = UriBuilder.fromUri(server.getUrl()).path("/liveness").build();
int port;
if (uri.getPort() == -1) {
if ("http".equals(uri.getScheme())) {
port = 80;
} else {
port = 443;
}
} else {
port = uri.getPort();
}
return new HttpProbeConfig(port, uri.getHost(), uri.getScheme(), uri.getPath(), singletonMap(HttpHeaders.AUTHORIZATION, "Bearer " + machineTokenProvider.getToken(userId, workspaceId)), successThreshold, 3, 120, 10, 10);
} catch (MachineTokenException e) {
throw new InternalInfrastructureException("Failed to retrieve workspace token for ws-agent server liveness probe. Error: " + e.getMessage());
} catch (UriBuilderException e) {
throw new InternalInfrastructureException("Wsagent server liveness probe url is invalid. Error: " + e.getMessage());
}
}
use of org.eclipse.che.api.workspace.server.token.MachineTokenException in project devspaces-images by redhat-developer.
the class WorkspaceService method asDtoWithLinksAndToken.
private WorkspaceDto asDtoWithLinksAndToken(WorkspaceImpl 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;
}
Aggregations