Skip to content

Commit

Permalink
fix: 403 http error consulcam website (#126)
Browse files Browse the repository at this point in the history
issue caused by activation of cpanel badbots rule
  • Loading branch information
billmetangmo committed Jul 26, 2023
1 parent 820d7c2 commit 7065130
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
19 changes: 12 additions & 7 deletions infra/api/scan.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys

import shutil
sys.path.insert(0, "./package")
import requests
import urllib.request
Expand All @@ -13,6 +13,9 @@
bucket_name = os.environ["BUCKET_NAME"]
Table_Links = os.environ["LINKS_TABLE"]
maintainer_mail = os.environ["MAINTAINER_MAIL"]
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0"
}


def S3_bucket_pictures(Picture_image, bucket_name):
Expand All @@ -37,7 +40,12 @@ def dowload_image(url):
"""
name = url.split("/")[-1]
real_image = f"/tmp/{str(name)}" # image in jpg version ( only /tmp is writable in aws lambda)
urllib.request.urlretrieve(url, real_image)
r = requests.get(link,stream=True,headers=headers)
r.raw.decode_content = True
r.raise_for_status()

with open( real_image, 'wb') as f:
shutil.copyfileobj(r.raw, f)
return real_image


Expand All @@ -48,12 +56,9 @@ def get_source_code(link):
:param link: the link of the web page you want to scrape
:return: the source code of the web page
"""
proxy_url = os.environ["PROXY_URL"]
proxies = {"http": proxy_url, "https": proxy_url}

r = requests.get(link,proxies=proxies, verify=False)
r = requests.get(url,headers=headers)
r.raise_for_status()
return soup(r.text)
return soup(r.text,features="html.parser")


def filter(code_source_html):
Expand Down
2 changes: 1 addition & 1 deletion infra/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ resource "aws_lambda_function" "scan" {
API_KEY = var.API_KEY
SENTRY_DNS = var.SENTRY_DNS
ENV = (terraform.workspace == "mtchoun-mouh-master") ? "production" : "${terraform.workspace}"
PROXY_URL = var.PROXY_URL

}
}

Expand Down
4 changes: 0 additions & 4 deletions infra/vars.tf
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,3 @@ variable "TFC_WORKSPACE_NAME" {
type = string
default = ""
}

variable "PROXY_URL" {
type = string
}

2 comments on commit 7065130

@billmetangmo
Copy link
Member Author

Choose a reason for hiding this comment

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

Plan Result

CI link

Plan: 1 to add, 4 to change, 0 to destroy.
  • Create
    • local_file.index_page
  • Update
    • aws_lambda_function.lambda
    • aws_lambda_function.scan
    • aws_s3_bucket.images
    • aws_s3_bucket.website
Change Result (Click me)
  # aws_lambda_function.lambda will be updated in-place
  ~ resource "aws_lambda_function" "lambda" {
        id                             = "user_registration_consulcam"
      ~ last_modified                  = "2023-07-26T12:01:09.000+0000" -> (known after apply)
      ~ source_code_hash               = "ZZAcdqHvbDQTWepXeSsEZQIx67SSas024knUa2rV8KI=" -> "JzdFncAsVjFhJJa+STDgX3rPAAAtQDaAIK660B1jJag="
        tags                           = {
            "environment" = "mtchoun-mouh-master"
            "project"     = "mtchoun-mouh"
        }
        # (17 unchanged attributes hidden)

        # (2 unchanged blocks hidden)
    }

  # aws_lambda_function.scan will be updated in-place
  ~ resource "aws_lambda_function" "scan" {
        id                             = "scan_user_consulcam"
      ~ last_modified                  = "2023-07-26T12:21:05.000+0000" -> (known after apply)
      ~ source_code_hash               = "BKTYrhJv81gIOVkblY+qwskYBJcoAuAx2oKZe5PJaQc=" -> "JzdFncAsVjFhJJa+STDgX3rPAAAtQDaAIK660B1jJag="
        tags                           = {
            "environment" = "mtchoun-mouh-master"
            "project"     = "mtchoun-mouh"
        }
        # (17 unchanged attributes hidden)

        # (2 unchanged blocks hidden)
    }

  # aws_s3_bucket.images will be updated in-place
  ~ resource "aws_s3_bucket" "images" {
        id                          = "djansang"
        tags                        = {
            "Name"        = "images"
            "environment" = "mtchoun-mouh-master"
            "project"     = "mtchoun-mouh"
        }
        # (10 unchanged attributes hidden)

      - server_side_encryption_configuration {
          - rule {
              - bucket_key_enabled = false -> null

              - apply_server_side_encryption_by_default {
                  - sse_algorithm = "AES256" -> null
                }
            }
        }

        # (1 unchanged block hidden)
    }

  # aws_s3_bucket.website will be updated in-place
  ~ resource "aws_s3_bucket" "website" {
        id                          = "mtchoun-mouh.mongulu.cm"
        tags                        = {
            "Name"        = "Website"
            "environment" = "mtchoun-mouh-master"
            "project"     = "mtchoun-mouh"
        }
        # (12 unchanged attributes hidden)

      - server_side_encryption_configuration {
          - rule {
              - bucket_key_enabled = false -> null

              - apply_server_side_encryption_by_default {
                  - sse_algorithm = "AES256" -> null
                }
            }
        }

        # (3 unchanged blocks hidden)
    }

  # local_file.index_page will be created
  + resource "local_file" "index_page" {
      + content              = (sensitive value)
      + content_base64sha256 = (known after apply)
      + content_base64sha512 = (known after apply)
      + content_md5          = (known after apply)
      + content_sha1         = (known after apply)
      + content_sha256       = (known after apply)
      + content_sha512       = (known after apply)
      + directory_permission = "0777"
      + file_permission      = "0777"
      + filename             = "../html/index.html"
      + id                   = (known after apply)
    }

Plan: 1 to add, 4 to change, 0 to destroy.

@billmetangmo
Copy link
Member Author

Choose a reason for hiding this comment

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

✅ Apply Succeeded

CI link

Apply complete! Resources: 1 added, 4 changed, 0 destroyed.
Details (Click me)
Running apply in the remote backend. Output will stream here. Pressing Ctrl-C
will cancel the remote apply if it's still pending. If the apply started it
will stop streaming the logs, but will not stop the apply running remotely.

Preparing the remote apply...

To view this run in a browser, visit:
https://app.terraform.io/app/tfc-mongulu-cm/mtchoun-mouh-master/runs/run-2xUD61CnBLLSkCAN

Waiting for the plan to start...

Terraform v1.5.2
on linux_amd64
Initializing plugins and modules...
data.archive_file.lambda_zip: Reading...
data.archive_file.lambda_zip: Read complete after 0s [id=23b4d20aa1e3630c2153055634b19f15b52aad52]
data.aws_caller_identity.current: Reading...
aws_api_gateway_rest_api.api: Refreshing state... [id=gozv19c3jf]
data.aws_iam_role.role: Reading...
aws_cloudwatch_event_rule.scheduler: Refreshing state... [id=trigger_user_scan]
aws_dynamodb_table.Register: Refreshing state... [id=Register]
aws_s3_bucket.website: Refreshing state... [id=mtchoun-mouh.mongulu.cm]
aws_dynamodb_table.Users: Refreshing state... [id=Users]
aws_dynamodb_table.Link_table: Refreshing state... [id=Link_table]
aws_s3_bucket.images: Refreshing state... [id=djansang]
data.aws_caller_identity.current: Read complete after 0s [id=053932140667]
data.aws_iam_role.role: Read complete after 0s [id=website-deployer]
aws_lambda_function.lambda: Refreshing state... [id=user_registration_consulcam]
aws_lambda_function.scan: Refreshing state... [id=scan_user_consulcam]
aws_api_gateway_resource.resource: Refreshing state... [id=gtenoa]
aws_api_gateway_method.method: Refreshing state... [id=agm-gozv19c3jf-gtenoa-POST]
module.cors.aws_api_gateway_method._: Refreshing state... [id=agm-gozv19c3jf-gtenoa-OPTIONS]
aws_lambda_permission.allow_cloudwatch_to_call_check_foo: Refreshing state... [id=AllowExecutionFromCloudWatch]
aws_cloudwatch_event_target.target: Refreshing state... [id=trigger_user_scan-lambda]
aws_api_gateway_method_response.method_response_200: Refreshing state... [id=agmr-gozv19c3jf-gtenoa-POST-200]
aws_lambda_permission.apigw_lambda: Refreshing state... [id=AllowExecutionFromAPIGateway]
aws_api_gateway_integration.integration: Refreshing state... [id=agi-gozv19c3jf-gtenoa-POST]
module.cors.aws_api_gateway_integration._: Refreshing state... [id=agi-gozv19c3jf-gtenoa-OPTIONS]
module.cors.aws_api_gateway_method_response._: Refreshing state... [id=agmr-gozv19c3jf-gtenoa-OPTIONS-200]
aws_api_gateway_deployment.test: Refreshing state... [id=7suq19]
module.cors.aws_api_gateway_integration_response._: Refreshing state... [id=agir-gozv19c3jf-gtenoa-OPTIONS-200]
local_file.index_page: Refreshing state... [id=3df23f6ccd0c737240f079546f3bd4cefc59b440]
aws_s3_bucket_public_access_block.website: Refreshing state... [id=mtchoun-mouh.mongulu.cm]
aws_s3_bucket_object.example_file: Refreshing state... [id=index.html]
aws_s3_bucket_policy.website: Refreshing state... [id=mtchoun-mouh.mongulu.cm]

Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
  + create
  ~ update in-place

Terraform will perform the following actions:

  # aws_lambda_function.lambda will be updated in-place
  ~ resource "aws_lambda_function" "lambda" {
        id                             = "user_registration_consulcam"
      ~ last_modified                  = "2023-07-26T12:01:09.000+0000" -> (known after apply)
      ~ source_code_hash               = "ZZAcdqHvbDQTWepXeSsEZQIx67SSas024knUa2rV8KI=" -> "JzdFncAsVjFhJJa+STDgX3rPAAAtQDaAIK660B1jJag="
        tags                           = {
            "environment" = "mtchoun-mouh-master"
            "project"     = "mtchoun-mouh"
        }
        # (17 unchanged attributes hidden)

        # (2 unchanged blocks hidden)
    }

  # aws_lambda_function.scan will be updated in-place
  ~ resource "aws_lambda_function" "scan" {
        id                             = "scan_user_consulcam"
      ~ last_modified                  = "2023-07-26T12:21:05.000+0000" -> (known after apply)
      ~ source_code_hash               = "BKTYrhJv81gIOVkblY+qwskYBJcoAuAx2oKZe5PJaQc=" -> "JzdFncAsVjFhJJa+STDgX3rPAAAtQDaAIK660B1jJag="
        tags                           = {
            "environment" = "mtchoun-mouh-master"
            "project"     = "mtchoun-mouh"
        }
        # (17 unchanged attributes hidden)

        # (2 unchanged blocks hidden)
    }

  # aws_s3_bucket.images will be updated in-place
  ~ resource "aws_s3_bucket" "images" {
        id                          = "djansang"
        tags                        = {
            "Name"        = "images"
            "environment" = "mtchoun-mouh-master"
            "project"     = "mtchoun-mouh"
        }
        # (10 unchanged attributes hidden)

      - server_side_encryption_configuration {
          - rule {
              - bucket_key_enabled = false -> null

              - apply_server_side_encryption_by_default {
                  - sse_algorithm = "AES256" -> null
                }
            }
        }

        # (1 unchanged block hidden)
    }

  # aws_s3_bucket.website will be updated in-place
  ~ resource "aws_s3_bucket" "website" {
        id                          = "mtchoun-mouh.mongulu.cm"
        tags                        = {
            "Name"        = "Website"
            "environment" = "mtchoun-mouh-master"
            "project"     = "mtchoun-mouh"
        }
        # (12 unchanged attributes hidden)

      - server_side_encryption_configuration {
          - rule {
              - bucket_key_enabled = false -> null

              - apply_server_side_encryption_by_default {
                  - sse_algorithm = "AES256" -> null
                }
            }
        }

        # (3 unchanged blocks hidden)
    }

  # local_file.index_page will be created
  + resource "local_file" "index_page" {
      + content              = (sensitive value)
      + content_base64sha256 = (known after apply)
      + content_base64sha512 = (known after apply)
      + content_md5          = (known after apply)
      + content_sha1         = (known after apply)
      + content_sha256       = (known after apply)
      + content_sha512       = (known after apply)
      + directory_permission = "0777"
      + file_permission      = "0777"
      + filename             = "../html/index.html"
      + id                   = (known after apply)
    }

Plan: 1 to add, 4 to change, 0 to destroy.
╷
│ Warning: Value for undeclared variable
│ 
│ The root module does not declare a variable named "PROXY_URL" but a value
│ was found in file
│ "/home/tfc-agent/.tfc-agent/component/terraform/runs/run-2xUD61CnBLLSkCAN/terraform.tfvars".
│ If you meant to use this value, add a "variable" block to the
│ configuration.
│ 
│ To silence these warnings, use TF_VAR_... environment variables to provide
│ certain "global" settings to all configurations in your organization. To
│ reduce the verbosity of these warnings, use the -compact-warnings option.------------------------------------------------------------------------

Cost estimation:

Resources: 3 of 5 estimated
           $1.74096/mo +$0.0

------------------------------------------------------------------------

aws_lambda_function.lambda: Modifying... [id=user_registration_consulcam]
aws_lambda_function.scan: Modifying... [id=scan_user_consulcam]
local_file.index_page: Creating...
aws_s3_bucket.website: Modifying... [id=mtchoun-mouh.mongulu.cm]
local_file.index_page: Creation complete after 0s [id=3df23f6ccd0c737240f079546f3bd4cefc59b440]
aws_lambda_function.scan: Modifications complete after 3s [id=scan_user_consulcam]
aws_lambda_function.lambda: Modifications complete after 5s [id=user_registration_consulcam]
aws_s3_bucket.images: Modifications complete after 6s [id=djansang]
aws_s3_bucket.website: Modifications complete after 6s [id=mtchoun-mouh.mongulu.cm]

Apply complete! Resources: 1 added, 4 changed, 0 destroyed.

Outputs:

register_table = "Register"
stage_url = "https://gozv19c3jf.execute-api.eu-central-1.amazonaws.com/dev"
website_url = "http://mtchoun-mouh.mongulu.cm.s3-website.eu-central-1.amazonaws.com"

Please sign in to comment.