use of org.eclipse.che.api.core.model.workspace.config.ServerConfig in project devspaces-images by redhat-developer.
the class KubernetesServerExposer method exposeSecureServers.
private void exposeSecureServers(Map<String, ServerConfig> securedServers, Map<String, ServicePort> securedPorts) throws InfrastructureException {
if (securedPorts.isEmpty()) {
return;
}
Optional<Service> secureService = secureServerExposer.createService(securedPorts.values(), pod, machineName, securedServers);
String secureServiceName = secureService.map(s -> {
String n = s.getMetadata().getName();
k8sEnv.getServices().put(n, s);
return n;
}).orElse(null);
for (ServicePort servicePort : securedPorts.values()) {
// expose service port related secure servers if exist
Map<String, ServerConfig> matchedSecureServers = match(securedServers, servicePort);
if (!matchedSecureServers.isEmpty()) {
onEachExposableServerSet(matchedSecureServers, (serverId, srvrs) -> {
secureServerExposer.expose(k8sEnv, pod, machineName, secureServiceName, serverId, servicePort, srvrs);
});
}
}
}
use of org.eclipse.che.api.core.model.workspace.config.ServerConfig in project devspaces-images by redhat-developer.
the class KubernetesServerExposer method expose.
/**
* Exposes specified servers.
*
* <p>Note that created Kubernetes objects will select the corresponding pods by {@link
* Constants#CHE_ORIGINAL_NAME_LABEL} label. That should be added by {@link
* UniqueNamesProvisioner}.
*
* @param servers servers to expose
* @see ConfigurationProvisioner#provision(KubernetesEnvironment, RuntimeIdentity)
*/
public void expose(Map<String, ? extends ServerConfig> servers) throws InfrastructureException {
Map<String, ServerConfig> internalServers = new HashMap<>();
Map<String, ServerConfig> externalServers = new HashMap<>();
Map<String, ServerConfig> secureServers = new HashMap<>();
Map<String, ServicePort> unsecuredPorts = new HashMap<>();
Map<String, ServicePort> securedPorts = new HashMap<>();
splitServersAndPortsByExposureType(servers, internalServers, externalServers, secureServers, unsecuredPorts, securedPorts);
provisionServicesForDiscoverableServers(servers);
Optional<Service> serviceOpt = createService(internalServers, unsecuredPorts);
if (serviceOpt.isPresent()) {
Service service = serviceOpt.get();
String serviceName = service.getMetadata().getName();
k8sEnv.getServices().put(serviceName, service);
exposeNonSecureServers(serviceName, externalServers, unsecuredPorts);
}
exposeSecureServers(secureServers, securedPorts);
}
use of org.eclipse.che.api.core.model.workspace.config.ServerConfig in project devspaces-images by redhat-developer.
the class AbstractJwtProxyProvisioner method expose.
/**
* Modifies Kubernetes environment to expose the specified service port via JWTProxy.
*
* @param k8sEnv Kubernetes environment to modify
* @param pod the pod that runs the server being exposed
* @param backendServiceName service name that will be exposed
* @param backendServicePort service port that will be exposed
* @param protocol protocol that will be used for exposed port
* @param secureServers secure servers to expose
* @return JWTProxy service port that expose the specified one
* @throws InfrastructureException if any exception occurs during port exposing
*/
@Override
public ServicePort expose(KubernetesEnvironment k8sEnv, PodData pod, String machineName, String backendServiceName, ServicePort backendServicePort, String protocol, boolean requireSubdomain, Map<String, ServerConfig> secureServers) throws InfrastructureException {
Preconditions.checkArgument(secureServers != null && !secureServers.isEmpty(), "Secure servers are missing");
ensureJwtProxyInjected(k8sEnv, machineName, pod);
Set<String> excludes = new HashSet<>();
Boolean cookiesAuthEnabled = null;
for (ServerConfig serverConfig : secureServers.values()) {
ExposureConfiguration config = getExposureConfiguration(serverConfig);
// accumulate unsecured paths
if (config.excludedPaths != null) {
excludes.addAll(config.excludedPaths);
}
// calculate `cookiesAuthEnabled` attributes
if (detectCookieAuth) {
if (cookiesAuthEnabled == null) {
cookiesAuthEnabled = config.cookiesAuthEnabled;
} else {
if (!cookiesAuthEnabled.equals(config.cookiesAuthEnabled)) {
throw new InfrastructureException("Secure servers which expose the same port should have the same `cookiesAuthEnabled` value.");
}
}
}
}
int listenPort = availablePort++;
ServicePort exposedPort = new ServicePortBuilder().withName("server-" + listenPort).withPort(listenPort).withProtocol(protocol).withNewTargetPort(listenPort).build();
k8sEnv.getServices().get(serviceName).getSpec().getPorts().add(exposedPort);
CookiePathStrategy actualCookiePathStrategy = requireSubdomain ? multihostCookiePathStrategy : cookiePathStrategy;
ExternalServiceExposureStrategy actualExposureStrategy = requireSubdomain ? multiHostExternalServiceExposureStrategy : externalServiceExposureStrategy;
// JwtProxySecureServerExposer creates no service for the exposed secure servers and
// assumes everything will be proxied from localhost, because JWT proxy is collocated
// with the workspace pod (because it is added to the environment as an injectable pod).
// This method historically supported proxying secure servers exposed through a service
// (which is not secure in absence of a appropriate network policy). The support for
// accessing the backend server through a service was kept here because it doesn't add
// any additional complexity to this method and keeps the door open for the
// JwtProxySecureServerExposer to be enhanced in the future with support for service-handled
// secure servers.
backendServiceName = backendServiceName == null ? "127.0.0.1" : backendServiceName;
proxyConfigBuilder.addVerifierProxy(listenPort, "http://" + backendServiceName + ":" + backendServicePort.getTargetPort().getIntVal(), excludes, cookiesAuthEnabled == null ? false : cookiesAuthEnabled, actualCookiePathStrategy.get(serviceName, exposedPort), actualExposureStrategy.getExternalPath(serviceName, exposedPort.getName()));
k8sEnv.getConfigMaps().get(getConfigMapName()).getData().put(JWT_PROXY_CONFIG_FILE, proxyConfigBuilder.build());
return exposedPort;
}
use of org.eclipse.che.api.core.model.workspace.config.ServerConfig in project devspaces-images by redhat-developer.
the class KubernetesServerExposerTest method assertThatExternalServersAreExposed.
@SuppressWarnings("SameParameterValue")
private void assertThatExternalServersAreExposed(String machineName, String portProtocol, Integer port, Map<String, ServerConfig> expectedServers) {
// then
assertThatContainerPortIsExposed(portProtocol, port);
// ensure that service is created
Service service = findContainerRelatedService();
assertNotNull(service);
// ensure that required service port is exposed
ServicePort servicePort = assertThatServicePortIsExposed(port, service);
Annotations.Deserializer serviceAnnotations = Annotations.newDeserializer(service.getMetadata().getAnnotations());
assertEquals(serviceAnnotations.machineName(), machineName);
// check that we did not create servers for public endpoints
assertFalse(serviceAnnotations.servers().keySet().stream().anyMatch(key -> expectedServers.containsKey(key)));
verify(externalServerExposer).expose(eq(kubernetesEnvironment), eq(machineName), eq(service.getMetadata().getName()), any(), eq(servicePort), eq(expectedServers));
}
use of org.eclipse.che.api.core.model.workspace.config.ServerConfig in project devspaces-images by redhat-developer.
the class KubernetesServerExposerTest method shouldCreateIngressPerUniqueServerWithTheSamePort.
@Test
public void shouldCreateIngressPerUniqueServerWithTheSamePort() throws Exception {
// given
ServerConfigImpl httpServerConfig = new ServerConfigImpl("8080/tcp", "http", "/api", UNIQUE_SERVER_ATTRIBUTES);
ServerConfigImpl wsServerConfig = new ServerConfigImpl("8080/tcp", "ws", "/connect", UNIQUE_SERVER_ATTRIBUTES);
ServicePort servicePort = new ServicePortBuilder().withName("server-8080").withPort(8080).withProtocol("TCP").withTargetPort(new IntOrString(8080)).build();
Map<String, ServerConfig> serversToExpose = ImmutableMap.of("http-server", httpServerConfig, "ws-server", wsServerConfig);
// when
serverExposer.expose(serversToExpose);
// then
assertThatExternalServerIsExposed(MACHINE_NAME, "tcp", 8080, "http-server", new ServerConfigImpl(httpServerConfig).withAttributes(UNIQUE_SERVER_ATTRIBUTES));
assertThatExternalServerIsExposed(MACHINE_NAME, "tcp", 8080, "ws-server", new ServerConfigImpl(wsServerConfig).withAttributes(UNIQUE_SERVER_ATTRIBUTES));
}
Aggregations