<p>I want the traffic thar comes to my cluster as HTTP to be redirected to HTTPS. However, the cluster receives requests from hundreds of domains that change dinamically (creating new certs with cert-manager). So I want the redirect to happen only when the URI doesn't have the prefix <code>/.well-known/acme-challenge</code></p>
<p>I am using a gateway that listens to 443 and other gateway that listens to 80 and send the HTTP to an acme-solver virtual service.</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: default-gateway
spec:
  selector:
    istio: ingressgateway
  servers:
  - hosts:
    - site1.com
    port:
      name: https-site1.com
      number: 443
      protocol: HTTPS
    tls:
      credentialName: cert-site1.com
      mode: SIMPLE
  - hosts:
    - site2.com
    port:
      name: https-site2.com
      number: 443
      protocol: HTTPS
    tls:
      credentialName: cert-site2.com
      mode: SIMPLE
  ...
---
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: acme-gateway
  namespace: istio-system
spec:
  selector:
    istio: ingressgateway
  servers:
  - hosts:
    - '*'
    port:
      name: http
      number: 80
      protocol: HTTP
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: acme-solver
  namespace: istio-system
spec:
  hosts:
  - &quot;*&quot;
  gateways:
  - acme-gateway
  http:
  - match:
    - uri:
        prefix: /.well-known/acme-challenge
    route:
    - destination:
        host: acme-solver.istio-system.svc.cluster.local
        port:
          number: 8089
  - redirect:
      authority: # Should redirect to https://$HOST, but I don't know how to get the $HOST
</code></pre>
<p>How can I do that using istio?</p>

<p>Looking into the documentation:</p>
<ul>
<li>The HTTP-01 challenge can only be done on port 80. Allowing clients to specify arbitrary ports would make the challenge less secure, and so it is not allowed by the ACME standard.</li>
</ul>
<p>As a workaround:</p>
<ol>
<li>Please consider using DNS-01 challenge:</li>
</ol>
<p>a) it only makes sense to use DNS-01 challenges if your DNS provider has an API you can use to automate <a href="https://community.letsencrypt.org/t/dns-providers-who-easily-integrate-with-lets-encrypt-dns-validation/86438" rel="nofollow noreferrer">updates</a>.</p>
<p>b) using this approach you should consider additional security risk as stated in the <a href="https://letsencrypt.org/docs/challenge-types/#dns-01-challenge" rel="nofollow noreferrer">docs</a>:</p>
<p>Pros:
You can use this challenge to issue certificates containing wildcard domain names.
It works well even if you have multiple web servers.</p>
<p>Cons:
*<em><strong>Keeping API credentials on your web server is risky.</strong></em>
Your DNS provider might not offer an API.
Your DNS API may not provide information on propagation times.</p>
<p>As mentioned <a href="https://www.eff.org/deeplinks/2018/02/technical-deep-dive-securing-automation-acme-dns-challenge-validation" rel="nofollow noreferrer">here</a>:</p>
<blockquote>
<p>In order to be automatic, though, the software that requests the certificate will also need to be able to modify the DNS records for that domain. In order to modify the DNS records, that software will also need to have access to the credentials for the DNS service (e.g. the login and password, or a cryptographic token), and those credentials will have to be stored wherever the automation takes place. In many cases, this means that if the machine handling the process gets compromised, so will the DNS credentials, and this is where the real danger lies.</p>
</blockquote>
<hr />
<ol start="2">
<li>I would suggest  also another approach to use some simple nginx pod which would redirect all http traffic to https.</li>
</ol>
<p>There is a tutorial on <a href="https://medium.com/@gregoire.waymel/istio-cert-manager-lets-encrypt-demystified-c1cbed011d67" rel="nofollow noreferrer">medium</a> with nginx configuration you might try to use.</p>
<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config
data:
  nginx.conf: |
    server {
      listen 80 default_server;
      server_name _;
      return 301 https://$host$request_uri;
    }
---
apiVersion: v1
kind: Service
metadata:
  name: redirect
  labels:
    app: redirect
spec:
  ports:
  - port: 80
    name: http
  selector:
    app: redirect
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: redirect
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redirect
  template:
    metadata:
      labels:
        app: redirect
    spec:
      containers:
      - name: redirect
        image: nginx:stable
        resources:
          requests:
            cpu: &quot;100m&quot;
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
        volumeMounts:
        - mountPath: /etc/nginx/conf.d
          name: config
      volumes:
      - name: config
        configMap:
          name: nginx-config
</code></pre>
<p>Additionally you would have to change your virtual service to send all the traffic except <code>prefix: /.well-known/acme-challenge</code> to nginx.</p>
<pre><code>apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: acme-solver
  namespace: istio-system
spec:
  hosts:
  - &quot;*&quot;
  gateways:
  - acme-gateway
  http:
  - name: &quot;acmesolver&quot;
    match:
    - uri:
        prefix: /.well-known/acme-challenge
    route:
    - destination:
        host: reviews.prod.svc.cluster.local
        port:
          number: 8089
  - name: &quot;nginx&quot;
    route:
    - destination:
        host: nginx
</code></pre>
