Skip to content

feat: redirect HTTP to HTTPS#2153

Merged
mergify[bot] merged 2 commits intoaws:mainlinefrom
iamhopaul123:forward-http-domain
Apr 8, 2021
Merged

feat: redirect HTTP to HTTPS#2153
mergify[bot] merged 2 commits intoaws:mainlinefrom
iamhopaul123:forward-http-domain

Conversation

@iamhopaul123
Copy link
Copy Markdown
Contributor

Part of #1188. Redirect HTTP to HTTPS if possible.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@iamhopaul123 iamhopaul123 requested a review from a team as a code owner April 7, 2021 20:36
@iamhopaul123 iamhopaul123 requested a review from efekarakus April 7, 2021 20:36
@iamhopaul123 iamhopaul123 requested a review from kohidave April 7, 2021 20:37
Comment thread templates/workloads/services/lb-web/cf.yml
Comment on lines +285 to +292
- Field: 'host-header'
HostHeaderConfig:
Values:
- Fn::Join:
- '.'
- - !Ref WorkloadName
- Fn::ImportValue:
!Sub "${AppName}-${EnvName}-SubDomain"
Copy link
Copy Markdown
Contributor

@efekarakus efekarakus Apr 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this saying exactly? 🤔
if you go to http://workload.env.app.domain.com/path then redirect to https?

I thought what we wanted was something like if I go to http://<anything>/path redirect to https://workload.env.app.domain.com/path so that it also captures the default elb dns name but I might be misunderstanding!

      Actions:
        - Type: redirect
          RedirectConfig:
            Protocol: HTTPS
            Port: 443
            Host: 
             - Fn::Join:
                - '.'
                - - !Ref WorkloadName
                  - Fn::ImportValue:
                      !Sub "${AppName}-${EnvName}-SubDomain"
            Path: "/#{path}"
            Query: "#{query}"
            StatusCode: HTTP_301
      Conditions:
       - Field: 'path-pattern'
          PathPatternConfig:
            Values:
              !If
                - HTTPRootPath
                -
                  - "/*"
                -
                  - !Sub "/${RulePath}"
                  - !Sub "/${RulePath}/*"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we might just want to redirect http://frontend.env.app.domain.com/path to https://frontend.env.app.domain.com/path, and http://backend.env.app.domain.com/path to https://backend.env.app.domain.com/path.

@mergify mergify Bot merged commit f81b0d4 into aws:mainline Apr 8, 2021
@craquiest
Copy link
Copy Markdown

Hi guys, thanks so much for releasing this feature!

Is it possible to use it without tearing down my envs/app that are already live? Will it work if I just upgrade to copilot 1.5.0 and run copilot pipeline update or maybe copilot svc deploy -n frontend -e test ?

By the way, is it possible for the test and prod envs to share the same certificate (& hosted zone) that would cover the whole domain (instead of having separate ones). I was trying to have CNAME / Alias records pointing across different subdomains, but it is triggering Certificate errors on the browser.

Thank you and best regards,
Lamine

@efekarakus
Copy link
Copy Markdown
Contributor

Hi @craquiest !

Is it possible to use it without tearing down my envs/app that are already live?

If you initialized your application with app init --domain then yes! In your buildspec.yml file, you'd need to update the install instructions to use the latest version of copilot:

phases:
  install:
    commands:
     -  wget https://ecs-cli-v2-release.s3.amazonaws.com/copilot-linux-v1.5.0 # Use 1.5.0
      - mv ./copilot-linux-v1.5.0 ./copilot-linux
      - chmod +x ./copilot-linux

Then commit and push the buildspec so that the binary can generate the service templates with this new change :)

By the way, is it possible for the test and prod envs to share the same certificate (& hosted zone) that would cover the whole domain (instead of having separate ones). I was trying to have CNAME / Alias records pointing across different subdomains, but it is triggering Certificate errors on the browser.

Not the at the moment apologies 🙇 I believe @iamhopaul123's http.alias work that we are working on at the moment:
#1188 (comment) should address this request! We will still create two certs but the alternate domains should allow the subdomains you're trying to specify, alternatively you'll be able to import certificates. Can you share with us an example endpoint you'd like to use for your service in "test" and "prod"?

@craquiest
Copy link
Copy Markdown

Thank you @efekarakus ,

So I committed and pushed that change in buildspec.yml. But I keep get this error in the build phase.

Step 1/15 : FROM python:3.9
toomanyrequests: You have reached your pull rate limit. You may increase the limit by authenticating and upgrading: https://www.docker.com/increase-rate-limit
--
2629 | WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
2630 | Configure a credential helper to remove this warning. See
2631 | https://docs.docker.com/engine/reference/commandline/login/#credentials-store

It is not the 1st time, so it is probably not be related to the release. I have already tried the instructions in the docker link. In essence I put my docker credentials in Secret Manager, and added the copilot-application & copilot-environment tags to the secret, for both prod and test. But it is failing more often than not. Isn't the caching system working in ECR , when the new code is deployed through the pipeline and codebuild? I do not understand why the 1st layers like getting python 3.9 and other pip install are being pulled everytime instead of tapping the cache... Am I missing something?

Lamine

@efekarakus
Copy link
Copy Markdown
Contributor

@craquiest Oh noo looks like you got hit by the docker pull limit!

A wrote this a while back which sounds similar to your secret manager work around: #1869 (comment) which has worked alright for me. If you don't mind can you take a look at it?

I think while pushing it should realize that python3.9 is cached already in the repo and the push won't store the layer again but the problem seems to be while building the image itself. Since there is no cache for your codebuild builds, then each time it's downloading the layer again.

@craquiest
Copy link
Copy Markdown

@efekarakus, thanks, the parameter store workaround did the trick! awesome! and now the redirects from http to https work.

I still do have the issue of the frontend not finding the backend after the pipeline finished. As I mentioned this back in Oct/November, I need to manually stop the frontend task in ECS and then when it re-provisions and starts, the connection to the backend works. I remember you saying it was maybe something in CloudMap, and that maybe a (back-then in version 0.5.0) previewed feature would fix it. I would really like to solve the problem this time around, as it throws off my continuity/availability for a few mins every time...

Re: domain, what I am trying to do is simply point the {domain}.net, or at least the {appname}.{domain}.net to the production frontend frontend.prod.{appname}.{domain}.net - without having the whole 'frontend.prod' bit. I naively assumed that was what the --prod flag in svc deploy going to do. I tried adding an extra alias /cname in the hosted zone poiting to the ALB, but since the certificate is only for prod, or only for test, not the whole domain or appname.domain, I get 'insecure' , 'invalid certificate' errors. There is pbly an easy workaround with domain records, but as you can gather my DNS is pretty sketchy. Any help would be much appreciated.

@efekarakus
Copy link
Copy Markdown
Contributor

@efekarakus, thanks, the parameter store workaround did the trick! awesome! and now the redirects from http to https work.

Yay this is awesome to hear 😄

I would really like to solve the problem this time around, as it throws off my continuity/availability for a few mins every time...

This is the same account that you emailed me earlier and in the region eu-west-1 right? That's odd the fix should be out 🤔 would you mind sharing one of the task IDs of a Backend Service where the DNS TTL wasn't waited before deregistering the task? I'll reach out to one of the engineers on the service team to investigate.

Re: domain, what I am trying to do is simply point the {domain}.net, or at least the {appname}.{domain}.net to the production frontend frontend.

Ok yes, then #1188 will definitely enable this usecase, stay tuned since we are actively working on the feature 😄 ! In the mean time for manual steps that can be taken check out this comment: #1188 (comment) Hope this help until we have the http.alias field out!

@craquiest
Copy link
Copy Markdown

@efekarakus Thanks!

Ok yes, then #1188 will definitely enable this usecase, stay tuned since we are actively working on the feature 😄 ! In the mean time for manual steps that can be taken check out this comment: #1188 (comment) Hope this help until we have the http.alias field out!

this worked like a charm. Can't wait for the http.alias field to come out.

This is the same account that you emailed me earlier and in the region eu-west-1 right? That's odd the fix should be out 🤔 would you mind sharing one of the task IDs of a Backend Service where the DNS TTL wasn't waited before deregistering the task? I'll reach out to one of the engineers on the service team to investigate.

Yes it is the same issue. But I moved the app to my business account. Same setup, same region. I will email you the details you asked.

@gautam-nutalapati
Copy link
Copy Markdown
Contributor

gautam-nutalapati commented Apr 28, 2021

I am seeing an interesting behavior related to this:

  • Copilot app already exists with a domain which was created with older version of copilot
  • I am using copilot version 1.5.0 and created copilot ENV + SVC in our app
  • CloudFormation created a resource "HTTPListenerRuleWithDomain" and its respective listener is not using redirect config but I guess it should as per this, it should.
  • But the endpoint of service does get redirected! 'curl -v http://frontenv.gautam-stg.vault.prisidio.net/health'
* TCP_NODELAY set
* Connected to frontenv.gautam-stg.vault.prisidio.net (x.x.x.x) port 80 (#0)
> GET /health HTTP/1.1
> Host: frontenv.gautam-stg.vault.prisidio.net
> User-Agent: curl/7.64.1
> Accept: */*
> 
< HTTP/1.1 301 Moved Permanently
< Server: awselb/2.0
< Date: Wed, 28 Apr 2021 17:50:25 GMT
< Content-Type: text/html
< Content-Length: 134
< Connection: keep-alive
< Location: https://frontenv.gautam-stg.vault.prisidio.net:443/health
< 
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
</body>
</html>
* Connection #0 to host frontenv.gautam-stg.vault.prisidio.net left intact
* Closing connection 0
  • I am failing to understand how the redirect is working. Does anyone have an idea?
    FYI port 81 is my test config in below pic:

Screen Shot 2021-04-28 at 12 38 45 PM

@iamhopaul123
Copy link
Copy Markdown
Contributor Author

Hello @gautam-nutalapati.

and its respective listener is not using redirect config

Is it not in the rules for the HTTP listener (HTTP: 80)?

@gautam-nutalapati
Copy link
Copy Markdown
Contributor

Rules for HTTP:80 listener is forwarding requests to target group but it is not redirecting them.
It's the first row in attached image.

@iamhopaul123
Copy link
Copy Markdown
Contributor Author

That's the default rule we need to create the listener since the listeners are created in the env stack while those fancy listener rules like HTTPListenerRuleWithDomain are created in the svc stack. I think if you click View/edit rules you'll find the other rules attaching to the HTTP listener.

@gautam-nutalapati
Copy link
Copy Markdown
Contributor

You are correct! I see them now. I wasn't looking at the console correctly, I wrongly thought that all rules would be listed in that page itself. Sorry for raising an invalid issue.

@iamhopaul123
Copy link
Copy Markdown
Contributor Author

iamhopaul123 commented Apr 28, 2021

It's totally fine! Don't worry about it. I'll let the Console team for ELB know of this feedback

thrau pushed a commit to localstack/copilot-cli-local that referenced this pull request Dec 9, 2022
<!-- Provide summary of changes -->
Part of aws#1188. Redirect HTTP to HTTPS if possible.
<!-- Issue number, if available. E.g. "Fixes aws#31", "Addresses aws#42, 77" -->

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants