Skip to content

Commit

Permalink
CHE-4058 Removed usage name of a current user as a namespace (eclipse…
Browse files Browse the repository at this point in the history
…-che#5207)

Also, it contains fixing logging errors while fetching user by namespace
  • Loading branch information
Sergii Leshchenko authored Jun 2, 2017
1 parent 6d41a39 commit ac8d44e
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ public class DockerContainerNameGenerator implements ContainerNameGenerator {
* unique workspace id, see more (@link WorkspaceConfig#getId)
* @param machineId
* unique machine id, see more {@link Machine#getId()}
* @param userName
* @param ownerName
* name of the user who is docker container owner
* @param machineName
* name of the workspace machine, see more {@link MachineConfig#getName()}
*/
@Override
public String generateContainerName(String workspaceId, String machineId, String userName, String machineName) {
String containerName = workspaceId + '_' + machineId + '_' + userName + '_' + machineName;
public String generateContainerName(String workspaceId, String machineId, String ownerName, String machineName) {
String containerName = workspaceId + '_' + machineId + '_' + ownerName + '_' + machineName;
return containerName.toLowerCase().replaceAll("[^a-z0-9_-]+", "");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ public MachineProviderImpl(DockerConnectorProvider dockerProvider,
}

@Override
public Instance startService(String namespace,
public Instance startService(String ownerName,
String workspaceId,
String envName,
String machineName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,11 @@ public void onEvent(WorkspaceCreatedEvent workspaceCreatedEvent) {
try {
final User user = userManager.getByName(workspaceCreatedEvent.getWorkspace().getNamespace());
userId = user.getId();
} catch (NotFoundException | ServerException e) {
LOG.error("Unable to get owner of the workspace {} with namespace {}", workspaceCreatedEvent.getWorkspace().getId(), workspaceCreatedEvent.getWorkspace().getNamespace());
} catch (NotFoundException ignored) {
// namespace can be different from username
return;
} catch (ServerException e) {
LOG.warn("Unable to get owner of the workspace {} with namespace {}", workspaceCreatedEvent.getWorkspace().getId(), workspaceCreatedEvent.getWorkspace().getNamespace());
return;
}

Expand All @@ -94,8 +97,8 @@ public void onEvent(WorkspaceCreatedEvent workspaceCreatedEvent) {
workspaceCreatedEvent.getWorkspace().getId());
} catch (ServerException | ConflictException e) {
// Conflict shouldn't happen as workspace id is new each time.
LOG.error("Unable to generate a default ssh pair for the workspace with ID {}",
workspaceCreatedEvent.getWorkspace().getId(), e);
LOG.warn("Unable to generate a default ssh pair for the workspace with ID {}",
workspaceCreatedEvent.getWorkspace().getId(), e);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,9 @@ public List<Instance> start(String workspaceId,
// add random chars to ensure that old environments that weren't removed by some reason won't prevent start
String networkId = NameGenerator.generate(workspaceId + "_", 16);

String namespace = EnvironmentContext.getCurrent().getSubject().getUserName();
String ownerName = EnvironmentContext.getCurrent().getSubject().getUserName();

initializeEnvironment(namespace,
initializeEnvironment(ownerName,
workspaceId,
envName,
environment,
Expand All @@ -265,7 +265,7 @@ public List<Instance> start(String workspaceId,
throw new ServerException("Agent 'org.eclipse.che.ws-agent' is not found in any of environment machines");
}

startEnvironmentQueue(namespace,
startEnvironmentQueue(ownerName,
workspaceId,
devMachineName,
networkId,
Expand Down Expand Up @@ -377,7 +377,6 @@ public Instance startMachine(String workspaceId,
}
}
final String creator = EnvironmentContext.getCurrent().getSubject().getUserId();
final String namespace = EnvironmentContext.getCurrent().getSubject().getUserName();

MachineImpl machine = MachineImpl.builder()
.setConfig(machineConfig)
Expand All @@ -392,7 +391,8 @@ public Instance startMachine(String workspaceId,
// needed to reuse startInstance method and
// create machine instances by different implementation-specific providers
CheServiceImpl service = machineConfigToService(machineConfig);
normalize(namespace,
final String ownerName = EnvironmentContext.getCurrent().getSubject().getUserName();
normalize(ownerName,
workspaceId,
machineConfig.getName(),
service);
Expand All @@ -401,15 +401,15 @@ public Instance startMachine(String workspaceId,
machineStarter = (machineLogger, machineSource) -> {
CheServiceImpl serviceWithNormalizedSource = normalizeServiceSource(service, machineSource);

normalize(namespace,
normalize(ownerName,
workspaceId,
machineConfig.getName(),
serviceWithNormalizedSource);

infrastructureProvisioner.provision(new ExtendedMachineImpl().withAgents(agents),
serviceWithNormalizedSource);

return machineProvider.startService(namespace,
return machineProvider.startService(ownerName,
workspaceId,
environmentHolder.name,
machineConfig.getName(),
Expand Down Expand Up @@ -559,7 +559,7 @@ public void removeSnapshot(SnapshotImpl snapshot) throws ServerException, NotFou
instanceProvider.removeInstanceSnapshot(snapshot.getMachineSource());
}

private void initializeEnvironment(String namespace,
private void initializeEnvironment(String ownerName,
String workspaceId,
String envName,
EnvironmentImpl envConfig,
Expand All @@ -575,7 +575,7 @@ private void initializeEnvironment(String namespace,

infrastructureProvisioner.provision(envConfig, internalEnv);

normalize(namespace,
normalize(ownerName,
workspaceId,
internalEnv);

Expand Down Expand Up @@ -618,13 +618,13 @@ private void addAgentsProvidedServers(MachineImpl machine, List<String> agentKey
}
}

private void normalize(String namespace,
private void normalize(String ownerName,
String workspaceId,
CheServicesEnvironmentImpl environment) throws ServerException {

Map<String, CheServiceImpl> services = environment.getServices();
for (Map.Entry<String, CheServiceImpl> serviceEntry : services.entrySet()) {
normalize(namespace,
normalize(ownerName,
workspaceId,
serviceEntry.getKey(),
serviceEntry.getValue());
Expand Down Expand Up @@ -694,7 +694,7 @@ void normalizeLinks(CheServiceImpl serviceToNormalizeLinks, Map<String, CheServi
}).collect(toList()));
}

private void normalize(String namespace,
private void normalize(String ownerName,
String workspaceId,
String machineName,
CheServiceImpl service) throws ServerException {
Expand All @@ -718,14 +718,14 @@ private void normalize(String namespace,

service.setContainerName(containerNameGenerator.generateContainerName(workspaceId,
service.getId(),
namespace,
ownerName,
machineName));
}

/**
* Starts all machine from machine queue of environment.
*/
private void startEnvironmentQueue(String namespace,
private void startEnvironmentQueue(String ownerName,
String workspaceId,
String devMachineName,
String networkId,
Expand Down Expand Up @@ -782,7 +782,7 @@ private void startEnvironmentQueue(String namespace,
// create machine instances by different implementation-specific providers
MachineStarter machineStarter = (machineLogger, machineSource) -> {
CheServiceImpl serviceWithNormalizedSource = normalizeServiceSource(service, machineSource);
return machineProvider.startService(namespace,
return machineProvider.startService(ownerName,
workspaceId,
envName,
finalMachineName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ public interface ContainerNameGenerator {
* unique workspace id, see more (@link WorkspaceConfig#getId)
* @param machineId
* unique machine id, see more {@link Machine#getId()}
* @param namespace
* @param ownerName
* name of the user who is docker container owner
* @param machineName
* name of the workspace machine, see more {@link MachineConfig#getName()}
*/
String generateContainerName(String workspaceId,
String machineId,
String namespace,
String ownerName,
String machineName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public interface MachineInstanceProvider {
/**
* Create docker container from compose service definition.
*
* @param namespace
* namespace of workspace that owns provided service
* @param ownerName
* name of user who starts provided service
* @param workspaceId
* ID of workspace that owns provided service
* @param envName
Expand All @@ -44,7 +44,7 @@ public interface MachineInstanceProvider {
* @throws ServerException
* if any error occurs
*/
Instance startService(String namespace,
Instance startService(String ownerName,
String workspaceId,
String envName,
String machineName,
Expand Down

0 comments on commit ac8d44e

Please sign in to comment.