IngressTlsProvisioner implements ConfigurationProvisioner<KubernetesEnvironment>,
        TlsProvisioner<KubernetesEnvironment> { @Override public void provision(KubernetesEnvironment k8sEnv, RuntimeIdentity identity) throws KubernetesInfrastructureException { if (!isTlsEnabled) { return; } String wsTlsSecretName = tlsSecretName; if (!isNullOrEmpty(tlsCert) && !isNullOrEmpty(tlsKey)) { wsTlsSecretName = identity.getWorkspaceId() + '-' + tlsSecretName; provisionTlsSecret(k8sEnv, wsTlsSecretName); } for (Ingress ingress : k8sEnv.getIngresses().values()) { useSecureProtocolForIngressServers(ingress); enableTLS(ingress, wsTlsSecretName); } } @Inject  IngressTlsProvisioner(
      @Named("che.infra.kubernetes.tls_enabled") boolean isTlsEnabled,
      @Named("che.infra.kubernetes.tls_secret") String tlsSecretName,
      @Nullable @Named("che.infra.kubernetes.tls_cert") String tlsCert,
      @Nullable @Named("che.infra.kubernetes.tls_key") String tlsKey); @Override void provision(KubernetesEnvironment k8sEnv, RuntimeIdentity identity);  }
@Test public void doNothingWhenTlsDisabled() throws Exception { IngressTlsProvisioner ingressTlsProvisioner = new IngressTlsProvisioner(false, "", "", ""); ingressTlsProvisioner.provision(k8sEnv, runtimeIdentity); verify(k8sEnv, never()).getIngresses(); }
@Test public void provisionTlsForIngressesWhenTlsEnabledAndSecretProvided() throws Exception { IngressTlsProvisioner ingressTlsProvisioner = new IngressTlsProvisioner(true, "secretname", "", ""); Map<String, Ingress> ingresses = new HashMap<>(); ingresses.put("ingress", ingress); when(k8sEnv.getIngresses()).thenReturn(ingresses); ingressTlsProvisioner.provision(k8sEnv, runtimeIdentity); assertEquals(ingress.getSpec().getTls().size(), 1); assertEquals(ingress.getSpec().getTls().get(0).getHosts().size(), 1); assertEquals(ingress.getSpec().getTls().get(0).getHosts().get(0), host); assertEquals(ingress.getSpec().getTls().get(0).getSecretName(), "secretname"); verifyServersTLS(ingress.getMetadata().getAnnotations()); }
@Test public void provisionTlsForIngressesWhenTlsEnabledAndSecretEmpty() throws Exception { IngressTlsProvisioner ingressTlsProvisioner = new IngressTlsProvisioner(true, "", "", ""); Map<String, Ingress> ingresses = new HashMap<>(); ingresses.put("ingress", ingress); when(k8sEnv.getIngresses()).thenReturn(ingresses); ingressTlsProvisioner.provision(k8sEnv, runtimeIdentity); assertEquals(ingress.getSpec().getTls().size(), 1); assertEquals(ingress.getSpec().getTls().get(0).getHosts().size(), 1); assertEquals(ingress.getSpec().getTls().get(0).getHosts().get(0), host); assertNull(ingress.getSpec().getTls().get(0).getSecretName()); verifyServersTLS(ingress.getMetadata().getAnnotations()); }
@Test public void provisionTlsForIngressesWhenTlsEnabledAndSecretNull() throws Exception { IngressTlsProvisioner ingressTlsProvisioner = new IngressTlsProvisioner(true, null, "", ""); Map<String, Ingress> ingresses = new HashMap<>(); ingresses.put("ingress", ingress); when(k8sEnv.getIngresses()).thenReturn(ingresses); ingressTlsProvisioner.provision(k8sEnv, runtimeIdentity); assertEquals(ingress.getSpec().getTls().size(), 1); assertEquals(ingress.getSpec().getTls().get(0).getHosts().size(), 1); assertEquals(ingress.getSpec().getTls().get(0).getHosts().get(0), host); assertEquals(ingress.getSpec().getTls().get(0).getSecretName(), null); verifyServersTLS(ingress.getMetadata().getAnnotations()); }
@Test public void provisionTlsSecretWhenTlsCertAndKeyAreSpecified() throws Exception { IngressTlsProvisioner ingressTlsProvisioner = new IngressTlsProvisioner(true, "ws-tls-secret", "cert", "key"); Map<String, Secret> secrets = new HashMap<>(); when(k8sEnv.getSecrets()).thenReturn(secrets); ingressTlsProvisioner.provision(k8sEnv, runtimeIdentity); assertEquals(k8sEnv.getSecrets().size(), 1); Secret tlsSecret = k8sEnv.getSecrets().get(runtimeIdentity.getWorkspaceId() + "-ws-tls-secret"); assertNotNull(tlsSecret); assertEquals(tlsSecret.getStringData().get("tls.crt"), "cert"); assertEquals(tlsSecret.getStringData().get("tls.key"), "key"); assertEquals(tlsSecret.getType(), TLS_SECRET_TYPE); verifyServersTLS(ingress.getMetadata().getAnnotations()); }